Title Description Description
There are N points on a line axis, respectively, 1~n. At first all the dots were dyed black. Then
We carry out m operations, and the first operation will [Li,ri] these dianran into white. Please output each operation after execution
The number of black dots remaining.
Enter a description
Input Description
Enter a behavior of N and M. The following m lines are two numbers per line Li, Ri
Output description
Output Description
Output m line, the number of black points remaining after each operation.
Sample input
Sample Input
10 3
3 3
5 7
2 8
Sample output
Sample Output
9
6
3
Data range and Tips
Data Size & Hint
Data limitations
1<=n<=2000,1<=m<=2000 for 30% of the data
1<=li<=ri<=n<=200000,1<=m<=200000 for 100% data
Test instructions: The Chinese problem face
Solving: Interval update output tree[1].sum;
if (tree[root].sum==0) return;//This line of code is the key
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <queue>5#include <stack>6 #definell Long Long7 #defineMAXN 2000058 using namespacestd;9 structnodeTen { One intL,r; A intsum; -}tree[5*MAXN]; - intx, y; the intn,q; - intans=0; - voidBuildtree (intRootintLeftintRight ) - { +Tree[root].l=Left ; -Tree[root].r=Right ; + if(left==Right ) A { attree[root].sum=1; - return ; - } - intMid= (left+right) >>1; -Buildtree (root<<1, left,mid); -Buildtree (root<<1|1, mid+1, right); intree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum; - } to voidUpdata (intRootintLeftintRightintc) + { - if(tree[root].sum==0)return; the if(tree[root].l==left&&tree[root].r==Right ) * { $tree[root].sum=0;Panax Notoginseng return ; - } the if(tree[root].l==TREE[ROOT].R) + return ; A intMid= (TREE[ROOT].L+TREE[ROOT].R) >>1; the if(right<=mid) +Updata (root<<1, left,right,c); - Else $ { $ if(left>mid) -Updata (root<<1|1, left,right,c); - Else the { -Updata (root<<1, left,mid,c);WuyiUpdata (root<<1|1, mid+1, right,c); the } - } Wutree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum; - } About intMain () $ { -scanf"%d%d",&n,&q); -Buildtree (1,1, n); - for(intj=1; j<=q;j++) A { +scanf"%d%d",&x,&y); theUpdata (1, X, Y,0); -printf"%d\n", tree[1].sum);}
return 0;
}
Codevs 1191 Segment Tree interval update (water)