Count Color
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 39149 | Accepted: 11763 |
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
题目大意:c x y z,意思是从x到y的区间染成z,p x y,是查询x到y染了多少种染色,题目已经说明木板开始图的编号为1的颜色
注释的代码那种写法超时。。
ac代码
Source Code
Problem: 2777 User: kxh1995
Memory: 2204K Time: 266MS
Language: C++ Result: Accepted
Source Code
#include<stdio.h>
#include<string.h>
struct s
{
int col,is;
}node[100010<<2];
int sum=0;
void pushup(int tr)
{
node[tr].col=node[tr<<1].col|node[tr<<1|1].col;
}
void pushdown(int tr)
{
if(node[tr].is)
{
node[tr<<1].col=node[tr].col;
node[tr<<1|1].col=node[tr].col;
node[tr<<1].is=1;
node[tr<<1|1].is=1;
node[tr].is=0;
}
}
void build(int l,int r,int tr)
{
node[tr].col=1;
node[tr].is=1;
if(l==r)
return;
int mid=(l+r)>>1;
build(l,mid,tr<<1);
build(mid+1,r,tr<<1|1);
// pushup(tr);
}
void insert(int L,int R,int val,int l,int r,int tr)
{
if(L<=l&&r<=R)
{
node[tr].col=val;
node[tr].is=1;
return;
}
if(node[tr].col==val)//剪枝
return;
pushdown(tr);
int mid=(l+r)>>1;
/* if(L<=mid)
insert(L,R,val,l,mid,tr<<1);
if(R>mid)
insert(L,R,val,mid+1,r,tr<<1|1);*/
if(R<=mid)
insert(L,R,val,l,mid,tr<<1);
else
if(L>mid)
insert(L,R,val,mid+1,r,tr<<1|1);
else
{
insert(L,mid,val,l,mid,tr<<1);
insert(mid+1,R,val,mid+1,r,tr<<1|1);
}
pushup(tr);
}
void query(int L,int R,int l,int r,int tr)
{
if(L<=l&&R>=r)
{
sum|=node[tr].col;
return;
}
if(node[tr].is)//剪枝,区间都是一种颜色没必要继续往下找了
{
sum|=node[tr].col;
return;
}
int mid=(l+r)>>1;
/*if(L<=mid)
query(L,R,l,mid,tr<<1);
if(R>mid)
query(L,R,mid+1,r,tr<<1|1);*/
if(R<=mid)
query(L,R,l,mid,tr<<1);
else
if(L>mid)
query(L,R,mid+1,r,tr<<1|1);
else
{
query(L,mid,l,mid,tr<<1);
query(mid+1,R,mid+1,r,tr<<1|1);
}
}
int solve(int sum)
{
int ans=0;
while(sum)
{
if(sum&1)
ans++;
sum>>=1;
}
return ans;
}
int main()
{
int n,t,o;
while(scanf("%d%d%d",&n,&t,&o)!=EOF)
{
//sum=0;
build(1,n,1);
while(o--)
{
char s[3];
scanf("%s",s);
if(s[0]=='C')
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
int t=a;
a=b;
b=t;
}
insert(a,b,1<<(c-1),1,n,1);
}
else
{
int a,b;
sum=0;
scanf("%d%d",&a,&b);
if(a>b)
{
int t=a;
a=b;
b=t;
}
query(a,b,1,n,1);
//printf("++%d\n",sum);
printf("%d\n",solve(sum));
}
}
}
}