POJ 2777 Count Color
就一个线段树,颜色二进制表示就可以,成段更新成段查询延迟操作
代码:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define lson(x) ((x<<1)+1) #define rson(x) ((x<<1)+2) const int N = 100005; struct Node { int l, r, color, lazy; } node[N * 4]; void pushup(int x) { node[x].color = node[lson(x)].color | node[rson(x)].color; } void pushdown(int x) { if (node[x].lazy) { node[lson(x)].color = node[x].lazy; node[lson(x)].lazy = node[x].lazy; node[rson(x)].color = node[x].lazy; node[rson(x)].lazy = node[x].lazy; node[x].lazy = 0; } } void build(int l, int r, int x = 0) { node[x].l = l; node[x].r = r; node[x].lazy = 0; if (l == r) { node[x].color = 1; return; } int mid = (l + r) / 2; build(l, mid, lson(x)); build(mid + 1, r, rson(x)); pushup(x); } void add(int l, int r, int c, int x = 0) { if (node[x].l >= l && node[x].r <= r) { node[x].color = (1<<(c - 1)); node[x].lazy = (1<<(c - 1)); return; } pushdown(x); int mid = (node[x].l + node[x].r) / 2; if (l <= mid) add(l, r, c, lson(x)); if (r > mid) add(l, r, c, rson(x)); pushup(x); } int query(int l, int r, int x = 0) { if (node[x].l >= l && node[x].r <= r) return node[x].color; int mid = (node[x].l + node[x].r) / 2; int ans = 0; pushdown(x); if (l <= mid) ans |= query(l, r, lson(x)); if (r > mid) ans |= query(l, r, rson(x)); pushup(x); return ans; } int bitcount(int x) { return x == 0 ? 0 : bitcount(x>>1) + (x&1); } int l, t, o; char str[5]; int a, b, v; int main() { while (~scanf("%d%d%d", &l, &t, &o)) { build(1, l); while (o--) { scanf("%s%d%d", str, &a, &b); if (a > b) swap(a, b); if (str[0] == 'C') { scanf("%d", &v); add(a, b, v); } else printf("%d\n", bitcount(query(a, b))); } } return 0; }
时间: 2024-11-05 13:32:11