POJ2777 Count Color【线段树 区间覆盖】

Count Color

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

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 54255   Accepted: 16304

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

题意

一条很长(L)的画板,有T种颜色,O个操作;每次操作将一个区间刷成一种颜色,或者查询一个区间内所含的颜色数

思路

线段树,区间覆盖

C++代码

#include
#include

using namespace std;

#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1

const int N=100100;
int color[N<<2];
bool vis[35];
int ans;

void pushup(int rt)
{
	if(color[rt<<1]==-1||color[rt<<1|1]==-1||color[rt<<1]!=color[rt<<1|1])
	  color[rt]=-1;
	else 
	  color[rt]=color[rt<<1];
}

void build(int l,int r,int rt)
{
	if(l==r)
	{
		color[rt]=1;
		return;
	}
	int m=(l+r)>>1;
	build(ls);
	build(rs);
	pushup(rt);
}

void pushdown(int rt)
{
	if(color[rt]>0)
	{
		color[rt<<1]=color[rt<<1|1]=color[rt];
	}
}

void update(int L,int R,int C,int l,int r,int rt)
{
	if(L<=l&&r<=R)
	{
		color[rt]=C;
		return;
	}
	int m=(l+r)>>1;
	pushdown(rt);
	if(L<=m)
	  update(L,R,C,ls);
	if(R>m)
	  update(L,R,C,rs);
	pushup(rt);
}


void query(int L,int R,int l,int r,int rt)
{
	if(color[rt]>0)
	{
		if(vis[color[rt]]==false) ans++;
		vis[color[rt]]=true;
		return;
	} 
	if(l==r) return;
	int m=(l+r)>>1;
	pushdown(rt);
	if(L<=m)
	  query(L,R,ls);
	if(R>m)
	  query(L,R,rs);
}

int main()
{
    int n,t,q;
    while(~scanf("%d%d%d",&n,&t,&q))
    {
    	build(1,n,1);
        while(q--)
        {
            char op;
            int L,R,C;
            scanf(" %c",&op);
            if(op=='C')
            { 
                scanf("%d%d%d",&L,&R,&C);
                update(L,R,C,1,n,1);
            }
            else
            {
                scanf("%d%d",&L,&R);
                memset(vis,0,sizeof(vis));
                ans=0;
                query(L,R,1,n,1);
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}

 

你可能感兴趣的:(数据结构)