线段树染色

Language: Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 53531   Accepted: 16114

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

POJ Monthly--2006.03.26,dodo 解题思路:        利用sum[33]来记录颜色的有无,有为1,无为0;        用树的根节点来记录一段的颜色;
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn=100000+2;

struct segtree
{
	int l,r,co;
};
segtree tree[maxn*3+1];

int sum[40];//记录颜色是否存在;
int L,T,O,a,b,c,d,e;
char ch;

void build(int n,int l,int r)
{
	tree[n].l=l;
	tree[n].r=r;
	tree[n].co=1;//初始的颜色都是1;
	if(l==r)
	{
		return;
	}
	int mid=(tree[n].l+tree[n].r)/2;
	build(n*2,l,mid);
	build(n*2+1,mid+1,r);//用递归建树;
}

void updata(int n,int l,int r,int co)
{
	if(tree[n].l==l&&tree[n].r==r)
	{
		tree[n].co=co;
		return;
	}
	if(tree[n].co!=-1)
	{
		tree[n*2].co=tree[n*2+1].co=tree[n].co;
		tree[n].co=-1;
	}
	int mid=(tree[n].l+tree[n].r)/2;
	if(r<=mid)
	{
		updata(n*2,l,r,co);
	}
	else if(l>mid)
	{
		updata(n*2+1,l,r,co);
	}
	else
	{
		updata(n*2,l,mid,co);
		updata(n*2+1,mid+1,r,co);
	}
}

void query(int n,int l,int r)
{
	if(tree[n].co!=-1)
	{
		sum[tree[n].co]=1;
		return; 
	}
	int mid=(tree[n].l+tree[n].r)/2;
	if(r<=mid)
	{
		query(n*2,l,r);
	}
	else if(l>mid)
	{
		query(n*2+1,l,r);
	}
	else
	{
		query(n*2,l,mid);
		query(n*2+1,mid+1,r);
	}
}

int main()
{
        scanf("%d%d%d",&L,&T,&O);
		build(1,1,L);
		
	//	printf("%d\n",O);
		while(O--)
		{
			getchar();
			scanf("%c",&ch);
		    if(ch=='C')
		    {
			    scanf("%d%d%d",&a,&b,&c);
			    updata(1,a,b,c);
			    //printf("!!"); 
		    }
		    else if(ch=='P')
		    {
		    	scanf("%d%d",&d,&e);
	    		memset(sum,0,sizeof(sum));
	     		query(1,d,e);
		    	int ans=0;
		    	for(int i=1;i<=33;i++)
		        {
		 	    	if(sum[i]!=0)
			    	{
				    	ans++;
			    	}
		    	}
	     		printf("%d\n",ans);
	    	}
    	}
	return 0;
}

  

上一篇:Serializable


下一篇:安装es