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: 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 |
给你木板的长度n,(是初始颜色为1)然后给木板涂色,c表示涂色,q表示询问从a 到b有多少不同的颜色,用位运算保存颜色的种类,
直接上代码了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
#define N 100055
struct stud{
int le,ri;
int cover;
int color;
}f[N*4];
int sum;
int fdd()
{
int x=0;
while(sum)
{
if(sum%2)
x++;
sum>>=1;
}
return x;
}
void build(int pos,int le,int ri)
{
f[pos].le=le;
f[pos].ri=ri;
f[pos].color=1;
f[pos].cover=1;
if(le==ri) return ;
int mid=MID(le,ri);
build(L(pos),le,mid);
build(R(pos),mid+1,ri);
}
void pushdown(int pos)
{
if(f[pos].cover)
{
f[L(pos)].color=f[R(pos)].color=f[pos].color;
f[L(pos)].cover=f[R(pos)].cover=1;
f[pos].cover=0;
}
}
void update(int pos,int le,int ri,int va)
{
if(f[pos].le==le&&f[pos].ri==ri)
{
f[pos].color=va;
f[pos].cover=1;
return ;
}
pushdown(pos);
int mid=MID(f[pos].le,f[pos].ri);
if(mid>=ri)
update(L(pos),le,ri,va);
else
if(mid<le)
update(R(pos),le,ri,va);
else
{
update(L(pos),le,mid,va);
update(R(pos),mid+1,ri,va);
}
f[pos].color=f[L(pos)].color|f[R(pos)].color;
}
void query(int pos,int le,int ri)
{
if(f[pos].le==le&&f[pos].ri==ri)
{
sum|=f[pos].color;
return ;
}
pushdown(pos);
int mid=MID(f[pos].le,f[pos].ri);
if(mid>=ri)
query(L(pos),le,ri);
else
if(mid<le)
query(R(pos),le,ri);
else
{
query(L(pos),le,mid);
query(R(pos),mid+1,ri);
}
}
int main()
{
int n,i,m,t;
while(~scanf("%d%d%d",&n,&t,&m))
{
build(1,1,n);
char c[10];
int le,ri,va,temp;
while(m--)
{
scanf("%s",c);
if(c[0]=='P')
{
sum=0;
scanf("%d%d",&le,&ri);
if(le>ri){temp=le; le=ri; ri=temp;}
query(1,le,ri);
printf("%d\n",fdd());
}
else
{
scanf("%d%d%d",&le,&ri,&va);
if(le>ri){temp=le; le=ri; ri=temp;}
update(1,le,ri,1<<(va-1));
}
}
}
return 0;
}