POJ 2777 Count Color ( 线段树&&位运算
2017-09-21 17:14
369 查看
Count Color
Description
Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, … L from left to right, each is 1 centimeter long. Now we have to color the board - one segment with only one color. We can do following two operations on the board:
“C A B C” Color the board from segment A to segment B with color C.
“P A B” Output the number of different colors painted between segment A and segment B (including).
In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, … color T. At the beginning, the board was painted in color 1. Now the rest of problem is left to your.
Input
First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains “C A B C” or “P A B” (here A, B, C are integers, and A may be larger than B) as an operation defined previously.Output
Ouput results of the output operation in order, each line contains a number.Sample Input
2 2 4 C 1 1 2 P 1 2 C 2 2 2 P 1 2
Sample Output
2 1
题意
有两个操作1 把一个区间变成一个颜色
2 查
4000
询区间中的颜色总量
AC代码
#include <cstdio> #include <algorithm> using namespace std; #define LL long long const int MAXN = 1e5+10; struct node { int l, r; int lazy; int color; }tree[MAXN*4]; void build(int l,int r,int st) { tree[st].l = l; tree[st].r = r; tree[st].lazy = 0; tree[st].color = 0; if(l == r) return ; int mid = (l+r) >> 1; build(l,mid,st<<1); build(mid+1,r,st<<1|1); } void update(int l, int r,int st,int val) { if(tree[st].l==l && tree[st].r==r) { tree[st].lazy = 1; tree[st].color = 1<<(val-1); //我们知道颜色的种类数一共不超过30,所以TAT return; } if(tree[st].lazy) { tree[st].lazy = 0; tree[st<<1].lazy = tree[st<<1|1].lazy = 1; tree[st<<1].color = tree[st<<1|1].color = tree[st].color; } int mid = (tree[st].l+tree[st].r) >> 1; if(r <= mid) update(l,r,st<<1,val); else if(l > mid) update(l,r,st<<1|1,val); else { update(l,mid,st<<1,val); update(mid+1,r,st<<1|1,val); } tree[st].color = tree[st<<1].color | tree[st<<1|1].color; if(tree[st<<1].lazy && tree[st<<1|1].lazy && tree[st<<1].color==tree[st<<1|1].color) tree[st].lazy = 1; } int query(int l,int r,int st) { if(l==tree[st].l && r==tree[st].r) return tree[st].color; if(tree[st].lazy) return tree[st].color; //剪下枝 int mid = (tree[st].l+tree[st].r) >> 1; if(r <= mid) return query(l,r,st<<1); else if(l > mid) return query(l,r,st<<1|1); else { return query(l,mid,st<<1)|query(mid+1,r,st<<1|1); } } int main() { int n, t, q; while(~scanf("%d%d%d",&n,&t,&q)) { build(1,n,1); tree[1].lazy = 1; tree[1].color = 1; while(q--) { char ch[10]; scanf("%s",ch); int x, y, z; if(ch[0] == 'C') { scanf("%d%d%d",&x,&y,&z); if(x > y) swap(x,y); update(x,y,1,z); } else { scanf("%d%d",&x,&y); if(x > y) swap(x,y); LL k = query(x,y,1); int num = 0; for(int i = 0; i < t; i++) { if(k&(1<<i)) num++; } printf("%d\n",num); } } } return 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
- poj 2777 Count Color(线段树区区+染色问题)
- POJ 2777 Count Color(线段树、lazy思想)
- poj 2777 Count Color(线段树)
- poj 2777 Count Color
- POJ 2777 Count Color(线段树:区间覆盖)
- ACM: 线段树 poj 2777 继续熟练线…
- POJ 2777 Count Color(线段树,区间更新,查询)
- Poj 2777 Count Color(线段树基础)
- [poj] 2777 - Count Color - 线段树 + 状态压缩思想(?)
- poj 2777 Count Color
- Count Color(poj 2777)
- POJ-2777Count Color 线段树+位移