Count Color
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 43947 | Accepted: 13311 |
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
给你n个点,每个点都以1颜色作为初始,刷新x-y区间里面的点为颜色z,询问区间内的不同颜色的总数,线段树的题,区间更新搞一下,然后就是要注意,怎么保存某一个区间的颜色数,因为颜色数是很少的只有30种,那么就可以在int 内用二进制来表示,某一位为1,表示在该区间里面存在该种颜色,区间合并就直接按位或' | '就可以合并两个区间了,注意x,y的大小有可能是反着的 做多了线段树这个坑点都已经快习惯了;
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define maxn 150005
struct node
{
int l;
int r;
int c;
int flag;
}tree[maxn*4];
int a[maxn*4],n;
void build(int id,int tl,int tr)
{
tree[id].l=tl;
tree[id].r=tr;
tree[id].flag=0;
if(tl==tr)
{
tree[id].c=1;
return;
}
int mid=(tl+tr)/2;
build(id*2,tl,mid);
build(id*2+1,mid+1,tr);
tree[id].c=tree[id*2].c|tree[id*2].c;
}
void pushdown(int id)
{
if(tree[id].flag!=0)
{
tree[id*2].c=tree[id].flag;
tree[id*2+1].c=tree[id].flag;
tree[id*2].flag=tree[id].flag;
tree[id*2+1].flag=tree[id].flag;
tree[id].flag=0;
}
}
void update(int id,int x,int y,int z)
{
int tl=tree[id].l;
int tr=tree[id].r;
int mid=(tl+tr)/2;
if(tl>y||tr<x)
return;
if(tl>=x&&tr<=y)
{
pushdown(id);
tree[id].flag=z;
tree[id].c=z;
return ;
}
pushdown(id);
update(id*2,x,y,z);
update(id*2+1,x,y,z);
tree[id].c=tree[id*2].c|tree[id*2+1].c;
}
int query(int id,int x,int y)
{
int tl=tree[id].l;
int tr=tree[id].r;
if(tl>y||tr<x)
return 0;
if(tl>=x&&tr<=y)
{
pushdown(id);
return tree[id].c;
}
pushdown(id);
return query(id*2,x,y)|query(id*2+1,x,y);
}
int solve(int ans)
{
int cnt=0;
while(ans)
{
cnt+=ans%2;
ans/=2;
}
return cnt;
}
int main()
{
int t,o=0,q,T;
char s[20];
while(scanf("%d%d%d",&n,&t,&q)!=EOF)
{
o++;
build(1,1,n);
while(q--)
{
int x,y,z;
scanf("%s",s);
if(s[0]=='C')
{
scanf("%d%d%d",&x,&y,&z);
if(x>y)
swap(x,y);
update(1,x,y,(1<<(z-1)));
}
else
{
scanf("%d%d",&x,&y);
if(x>y)
swap(x,y);
int ans=query(1,x,y);
int sum=solve(ans);
printf("%d\n",sum);
}
}
}
return 0;
}