Knocked N times .... Re happy to debug a night ... Found to change the # define MAXN = 100000 + 10 to #define MAXN = 100010 is over .... Feel the look of my hehe.
It seems that this problem uses a very classical line-segment tree and bit operations. I understand. But I don't understand. I do think it's very ingenious. Just want to say. Good pit.
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 #defineN = 1000106 7 structtree{8 intL,r;9 intCol//with a 32-bit int, each one corresponds to a color, using bit arithmetic instead of bool col[32]Ten BOOLCover//indicates that the interval is painted in the same color, the line segment tree efficiency, otherwise the insertion is 0 (n). One}tree[n<<2]; A - voidPushup (intRT) {//finally recursion back and change the parent node's color -Tree[rt].col=tree[l (RT)].col |Tree[r (RT)].col; the } - - voidBuildintLintRintroot) { -TREE[ROOT].L =l; +TREE[ROOT].R =R; -Tree[root].col =1; +Tree[root].cover =1; A at if(L = = r)return; - intMid = (l+r)/2; -Build (L, Mid,2*root); -Build (mid+1R2*root+1); - } - in - voidPushdown (intRoot) {//Pass the mark down totree[2*root].col =Tree[root].col; +tree[2*root].cover =1; -tree[2*root+1].col =Tree[root].col; thetree[2*root+1].cover =1; *Tree[root].cover =0;//I understand. $ }Panax Notoginseng voidUpdateintValintLintRintRoot) {//Update the color of an interval - if(l <= tree[root].l && TREE[ROOT].R <=r) { theTree[root].col = val;//indicates that the interval contains only one color. And it's Val. So modify complete, get it!!! +Tree[root].cover =1; A return ; the } + - if(Tree[root].col = = val)return;//indicates that there is only one color in the current range. So there is no need to go down,?? Pruning $ if(Tree[root].cover) Pushdown (root);//because you might want to modify the color of a certain sub-range of this interval. $ //or we're going to break down the interval. - intMid = (TREE[ROOT].L+TREE[ROOT].R)/2; - if(R <= Mid) update (Val, L, R,2*root); the Else if(L >= mid+1) Update (Val, L, R,2*root+1); - Else {WuyiUpdate (Val, L, Mid,2*root); theUpdate (Val, mid+1R2*root+1); - } WuPushup (root);//Modify the color of the parent node after recursively modifying the color of the child's node - } About $ intsum; - - voidQueryintLintRintRT) { - if(L<=TREE[RT].L && r>=TREE[RT].R) { ASum |=Tree[rt].col; + return ; the } - if(Tree[rt].cover) {//this interval is all 1 colors, there is no need to continue dividing the interval $Sum |= tree[rt].col;//bit operation codes for adding color types the return; the } the intMid= (TREE[RT].L+TREE[RT].R) >>1; the if(r<=mid) - query (l,r,l (RT)); in Else if(l>=mid+1) the query (L,r,r (RT)); the Else{ About query (l,mid,l (RT)); theQuery (mid+1, R,r (RT)); the } the } + - intsolve () { the intans=0;Bayi while(sum) { the if(sum&1) theans++; -sum>>=1; - } the returnans; the } the the voidSwapint&a,int&b) { - intt =A; theA =b; theb =T; the }94 the intMain () { the intl, N, M; the while(~SCANF ("%d%d%d", &l, &n, &m)) {98Build1L1); About Charop[Ten]; - intA, B, C;101 102 for(intI=0; i<m; ++i) {103scanf"%s", op);104 if(op[0] =='C') { thescanf"%d%d%d", &a, &b, &c);106 if(A >b) Swap (A, b);107Update1<< C-1), A, B,1);108 }109 Else if(op[0] =='P') { thescanf"%d%d", &a, &b);111 if(A >b) Swap (A, b); thesum =0;113Query (A, B,1); theprintf"%d\n", Solve ()); the } the }117 }118 return 0;119}View Code
POJ 2777 Segment Tree Application