Poj 2777 Counting Colors
This interval is used to record a single line segment tree and a single record.ConsistentColor or 0
The trick is that the color can be represented by 1 <(color-1), so that the result is all the collected colors or operations, the number of a number of 1 is enough.
This consistency is reflected in the following process of merging and splitting. You only need to put the same color down (0 will not be placed). During merging, only the colors of the two sides are consistent.
#define tree_merge tree_obj = tree[root<<1] == tree[root<<1|1] ? tree[root << 1] : 0#define tree_split decl_mid; if(tree_obj){tree[root<<1]=tree[root<<1|1] = tree_obj; tree_obj = 0;}
#include
#include
#include
#include #define FRAME tree_frame#define eq(x, y) (!strcmp((x), (y)))#define times(n, i) for(int i=0; i
= sl && r <= sr)#define TAG_OUTSIDE (l >= sr || r <= sl)#define TAG_LEAF (l == r-1)#define decl_mid int mid = (l + r) / 2;#define recur_left l, mid, root << 1#define recur_right mid, r, root << 1 | 1void build(FRAME){ if(TAG_LEAF){ tree_obj = 1; return; } tree_split; build(recur_left); build(recur_right); tree_merge; }void update(int value, int sl, int sr, FRAME){//printf("%d %d %d %d %d %d\n", value, sl, sr, l, r, root); if(TAG_INSIDE) { tree_obj = 1 << (value - 1); return ; } if(TAG_OUTSIDE){ return; } tree_split; update(value, sl, sr, recur_left ); update(value, sl, sr, recur_right); tree_merge;}int result;void query_impl(int sl, int sr, FRAME){ if(TAG_INSIDE){ if(tree_obj != 0){ result |= tree_obj; return; } } if(TAG_OUTSIDE){ return; } tree_split; query_impl(sl, sr, recur_left); query_impl(sl, sr, recur_right); tree_merge;}int query(int sl, int sr){ result = 0; query_impl(sl, sr); int ans = 0; // printf("%x\n", result); while(result > 0) ans += result & 1, result >>= 1; return ans;}int main(){ scanf("%d%d%d", &N, &T, &M); build(); while(M--){int a, b, c;char tmp[1024];scanf("%s%d%d", tmp, &a, &b);if(a > b) std::swap(a, b);if(eq(tmp, "C")) { scanf("%d", &c); update(c, a, b+1);}if(eq(tmp, "P")){ printf("%d\n", query(a, b+1));} } return 0;}