这个线段树要维护的东西挺多。。。连样例都不敢拿来调。。只好自己出更弱的数据0.0
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<bits/stdc++.h> 2 #define inc(i,l,r) for(i=l;i<=r;i++) 3 #define dec(i,l,r) for(i=1;i>=r;i--) 4 #define inf 1e9 5 #define mem(a) memset(a,0,sizeof(a)) 6 #define NM 100000+5 7 using namespace std; 8 int n,m,i,t,x,y; 9 struct info{ 10 int s,l,r,m,z,w,x,y,n; 11 info(int t=0){ 12 z=-1; 13 l=r=s=m=t; 14 x=y=w=n=t^1; 15 } 16 }T[3*NM],none; 17 info operator+(const info&x,const info&y){ 18 info f; 19 f.z-1; 20 if(x.w==0)f.l=x.s+y.l;else f.l=x.l; 21 if(y.w==0)f.r=y.s+x.r;else f.r=y.r; 22 f.s=x.s+y.s; 23 f.m=max(x.m,y.m); 24 f.m=max(f.m,x.r+y.l); 25 if(x.s==0)f.x=x.w+y.x;else f.x=x.x; 26 if(y.s==0)f.y=y.w+x.y;else f.y=y.y; 27 f.w=x.w+y.w; 28 f.n=max(x.n,y.n); 29 f.n=max(f.n,x.y+y.x); 30 return f; 31 } 32 void mark(int i){ 33 if(T[i].z==2)T[i].z=-1; 34 else if(T[i].z==-1)T[i].z=2; 35 else T[i].z^=1; 36 } 37 void pushdown(int i){ 38 info *t=&T[i]; 39 if(t->z==2){ 40 swap(t->l,t->x);swap(t->r,t->y);swap(t->s,t->w);swap(t->m,t->n); 41 mark(i*2);mark(i*2+1); 42 }else if(t->z==1){ 43 t->l=t->r=t->m=t->s=t->s+t->w; 44 t->w=t->n=t->x=t->y=0; 45 T[i*2].z=T[i*2+1].z=1; 46 }else if(t->z==0){ 47 t->w=t->n=t->x=t->y=t->s+t->w; 48 t->l=t->r=t->m=t->s=0; 49 T[i*2].z=T[i*2+1].z=0; 50 } 51 t->z=-1; 52 } 53 void build(int i,int x,int y){ 54 int t=(x+y)>>1; 55 if(x==y){ 56 scanf("%d",&x); 57 T[i]=info(x); 58 return; 59 } 60 build(i*2,x,t);build(i*2+1,t+1,y); 61 T[i]=T[i*2]+T[i*2+1]; 62 } 63 void deal(int i,int x,int y,int a,int b,int k){ 64 int t=(x+y)>>1; 65 pushdown(i); 66 if(b<x||y<a)return; 67 if(a<=x&&y<=b){ 68 T[i].z=k; 69 pushdown(i); 70 return; 71 } 72 deal(i*2,x,t,a,b,k);deal(i*2+1,t+1,y,a,b,k); 73 T[i]=T[i*2]+T[i*2+1]; 74 } 75 void ch(int i,int x,int y,int a,int b){ 76 int t=(x+y)>>1; 77 pushdown(i); 78 if(b<x||y<a)return; 79 if(a<=x&&y<=b){ 80 mark(i); 81 pushdown(i); 82 return; 83 } 84 ch(i*2,x,t,a,b);ch(i*2+1,t+1,y,a,b); 85 T[i]=T[i*2]+T[i*2+1]; 86 } 87 int sum(int i,int x,int y,int a,int b){ 88 int t=(x+y)>>1; 89 if(b<x||a>y)return 0; 90 pushdown(i); 91 if(a<=x&&y<=b)return T[i].s; 92 return sum(i*2,x,t,a,b)+sum(i*2+1,t+1,y,a,b); 93 } 94 info link(int i,int x,int y,int a,int b){ 95 int t=(x+y)>>1; 96 if(b<x||y<a)return none; 97 pushdown(i); 98 if(a<=x&&y<=b)return T[i]; 99 return link(i*2,x,t,a,b)+link(i*2+1,t+1,y,a,b); 100 } 101 int main(){ 102 none=info(0); 103 scanf("%d%d",&n,&m); 104 build(1,1,n); 105 inc(i,1,m){ 106 scanf("%d%d%d",&t,&x,&y); 107 x++;y++; 108 if(t==0||t==1)deal(1,1,n,x,y,t); 109 else if(t==2)ch(1,1,n,x,y); 110 else if(t==3)printf("%d\n",sum(1,1,n,x,y)); 111 else if(t==4)printf("%d\n",link(1,1,n,x,y).m); 112 } 113 return 0; 114 }