poj2777

Count Color

http://poj.org/problem?id=2777

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

更新时先将最底层的数据更新,然后再向上更新节点

#include 
#include 
#include 
#include 
#define maxn 100005
#define lson (root<<1)
#define rson (root<<1|1)
#define mid (l+r)>>1
using namespace std;
int n,m,tot;
char s[2];
int sum[maxn<<2],vis[maxn<<2];

void pushdown(int l,int r,int root)//向下更新
{
	if(vis[root]!=-1) {
		vis[lson]=vis[rson]=vis[root];
		sum[lson]=sum[rson]=(1<m) {
		update(m+1,r,rson,u,v,val);
	}else {
		update(l,m,lson,u,m,val);
		update(m+1,r,rson,m+1,v,val);
	}
	pushup(l,r,root);//向上更新
}
int query(int l,int r,int root,int u,int v)
{
	if(vis[root]!=-1){
		return (1<m) {
		res|=query(m+1,r,rson,u,v);
	}else {
		res|=query(l,m,lson,u,m);
		res|=query(m+1,r,rson,m+1,v);
	}
	return res;
}
int main()
{
	while(~scanf("%d%d%d",&n,&tot,&m))
	{
		memset(vis,-1,sizeof(vis));
        memset(sum,0,sizeof(sum));
        update(1,n,1,1,n,0);
        while(m--)
		{
        	int u,v,val;
        	scanf("%s%d%d",s,&u,&v);
        	u=min(u,v);
        	v=max(u,v);
        	if(s[0]=='C') {
        		scanf("%d",&val);
        		update(1,n,1,u,v,val-1);
			}else {
				int num=query(1,n,1,u,v),ans=0;
				while(num)
				{
					if(num&1) ans++;
					num>>=1;
				}
				printf("%d\n",ans);
			}
		}
	}
	return 0;
}

你可能感兴趣的:(POJ)