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
题意:
C操作就是给一排板子涂上一种颜色,P操作就是查询区间内不同颜色的数量。
题解:
由于颜色很少,只有30种,所以可以用二进制数模拟当前颜色数量的状态。
有意思的是,刚好卡在1000ms但是AC了。
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
const int maxn=1e5+5;
#define lson L,mid,rt<<1
#define rson mid+1,R,rt<<1|1
int seg[maxn<<2];
bool lazy[maxn<<2];
void build(int L,int R,int rt)
{
seg[rt]=1;
if(L==R)
return ;
int mid=(L+R)>>1;
build(lson);
build(rson);
}
void push_up(int rt)
{
seg[rt]=seg[rt<<1]|seg[rt<<1|1];
}
void push_down(int rt)//lazy操作
{
seg[rt<<1]=seg[rt<<1|1]=seg[rt];
lazy[rt<<1]=lazy[rt<<1|1]=true;
lazy[rt]=false;
}
void updata(int L,int R,int rt,int l,int r,int val)
{
if(L>=l&&R<=r)
{
seg[rt]=val;
lazy[rt]=true;
return ;
}
if(seg[rt]==val)//剪枝。已经是同种颜色,不需要再涂了
return ;
if(lazy[rt])
push_down(rt);
int mid=(L+R)>>1;
if(l>mid)
updata(rson,l,r,val);
else if(r<=mid)
updata(lson,l,r,val);
else
updata(lson,l,r,val),updata(rson,l,r,val);
push_up(rt);//更新
}
int sum;
void query(int L,int R,int rt,int l,int r)
{
if(L>=l&&R<=r)
{
sum|=seg[rt];
return ;
}
if(lazy[rt])//剪枝。当前区间被一种颜色所覆盖,不用再向下分解区间
{
sum|=seg[rt];
return ;
}
int mid=(L+R)>>1;
if(l>mid)
query(rson,l,r);
else if(r<=mid)
query(lson,l,r);
else
query(lson,l,r),query(rson,l,r);
}
int cal(int num)//计算有多少种颜色
{
int ans=0;
while(num)
{
if(num&1)
ans++;
num>>=1;
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
int n,t,q;
cin>>n>>t>>q;
build(1,n,1);
while(q--)
{
char op;
cin>>op;
int x,y,c;
if(x>y)
swap(x,y);
if(op=='C')
{
cin>>x>>y>>c;
updata(1,n,1,x,y,1<<(c-1));
}
else
{
cin>>x>>y;
sum=0;
query(1,n,1,x,y);
cout<<cal(sum)<<endl;
}
}
return 0;
}