# Poj2777count color

Posted by yjjr's blog on February 6, 2018

POJ2777Count Color

http://www.sakurasake.icoc.me/nd.jsp?id=8#_np=2_327

 Time Limit: 1000MS Memory Limit: 65536K

Description

Chosen Problem Solving and Program designas 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 colorthe board - one segment with only one color. We can do following two operationson the board:

1. "C A B C" Color the board from segment A to segment B with colorC.
2. "P A B" Output the number of different colors painted betweensegment 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 Tis 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. Nowthe 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 Odenotes the number of operations. Following O lines, each contains "C A BC" or "P A B" (here A, B, C are integers, and A may be largerthan B) as an operation defined previously.

Output

Ouput results of the output operation inorder, 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

#include<iostream>
#include<cstdio>
#include<cstdlib>
#define maxn 100005
struct data
{
int a,b;
int color,lazy;
}tree[maxn*4];

int code(int x)
{
return 1<<(x-1);
}
int uncode(int x)
{
int t=0;
while(x>0)
{
if((x%2)==1)t++;
x=x/2;
}
return t;
}
void build(int p,int l,int r)
{
tree[p].a=l;tree[p].b=r;
tree[p].color=1;tree[p].lazy=0;
if(l==r)return;
build(p*2,l,(l+r)/2);
build(p*2+1,(l+r)/2+1,r);
}

void change(int p,int l,int r,int w)
{
if(tree[p].a==l&&tree[p].b==r)
{
tree[p].color=w;
if(l!=r)tree[p].lazy=1;
return;
}
else
{
if(tree[p].lazy==1)
{
tree[p*2].lazy=tree[p*2+1].lazy=1;
tree[p*2].color=tree[p*2+1].color=tree[p].color;
tree[p].lazy=0;
}
int mid=(tree[p].a+tree[p].b)/2;
if(r<=mid)change(p*2,l,r,w);
else if(l>mid)change(p*2+1,l,r,w);
else {change(p*2,l,mid,w);change(p*2+1,mid+1,r,w);}
}
tree[p].color=tree[p*2].color|tree[p*2+1].color;
}

int query(int p,int l,int r)
{
if((tree[p].a==l&&tree[p].b==r)||tree[p].lazy==1)return tree[p].color;
else
{
int mid=(tree[p].a+tree[p].b)/2;
if(r<=mid)query(p*2,l,r);
else if(l>mid)query(p*2+1,l,r);
else return query(p*2,l,mid)|query(p*2+1,mid+1,r);
}
}
void swap(int x,int y)
{
int t=x;
x=y;
y=t;
}
int main()
{
int i,L,m,t,ans;
char ch;
while(scanf("%d%d%d",&L,&t,&m)!=EOF)
{
build(1,1,L);
for(i=1;i<=m;i++)
{
scanf(" %c",&ch);
if(ch=='C')
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
c=code(c);
if(a>b)swap(a,b);
change(1,a,b,c);
}
else
{
int st,en;
scanf("%d%d",&st,&en);
if(st>en)swap(st,en);
ans=uncode(query(1,st,en));
printf("%d\n",ans);
}
}
}
return 0;
}