PKU 2777 线段树
2011-11-16 14:23
148 查看
/**************************************************************************************************** 这题还学了不少思想~用二进制数位统计颜色个数是个亮点~ ****************************************************************************************************/ #include <iostream> #include <functional> #include <algorithm> #include <complex> #include <cstdlib> #include <cstring> #include <fstream> #include <sstream> #include <utility> #include <bitset> #include <cctype> #include <cstdio> #include <limits> #include <memory> #include <string> #include <vector> #include <cmath> #include <ctime> #include <queue> #include <stack> #include <list> #include <map> #include <set> using namespace std; #define LOWBIT(x) ( (x) & ( (x) ^ ( (x) - 1 ) ) ) #define CLR(x, k) memset((x), (k), sizeof(x)) #define CPY(t, s) memcpy((t), (s), sizeof(s)) #define SC(t, s) static_cast<t>(s) #define LEN(s) static_cast<int>( strlen((s)) ) #define SZ(s) static_cast<int>( (s).size() ) typedef double LF; //typedef long long LL; //GNU C++ //typedef unsigned long long ULL; typedef __int64 LL; //Visual C++ 2010 typedef unsigned __int64 ULL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<double, double> PDD; typedef map<int, int>::iterator MI; typedef vector<int>::iterator VI; typedef list<int>::iterator LI; typedef set<int>::iterator SI; template <typename T> T sqa(const T &x) { return x * x; } template <typename T> T gcd(T a, T b) { if (!a || !b) { return max(a, b); } T t; while (t = a % b) { a = b; b = t; } return b; } template <typename T> T ext_gcd(T a, T b, T &x, T &y) { if (!b) { x = 1; y = 0; return a; } T d = ext_gcd(b, a % b, x, y); T t = x; x = y; y = t - a / b * y; return d; } template <typename T> T invmod(T a, T p) { LL inv, y; ext_gcd(a, p, inv, y); inv < 0 ? inv += p : 0; return inv; } const int INF_INT = 0x3f3f3f3f; const LL INF_LL = 0x7fffffffffffffffLL; //15f const double oo = 10e9; const double eps = 10e-7; const double PI = acos(-1.0); const int MAXN = 100004 << 2; int n, m, k; struct Node { int left, right; int col; bool cov; }st[MAXN]; int inline shl(const int &x) { return 1 << x; } int inline ll(const int &x) { return x << 1; } int inline rr(const int &x) { return x << 1 | 1; } void initST() { memset(st, 0, sizeof(st)); return ; } void buildST(int l, int r, int t) { st[t].left = l; st[t].right = r; st[t].col = shl(1); st[t].cov = true; if (l == r) { return ; } int mid = (l + r) >> 1; buildST(l, mid, ll(t)); buildST(mid + 1, r, rr(t)); return ; } void updateST(int l, int r, int c, int t) { if (l <= st[t].left && st[t].right <= r) { st[t].col = shl(c); st[t].cov = true; return ; } if (st[t].cov) { st[t].cov = false; st[ ll(t) ].cov = st[ rr(t) ].cov = true; st[ ll(t) ].col = st[ rr(t) ].col = st[t].col; } int mid = st[ ll(t) ].right; if (r <= mid) { updateST(l, r, c, ll(t)); } else if (l > mid) { updateST(l, r, c, rr(t)); } else { updateST(l, mid, c, ll(t)); updateST(mid + 1, r, c, rr(t)); } st[t].col = st[ ll(t) ].col | st[ rr(t) ].col; return ; } int query(int l, int r, int t) { if (l <= st[t].left && st[t].right <= r) { return st[t].col; } if (st[t].cov) { st[t].cov = false; st[ ll(t) ].cov = st[ rr(t) ].cov = true; st[ ll(t) ].col = st[ rr(t) ].col = st[t].col; } int mid = st[ ll(t) ].right; if (r <= mid) { return query(l, r, ll(t)); } else if (l > mid) { return query(l, r, rr(t)); } return query(l, mid, ll(t)) | query(mid + 1, r, rr(t)); } int countbit(const int &x) { int t = x, c = 0; while (t) { (t & 0x1) ? ++c : 0; t >>= 1; } return c; } void ace() { char op[2]; int l, r, c; while (cin >> n >> m >> k) { initST(); buildST(1, n, 1); while (k--) { cin >> op >> l >> r; if (l > r) { swap(l, r); } if ('C' == op[0]) { cin >> c; updateST(l, r, c, 1); } else { printf("%d\n", countbit(query(l, r, 1))); } } } return ; } int main() { ace(); return 0; }
相关文章推荐
- PKU 2777 【线段树】
- [PKU] 2777 Count Color -- 线段树
- pku 2777(经典线段树染色问题)
- [PKU 2777] 线段树(一) {概述 基本操作}
- PKU 2777 - Count Color(线段树 + 区间修改)
- 【线段树】PKU2777 区间更新区间询问(位优化)
- pku2777 线段树 (位运算加速)
- pku 2777Count Color 线段树 懒惰标记 位运算(位或)
- PKU 2777 Count Color (线段树区间更新)
- pku2777 线段树(染色问题)
- [Pku 2777] 线段树(六) {总结}
- 线段树专辑——pku 2777 Count Color
- pku 2777 cout the color--线段树
- pku 2777 Count Color(线段树变形)
- poj-2777 线段树lazy标记+位运算
- 区间更新+区间统计——pku2777
- POJ 2777 Count Color 线段树
- 线段树专辑 —— pku 2482 Stars in Your Window
- POJ 2777 Count Color 线段树染色问题+位运算表示状态
- POJ 2777 Count Color (线段树)