线段树直接处理,lazy标记记得下放。每个区间的各种颜色可以用二进制保存,因为最多只有30种颜色,所以用int就行。
两个区间合并后的颜色用或运算。
AC代码:
#include<cstdio>#include<algorithm>using namespace std;const int maxn = 4e5 + 5;struct node{ int L, R; int color, tag;}t[maxn];void Build(int l, int r, int cur){ //Build the tree t[cur].L = l; t[cur].R = r; t[cur].color = t[cur].tag = 1; if(l == r) return; int mid = (l + r) / 2; Build(l, mid, cur << 1); Build(mid + 1, r, (cur << 1) + 1);}void Update(int l, int r, int c, int cur){ //PRintf("%d %d/n",l,r); int l1 = t[cur].L, r1 = t[cur].R; if(l1 == l && r1 == r){ t[cur].color = 1 << (c - 1); t[cur].tag = 1; return; } if(t[cur].tag){ //Let the lazy tag down t[(cur << 1) + 1].tag = t[cur << 1].tag = t[cur].tag; t[(cur << 1) + 1].color = t[cur << 1].color = t[cur].color; } int mid = (l1 + r1) / 2; if(r <= mid) Update(l, r, c, cur << 1); else if(l >= mid + 1) Update(l, r, c, (cur << 1) + 1); else { Update(l, mid, c, cur << 1); Update(mid + 1, r, c, (cur << 1) + 1); } t[cur].color = t[cur << 1].color | t[(cur << 1) + 1].color; int x = t[cur].color; t[cur].tag = (x & (x - 1)) ? 0 :1;}int Find(int l, int r, int cur){ int l1 = t[cur].L, r1 = t[cur].R; if(l1 == l && r1 == r || t[cur].tag) return t[cur].color; if(t[cur].tag){ //Let the lazy tag down t[(cur << 1) + 1].tag = t[cur << 1].tag = t[cur].tag; t[(cur << 1) + 1].color = t[cur << 1].color = t[cur].color; } int mid = (l1 + r1) / 2; if(r <= mid) return Find(l, r, cur << 1); else if(l >= mid + 1) return Find(l, r, (cur << 1) + 1); else { int x = Find(l, mid, cur << 1); int y = Find(mid + 1, r, (cur << 1) + 1); return x | y; }}int main(){ int L, T, Q; while(scanf("%d%d%d", &L, &T, &Q) == 3){ Build(1, L, 1); getchar(); char ch; int a, b, c; for(int i = 0; i < Q; ++i){ scanf("%c",&ch); if(ch == 'C') { scanf("%d%d%d", &a, &b, &c); if(a > b) swap(a, b); Update(a, b, c, 1); } else { scanf("%d%d", &a, &b); if(a > b) swap(a, b); int x = Find(a, b, 1); int ans = 0; while(x > 0){ if(x & 1) ++ans; x >>= 1; } printf("%d/n", ans); } getchar(); } } return 0;}如有不当之处欢迎指出!
新闻热点
疑难解答