1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include<bits/stdc++.h> #define I inline #define It set<node>::iterator using namespace std; inline int read(){int res=0,f=1;char ch=getchar();while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=getchar();}while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar();return res*f;} inline void write(int x){if(x<0) putchar('-'),x=-x;if(x<10) putchar(x+'0');else write(x/10),putchar(x%10+'0');} struct node{ int l,r;mutable int v; node(int L,int R=-1,int V=0):l(L),r(R),v(V){} bool operator<(const node& x)const{return l<x.l;} }; set<node> s; I It split(int x){ It it=s.lower_bound(x); if(it!=s.end()&&it->l==x) return it; it--; int L=it->l,R=it->r,V=it->v; return s.erase(it),s.insert(node(L,x-1,V)),s.insert(node(x,R,V)).first; } I void assign(int l,int r,int v){ It it2=split(r+1),it1=split(l); s.erase(it1,it2);s.insert(node(l,r,v)); } I void flip(int l,int r){ It it2=split(r+1),it1=split(l); for(;it1!=it2;it1++) it1->v^=1; } I int sum(int l,int r){ It it2=split(r+1),it1=split(l);int s=0; for(;it1!=it2;it1++) s+=it1->v*(it1->r-it1->l+1); return s; } I int getmax(int l,int r){ It it2=split(r+1),it1=split(l);int s=0,Max=0; for(;it1!=it2;it1++) if(it1->v==1) s+=it1->r-it1->l+1,Max=max(Max,s); else s=0; return max(Max,s); } int n,m; int main(){ n=read(),m=read(); for(int x,i=0;i<n;i++) x=read(),s.insert(node(i,i,x)); s.insert(node(n,n,0)); for(int op,l,r,i=1;i<=m;i++){ op=read(),l=read(),r=read(); if(op==0) assign(l,r,0); if(op==1) assign(l,r,1); if(op==2) flip(l,r); if(op==3) write(sum(l,r)),putchar('\n'); if(op==4) write(getmax(l,r)),putchar('\n'); } }
|