POJ 2777 Count Color(线段树区间修改+位运算)
来源:互联网 发布:网络监控工程报价单 编辑:程序博客网 时间:2024/06/06 00:43
题目链接:点击打开链接
题意:两种操作, 一个是区间修改, 一个是区间查询颜色种类数。
该题因为要不断的求某个区间的颜色种类数, 我们可以用位运算的并来实现。
其他的就是线段树区间修改的经典操作了。
细节参见代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<stack>#include<bitset>#include<cstdlib>#include<cmath>#include<set>#include<list>#include<deque>#include<map>#include<queue>#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))using namespace std;typedef long long ll;const double PI = acos(-1.0);const double eps = 1e-6;const int mod = 1000000000 + 7;const int INF = 1000000000;const int maxn = 100000 + 10;int T,n,t,q,l,r,c,sum[maxn<<2],setv[maxn<<2];void PushUp(int o) { sum[o] = sum[o<<1] | sum[o<<1|1];}void pushdown(int o) { if(setv[o]) { setv[o<<1] = setv[o<<1|1] = setv[o]; sum[o<<1] = sum[o<<1|1] = (1<<setv[o]); setv[o] = 0; }}void build(int l, int r, int o) { int m = (l + r) >> 1; setv[o] = 0; sum[o] = (1<<1); if(l == r) return ; build(l, m, o<<1); build(m+1, r, o<<1|1);}void update(int L, int R, int c, int l, int r, int o) { int m = (l + r) >> 1; if(L <= l && r <= R) { sum[o] = (1 << c); setv[o] = c; return ; } pushdown(o); if(L <= m) update(L, R, c, l, m, o<<1); if(m < R) update(L, R, c, m+1, r, o<<1|1); PushUp(o);}int query(int L, int R, int l, int r, int o) { int m = (l + r) >> 1; if(L <= l && r <= R) { return sum[o]; } pushdown(o); int ans = 0; if(L <= m) ans |= query(L, R, l, m, o<<1); if(m < R) ans |= query(L, R, m+1, r, o<<1|1); PushUp(o); return ans;}char s[10];int main() { scanf("%d%d%d",&n, &t, &q); build(1, n, 1); while(q--) { scanf("%s",s); if(s[0] == 'C') { scanf("%d%d%d",&l, &r, &c); update(min(l,r), max(l,r), c, 1, n, 1); } else { scanf("%d%d",&l, &r); int ans = query(min(l,r), max(l,r), 1, n, 1), cnt = 0; for(int i=1;i<=t;i++) { if(ans & (1<<i)) ++cnt; } printf("%d\n",cnt); } } return 0;}
0 0
- 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 2777 Count Color(线段树+位运算)
- POJ 2777 Count Color(位运算+线段树+lazy+区间更新)
- POJ 2777 Count Color 【线段树 区间更新 按位或运算】
- POJ 2777Count Color (线段树区间更新 + 位运算压缩)
- poj 2777 Count Color(位运算+线段树区间更新 可用bitset记录)
- Count Color+POJ+线段树区间更新+位运算(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 ( 线段树&&位运算
- Android中下拉刷新和加载更多的实现方式
- 【WC2002】奶牛浴场
- java自带线程池和队列详细讲解
- 2016.01.17—-即便没有读者,你也要写博客
- zookeeper原理(转)
- POJ 2777 Count Color(线段树区间修改+位运算)
- 鸟瞰“网站服务器怎么工作?”
- CodeForces 584D Dima and Lisa
- Mac OS下安装配置Scala
- JavaWeb_SpringMVC和titles的简单整合
- JSONModel解析数据成Model
- JavaScript里的依赖注入
- css中的display属性
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)