265. Segment Coverage
★★☆ input file: xdfg.in
output file: xdfg.out
Simple comparison
Time limit: 2 s memory limit: MB
"Problem description"
There is a white bar with a length of L. There are two kinds of actions:
- Cover the [A, a+t] (0<=a, t<=l) of the bar with a black cloth of length T.
- Take a piece of black cloth away.
Input L and n operations, to output after each operation:
- How many black bands are in the bar.
- The total length of the black band on the bar.
"Input Format"
Input file first line two integers L (1<=l<=200000), N (1<=n<=200000)
There are n rows, the 2--n+1 line has 3 integer m,a,t,m for the operation type, 1 for the black cloth, 2 to remove the black cloth, a,t to the black cloth on the L start position and length, the black cloth to be taken to ensure that the original already exists.
"Output Format"
The output has n rows, two integers per line x,y,x indicates the number of black bands on L, and Y represents the total length of the black interval.
"Input and Output sample"
Input:
20 4
1 5 3
1 7 2
2 5 3
1 16 3
Output:
1 3
1 4
1 2
2 5
Exercises
Segment tree is covered by the interval, because there is no query operation, so it can not be pushdown. Then there is the Updata and insert operations. Updata to determine the value of the tag, and then update (is clear to black, or according to the child update); Add to find the interval to see whether it is a unit, if the element is directly updated, otherwise, to determine the scope of the tag.
AC Code:
#include <cstdio>using namespacestd;#defineN 200010#defineLC K<<1#defineRC K<<1|1#defineMid (L+r>>1)structnode{intL,r,sum,len; BOOLFlag;} Q[n<<2];intn,m,tag[n<<2];inlineintRead () {registerintx=0, f=1; RegisterCharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}voidUpdata (intKintLintR) { if(tag[k]>0) {Q[K].L=q[k].r=q[k].sum=1; q[k].len=r-l+1; } Else{Q[K].L=q[lc].l;q[k].r=Q[RC].R; Q[k].sum=q[lc].sum+q[rc].sum; if(q[lc].r==q[rc].l&&q[lc].r==1) q[k].sum--; Q[k].len=q[lc].len+Q[rc].len; } return ;}voidAddintKintLintRintXintYintv) { if(x<=l&&r<=y) {Tag[k]+=v; if(l==r) q[k].l=q[k].r=q[k].sum=q[k].len=tag[k]>0?1:0; ElseUpdata (K,L,R); return ; } if(y<=mid) Add (LC,L,MID,X,Y,V); Else if(x>mid) Add (rc,mid+1, r,x,y,v); ElseAdd (Lc,l,mid,x,mid,v), add (rc,mid+1, r,mid+1, y,v); Updata (k,l,r);}intMain () {Freopen ("xdfg.in","R", stdin); Freopen ("Xdfg.out","W", stdout); N=read (); m=read (); for(intI=1, x,y,z;i<=m;i++) {Z=read (); X=read (); y=read (); if(z==1) Add (1,1, n,x,x+y-1,1); ElseAdd (1,1, n,x,x+y-1,-1); printf ("%d%d\n", q[1].sum,q[1].len); } return 0;}
COGS 265 Segment Coverage