Count Color
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 34022 | Accepted: 10254 |
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:
1. "C A B C" Color the board from segment A to segment B with color C.
2. "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.
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:
1. "C A B C" Color the board from segment A to segment B with color C.
2. "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
Source
POJ Monthly--2006.03.26,dodo
线段树区间更新,要运用到位运算。。。
线段树区间更新,要运用到位运算。。。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=100010;
struct seg
{
int l,r;
int sum; //用一32位的整型变量对应的二进制的位数代表有几种颜色,如1代表有一种,11有两种
bool cover; //区间是否是只有一种颜色
}tree[MAX*3];
void pushdown(int k) //延迟,向下传递,大区间都只有一种颜色,由他分割出来的小区间肯定只有一种颜色
{
tree[2*k].sum=tree[2*k+1].sum=tree[k].sum;
tree[2*k].cover=1;
tree[2*k+1].cover=1;
tree[k].cover=0;
}
void build(int l,int r,int k)
{
tree[k].l=l;
tree[k].r=r;
tree[k].cover=1; //初始区间为单一颜色
tree[k].sum=1; //初始颜色为一种
if(l==r)
{
return;
}
int mid=(l+r)>>1;
build(l,mid,2*k);
build(mid+1,r,2*k+1);
}
void insert(int l,int r,int k,int val)
{
if(tree[k].l>=l&&tree[k].r<=r) //修改区间包含此节点对应的区间
{
tree[k].sum=val;
tree[k].cover=1;
return;
}
if(tree[k].sum==val) //相同的颜色,不需做修改了
return;
if(tree[k].cover) //需要更新子节点
pushdown(k);
int mid=(tree[k].l+tree[k].r)>>1;
if(r<=mid)
insert(l,r,2*k,val);
else if(l>mid)
insert(l,r,2*k+1,val);
else
{
insert(l,mid,2*k,val);
insert(mid+1,r,2*k+1,val);
}
tree[k].sum=tree[2*k].sum | tree[2*k+1].sum; //这句就是叠加颜色的几种,以二进制位数代表颜色,用或运算刚好叠加
}
int sum;
void query(int l,int r,int k)
{
if(tree[k].l>=l&&tree[k].r<=r)
{
sum|=tree[k].sum;
return;
}
if(tree[k].cover)
{
sum|=tree[k].sum;
return;
}
int mid=(tree[k].l+tree[k].r)>>1;
if(r<=mid)
query(l,r,2*k);
else if(l>mid)
query(l,r,2*k+1);
else{
query(l,mid,2*k);
query(mid+1,r,2*k+1);
}
}
int main()
{
int n,t,m,x,a,y,temp;
char s[2];
//freopen("in.txt","r",stdin);
while(scanf("%d%d%d",&n,&t,&m)==3)
{
build(1,n,1);
while(m--)
{
scanf("%s",s);
if(s[0]=='C')
{
scanf("%d%d%d",&x,&y,&a);
if(x>y)
temp=x,x=y,y=temp;
insert(x,y,1,1<<(a-1));
}
else
{
scanf("%d%d",&x,&y);
if(x>y)
temp=x,x=y,y=temp;
sum=0;
query(x,y,1);
int ans=0;
while(sum) //求出颜色的几种,位数上1代表有1种颜色
{
if(sum&1)
ans++;
sum>>=1;
}
printf("%d\n",ans);
}
}
}
return 0;
}