Language:
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 |
有一排气球 一开始颜色都是1 有两种操作一种是把一个区间内的气球全都换成一种颜色 一种是求一个区间内的气球有多少种颜色的气球
难点在于如果表示一个区间内的气球颜色个数
在这里 因为总的气球颜色较少 可以用int整数来表示区间内气球染色的情况 换成2进制 如果改为为1 则这位上有这个颜色的气球 添加一种颜色的时候 使用位运算|
最后就可以求得这个区间内气球染色的情况 判断这个整数中共有几个1 就有几个颜色了
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 100010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int Read()
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
void Print(int a)
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}
struct node
{
int l,r;
int col,same;
};
node no[MAXN*4];
void build(int l,int r,int k)
{
no[k].l=l;
no[k].r=r;
no[k].col=1;
no[k].same=1;//用来标记这个区间里面的颜色是不是一样的
if(l==r)
return;
int mid=(l+r)/2;
build(l,mid,k*2);
build(mid+1,r,k*2+1);
}
void pushdown(int k)
{
if(no[k].same)
{
no[k*2].same=no[k*2+1].same=1;
no[k*2].col=no[k*2+1].col=no[k].col;
no[k].same=0;
return;
}
}
void update(int l,int r,int k,int col)
{
if(l<=no[k].l&&no[k].r<=r)
{
no[k].col=col;
no[k].same=1;
return;
}
pushdown(k);
int mid=(no[k].l+no[k].r)/2;
if(r<=mid) update(l,r,k*2,col);
else if(l>mid) update(l,r,k*2+1,col);
else
{
update(l,mid,k*2,col);
update(mid+1,r,k*2+1,col);
}
no[k].col=no[2*k].col|no[k*2+1].col;
}
int ans;
void query(int l,int r,int k)
{
if(no[k].l>=l&&no[k].r<=r)
{
ans|=no[k].col;
return;
}
pushdown(k);
int mid=(no[k].l+no[k].r)/2;
if(r<=mid) query(l,r,k*2);
else if(l>mid) query(l,r,k*2+1);
else
{
query(l,mid,k*2);
query(mid+1,r,k*2+1);
}
}
int getcnt()
{
int cnt=0;
while(ans)
{
if(ans&1)
cnt++;
ans>>=1;
}
return cnt;
}
int main()
{
// fread;
int n,t,q;
while(scanf("%d%d%d",&n,&t,&q)!=EOF)
{
build(1,n,1);
while(q--)
{
int l,r,num;
char ch[10];
scanf("%s",ch);
if(ch[0]=='C')
{
scanf("%d%d%d",&l,&r,&num);
if(l>r) swap(l,r);
update(l,r,1,1<<(num-1));
}
else
{
scanf("%d%d",&l,&r);
ans=0;
if(l>r) swap(l,r);
query(l,r,1);
printf("%d\n",getcnt());
}
}
}
return 0;
}