POJ 2777 Count Color(线段树、lazy思想)
2012-07-04 22:16
288 查看
大意是一块定长的木板,在上面涂颜色,每次涂一个区间,当询问某个区间时,要返回这个区间中的颜色数目。
这道题目其实和3468非常之像,也是要更新区间,也用到了lazy思想。可能略有不同的是,这道题目有个用到位运算的小技巧。由于总颜色数目较少(<= 30),一个区间上的颜色可以用一个32位的 int 表示,最后统计总颜色数时,只要运用或运算符(|)就可以。
这道题目其实和3468非常之像,也是要更新区间,也用到了lazy思想。可能略有不同的是,这道题目有个用到位运算的小技巧。由于总颜色数目较少(<= 30),一个区间上的颜色可以用一个32位的 int 表示,最后统计总颜色数时,只要运用或运算符(|)就可以。
#include<iostream> #include<cstdio> #define MID(x, y) ((x + y) >> 1) #define R(x) (x << 1 | 1) #define L(x) (x << 1) using namespace std; typedef struct{ int l, r; long long board; long long c; } NODE; NODE st[440000]; void build(int t, int l, int r){ st[t].l = l; st[t].r = r; st[t].c = 0; st[t].board = 1; // cout << "l--r: " << l <<" "<< r<< endl; if(l == r) return; else{ build(L(t), l, MID(l, r)); build(R(t), MID(l, r) + 1, r); } } void update(int t, int l, int r, int c){ int mid = MID(st[t].l, st[t].r); //cout << t << l << r << c << endl;; if(st[t].l == l && st[t].r == r){ st[t].board = 1; st[t].board = st[t].board << c - 1; st[t].c = c; return; } if(st[t].c != 0){ st[L(t)].board = 1; st[L(t)].board = st[L(t)].board << st[t].c - 1; st[L(t)].c = st[t].c; st[R(t)].board = 1; st[R(t)].board = st[R(t)].board << st[t].c - 1; st[R(t)].c = st[t].c; st[t].c = 0; } if(r <= mid){ update(L(t), l, r, c); }else if(l > mid){ update(R(t), l, r, c); }else{ update(L(t), l, mid, c); update(R(t), mid + 1, r, c); } st[t].board = (st[L(t)].board | st[R(t)].board); // cout << st[t].board << endl; } long long query(int t, int l, int r){ int mid = MID(st[t].l, st[t].r); if(st[t].l == l && st[t].r == r){ return st[t].board; } if(st[t].c != 0){ st[L(t)].board = 1; st[L(t)].board = st[L(t)].board << st[t].c - 1; st[L(t)].c = st[t].c; st[R(t)].board = 1; st[R(t)].board = st[R(t)].board << st[t].c - 1; st[R(t)].c = st[t].c; st[t].c = 0; } if(r <= mid){ return query(L(t), l, r); }else if(l > mid){ return query(R(t), l, r); }else{ return query(L(t), l, mid) | query(R(t), mid + 1, r); } } int count(long long a){ int b = 0; while(a != 0){ if(a % 2 == 1) b++; a = a >> 1; } return b; } int main(){ int L, T, O; char cmd[10]; scanf("%d %d %d", &L, &T, &O); build(1, 1, L); while(O--){ scanf("%s", cmd); if(cmd[0] == 'C'){ int x, y, z; scanf("%d %d %d", &x, &y, &z); if(y < x) swap(x, y); update(1, x, y, z); }else{ int x, y; scanf("%d %d", &x, &y); if(y < x) swap(x, y); printf("%d\n", count(query(1, x, y))); } } }
相关文章推荐
- poj 2777 Count Color(线段树 Lazy-Tag思想 成段更新+区间统计)
- POJ 2777 Count Color (线段树、lazy思想)
- POJ 2777 Count Color (线段树的区间更新+lazy tag)
- POJ 2777 count color(线段树,lazy标记)
- 【线段树(lazy)】poj 2777 Count Color
- [poj] 2777 - Count Color - 线段树 + 状态压缩思想(?)
- poj(2777)——Count Color(lazy思想,成段更新,区间统计)
- [POJ 2777]Count Color(线段树)
- POJ 2777 Count Color(线段树 + 状态压缩)
- poj 2777 Count Color(线段树+染色问题)
- poj 2777 Count Color(线段树 区间更新)
- poj 2777 Count Color (线段树)
- poj 2777 Count Color【线段树段更新】
- poj_2777Count Color线段树_区间更新
- poj 2777 count color解题报告【线段树】
- poj 2777 Count Color(线段树区间修改)
- POJ 2777 Count Color (线段树 区间覆盖染色问题)
- [POJ 2777]Count Color[线段树区间更新查询]
- POJ 2777 Count Color (线段树区间更新)
- POJ 2777 Count Color (线段树区间修改 + 状态压缩)