hdu3911 线段树 区间合并

时间:2023-03-09 03:26:02
hdu3911 线段树 区间合并
 //Accepted 3911 750MS 9872K
 //线段树 区间合并
 #include <cstdio>
 #include <cstring>
 #include <iostream>
 #include <queue>
 #include <cmath>
 #include <algorithm>
 using namespace std;
 /**
   * This is a documentation comment block
   * 如果有一天你坚持不下去了,就想想你为什么走到这儿!
   * @authr songt
   */
 ;
 struct node
 {
     int l,r;
     int L1,R1;
     int L0,R0;
     int sum1,sum0;
     int change;
 }f[imax_n*];
 int a[imax_n];
 int max(int a,int b)
 {
     return a>b?a:b;
 }
 int min(int a,int b)
 {
     return a<b?a:b;
 }
 //合并
 //
 void pushUp(int t)
 {
     *t].r-f[*t].l+;
     *t+].r-f[*t+].l+;
     f[t].L1=f[*t].L1;
     *t].L1==lLen) f[t].L1+=f[*t+].L1;
     f[t].R1=f[*t+].R1;
     *t+].R1==rLen) f[t].R1+=f[*t].R1;
     f[t].L0=f[*t].L0;
     *t].L0==lLen) f[t].L0+=f[*t+].L0;
     f[t].R0=f[*t+].R0;
     *t+].R0==rLen) f[t].R0+=f[*t].R0;
     f[t].sum1=max(f[*t].sum1,f[*t+].sum1);
     f[t].sum1=max(f[t].sum1,f[*t].R1+f[*t+].L1);
     f[t].sum0=max(f[*t].sum0,f[*t+].sum0);
     f[t].sum0=max(f[t].sum0,f[*t].R0+f[*t+].L0);
 }
 void swap(int &a,int &b)
 {
     int t=a;
     a=b;
     b=t;
 }
 void pushDown(int t)
 {
     )
     {
         f[*t].change^=;
         f[*t+].change^=;
         f[t].change=;
         swap(f[*t].L0,f[*t].L1);
         swap(f[*t].R0,f[*t].R1);
         swap(f[*t].sum0,f[*t].sum1);

         swap(f[*t+].L0,f[*t+].L1);
         swap(f[*t+].R0,f[*t+].R1);
         swap(f[*t+].sum0,f[*t+].sum1);
     }
 }
 void build(int t,int l,int r)
 {
     f[t].l=l;
     f[t].r=r;
     f[t].change=;
     if (l==r)
     {
         )
         {
             f[t].L1=f[t].R1=;
             f[t].L0=f[t].R0=;
             f[t].sum1=;
             f[t].sum0=;
         }
         else
         {
             f[t].L1=f[t].R1=;
             f[t].L0=f[t].R0=;
             f[t].sum1=;
             f[t].sum0=;
         }
         return ;
     }
     ;
     build(*t,l,mid);
     build(*t+,mid+,r);
     pushUp(t);
 }
 void update(int t,int l,int r)
 {
     if (f[t].l==l && f[t].r==r)
     {
         f[t].change^=;
         swap(f[t].L0,f[t].L1);
         swap(f[t].R0,f[t].R1);
         swap(f[t].sum0,f[t].sum1);
         return ;
     }
     pushDown(t);
     ;
     *t,l,r);
     else
     {
         *t+,l,r);
         else
         {
             update(*t,l,mid);
             update(*t+,mid+,r);
         }
     }
     pushUp(t);
 }
 int query(int t,int l,int r)
 {
     if (f[t].l==l && f[t].r==r)
     {
         return f[t].sum1;
     }
     pushDown(t);
     ;
     *t,l,r);
     else
     {
         *t+,l,r);
         else
         {
             *t,l,mid);
             *t+,mid+,r);
             int ans=max(sum1,sum2);
             //在左半区间的长度不应大于该段区间在左半区间的长度
             ans=max(ans,min(f[*t].r-l+,f[*t].R1)+min(r-f[*t+].l+,f[*t+].L1));
             return ans;
         }
     }
 }
 int n;
 int Q,k,x,y;
 void slove()
 {
     build(,,n);
     scanf("%d",&Q);
     ;i<=Q;i++)
     {
         scanf("%d%d%d",&k,&x,&y);
         if (x>y) swap(x,y);
         )
         {
             printf(,x,y));
         }
         else
         {
             update(,x,y);
         }
     }
 }
 int main()
 {
     while (scanf("%d",&n)!=EOF)
     {
         ;i<=n;i++)
         scanf("%d",&a[i]);
         slove();
     }
     ;
 }