Bzoj 1858: [Scoi2010] Sequence operation

Source: Internet
Author: User

1858: [Scoi2010] sequence operation time limit:10 Sec Memory limit:64 MB segment tree, for each interval need to maintain the left and right of the 1 and 0 consecutive number, and in op=4 special treatment. DESCRIPTIONLXHGWW recently received a 01 sequence, the sequence contains n number, these numbers are either 0, or 1, now for this sequence there are five transformation operations and inquiry operation: 0 A B to the [A, b] range of all the numbers into 0 1 a b [A, b] All the numbers in the interval become 1 2 A B all the numbers in the [A, a] range are reversed, in other words, turn all 0 into 1, turn all 1 into 0 3 a B and ask [A, b] the total number of 1 4 a B in the interval to ask [A, b] the maximum number of consecutive 1 for each inquiry operation, l XHGWW need to give answers, smart handlers, can you help him? Input data The first row consists of 2 numbers, N and M, respectively, indicating the length of the sequence and the number of operations the second line includes n number, indicating the initial state of the sequence next m, 3 numbers per line, OP, a, B, (0<=op<=4,0<=a<=b< N) indicates an operation marked op for interval [A, b] = "" "<=" "div=" ">output for each query operation, output one line, including 1 numbers, indicating its corresponding answer, sample Input10 10
0 0 0 1 1 0 1 0 1 1
1 0 2
3 0 5
2 2 2
4 0 4
0 3 6
2 3 7
4 2 8
1 0 5
0 5 6
3 3 9
Sample Output5
2
6
5

HINT

For 30% of data, 1<=n, m<=1000
For 100% of data, 1<=n, m<=100000

Source

#include <cstdio>#defineM 100010inlineintMaxintAintb) {returnA>b?a:b;}structtree{intL,R,S,LAZY,SUM,ML1,MR1,MM1,ML0,MR0,MM0;} tr[m*6];intA[m],n,m,x,y,v,op;inlineintRead () {inttmp=0; CharCh=GetChar ();  while(ch<'0'|| Ch>'9') ch=GetChar ();  while(ch>='0'&&ch<='9') {tmp=tmp*Ten+ch-'0'; ch=GetChar ();} returntmp;} InlinevoidPuintp) {    intp1=p<<1,p2=p<<1|1; Tr[p].sum=tr[p1].sum+tr[p2].sum; TR[P].MM1=tr[p1].mr1+TR[P2].ML1; TR[P].MM0=tr[p1].mr0+tr[p2].ml0; if(TR[P1].MR1==TR[P1].S) tr[p].ml1=tr[p1].mr1+TR[P2].ML1; Elsetr[p].ml1=TR[P1].ML1; if(TR[P1].MR0==TR[P1].S) tr[p].ml0=tr[p1].mr0+tr[p2].ml0; Elsetr[p].ml0=tr[p1].ml0; if(TR[P2].MR1==TR[P2].S) tr[p].mr1=tr[p2].mr1+TR[P1].MR1; Elsetr[p].mr1=TR[P2].MR1; if(TR[P2].MR0==TR[P2].S) tr[p].mr0=tr[p2].mr0+tr[p1].mr0; Elsetr[p].mr0=tr[p2].mr0; TR[P].MM1=Max (Tr[p].mm1,max (TR[P1].MM1,TR[P2].MM1)); TR[P].MM0=Max (Tr[p].mm0,max (TR[P1].MM0,TR[P2].MM0));} InlinevoidChangeintp) {    intt1=tr[p].ml0,t2=tr[p].mr0,t3=tr[p].mm0; Tr[p].ml0=TR[P].ML1; TR[P].ML1=T1; Tr[p].mr0=TR[P].MR1; TR[P].MR1=T2; TR[P].MM0=tr[p].mm1; TR[P].MM1=T3;} InlinevoidMakeintLintRintp) {TR[P].L=l,tr[p].r=r,tr[p].s=tr[p].r-tr[p].l+1, tr[p].lazy=-1; if(l==r) {tr[p].sum=A[l]; TR[P].ML1=tr[p].mm1=tr[p].mr1=A[l]; Tr[p].ml0=tr[p].mm0=tr[p].mr0=a[l]^1; return; }    intMid= (l+r) >>1; Make (L,mid,p<<1); Make (Mid+1,r,p<<1|1); PU (p);} InlinevoidPdintp) {    intlz=tr[p].lazy,p1=p<<1,p2=p<<1|1; if(lz==0) {Tr[p1].lazy=tr[p2].lazy=0; Tr[p1].sum=0; Tr[p2].sum=0; TR[P1].ML1=tr[p1].mr1=tr[p1].mm1=0; TR[P2].ML1=tr[p2].mr1=tr[p2].mm1=0; Tr[p1].ml0=tr[p1].mr0=tr[p1].mm0=Tr[p1].s; Tr[p2].ml0=tr[p2].mr0=tr[p2].mm0=Tr[p2].s; }    Else if(lz==1) {Tr[p1].lazy=tr[p2].lazy=1; Tr[p1].sum=Tr[p1].s; Tr[p2].sum=Tr[p2].s; TR[P1].ML1=tr[p1].mr1=tr[p1].mm1=Tr[p1].s; TR[P2].ML1=tr[p2].mr1=tr[p2].mm1=Tr[p2].s; Tr[p1].ml0=tr[p1].mr0=tr[p1].mm0=0; Tr[p2].ml0=tr[p2].mr0=tr[p2].mm0=0; }    Else if(lz==2)    {        if(tr[p1].lazy==1) tr[p1].lazy=0; Else if(tr[p1].lazy==0) tr[p1].lazy=1; Else if(tr[p1].lazy==2) tr[p1].lazy=-1; Elsetr[p1].lazy=2; if(tr[p2].lazy==1) tr[p2].lazy=0; Else if(tr[p2].lazy==0) tr[p2].lazy=1; Else if(tr[p2].lazy==2) tr[p2].lazy=-1; Elsetr[p2].lazy=2; Tr[p1].sum=tr[p1].s-tr[p1].sum; Tr[p2].sum=tr[p2].s-tr[p2].sum;    Change (p1), change (P2); } Tr[p].lazy=-1;} InlineintFind1 (intLintRintp)    {PD (P); if(TR[P].L==L&AMP;&AMP;TR[P].R==R)returntr[p].sum; intMid= (TR[P].L+TR[P].R) >>1; if(MID&GT;=R)returnFind1 (l,r,p<<1); Else if(mid<l)returnFind1 (l,r,p<<1|1); Else returnFind1 (l,mid,p<<1) +find1 (mid+1,r,p<<1|1);} InlineintFindl (intLintRintp)    {PD (P); if(Tr[p].ml1+l>r)returnr-l+1; Else returnTR[P].ML1;} InlineintFindr (intLintRintp)    {PD (P); if(L+tr[p].mr1>r)returnr-l+1; Else returnTR[P].MR1;} InlineintFind2 (intLintRintp)    {PD (P); if(TR[P].L==L&AMP;&AMP;TR[P].R==R)returnMax (Tr[p].ml1,max (TR[P].MM1,TR[P].MR1)); intMid= (TR[P].L+TR[P].R) >>1; if(MID&GT;=R)returnFind2 (l,r,p<<1); Else if(mid<l)returnFind2 (l,r,p<<1|1); Else returnMax (Max (Find2 (l,mid,p<<1), Find2 (mid+1,r,p<<1|1)), Findl (mid+1,r,p<<1|1) +findr (l,mid,p<<1));} InlinevoidxgintLintRintCintp)    {PD (P); if(tr[p].l==l&&tr[p].r==r) {Tr[p].lazy=C; if(c==0) {TR[P].ML1=tr[p].mr1=tr[p].mm1=0; Tr[p].ml0=tr[p].mr0=tr[p].mm0=Tr[p].s; Tr[p].sum=0; }        Else if(c==1) {tr[p].ml0=tr[p].mr0=tr[p].mm0=0; TR[P].ML1=tr[p].mr1=tr[p].mm1=Tr[p].s; Tr[p].sum=Tr[p].s; }        Else{tr[p].sum=tr[p].s-tr[p].sum;        Change (p); }        return; }    intMid= (TR[P].L+TR[P].R) >>1; if(mid>=r) XG (l,r,c,p<<1); Else if(mid<l) XG (l,r,c,p<<1|1); Else{xg (l,mid,c,p<<1); XG (Mid+1,r,c,p<<1|1); } PU (P);}intMain () {n=read (), m=read ();  for(intI=1; i<=n;i++) a[i]=read (); Make (1N1);  for(intI=0; i<m;i++) {op=read (); X=read (); y=read (); if(op<3) XG (x+1, y+1, OP,1); Else if(op==3) printf ("%d\n", Find1 (x+1, y+1,1)); Elseprintf"%d\n", Find2 (x+1, y+1,1)); }    return 0;}

Bzoj 1858: [Scoi2010] Sequence action

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.