现在的位置: 首页 > 综合 > 正文

poj2777Count Color(线段树,段更新,段查询用map函数)

2018年02月22日 ⁄ 综合 ⁄ 共 2122字 ⁄ 字号 评论关闭

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<stdio.h>
#include<map>
#include<iostream>
using namespace std;
#define N 100000
struct node
{
	int type,b;
}tree[4*N];
void biulde(int l,int r,int k)
{
	int m=(l+r)/2;
	tree[k].b=1; tree[k].type=1;
	if(l==r) return ;
	biulde(l,m,k*2); biulde(m+1,r,k*2+1);
}
int L,R,C;
void updataColor(int l,int r,int k)
{
	int m=(l+r)/2;
	if(tree[k].b&&tree[k].type==C) return ;
	if(L<=l&&r<=R){
		tree[k].type=C; tree[k].b=1;
		return ;
	}
	if(tree[k].b){
		tree[k*2].b=tree[k*2+1].b=1;
		tree[k*2].type=tree[k*2+1].type=tree[k].type;
	}
	if(L<=m) updataColor(l,m,k*2);
	if(R>m) updataColor(m+1,r,k*2+1);
		tree[k].b=0;
}
int typesum;
map<int,int>types;
void count_typesum(int l,int r,int k)
{
	int m=(l+r)/2;
	if(tree[k].b){
		if(types[tree[k].type]==0)  typesum++; 
			types[tree[k].type]=1;	return ;
	}
	if(L<=m) count_typesum(l,m,k*2);
	if(R>m) count_typesum(m+1,r,k*2+1);
}
int main()
{
	int m,n,T;
	char c[2];
	scanf("%d%d%d",&n,&T,&m);
	biulde(1,n,1);
	while(m--)
	{
		scanf("%s%d%d",c,&L,&R);
		if(c[0]=='C'){
			scanf("%d",&C);
			updataColor(1,n,1);
		}
		else{
			types.clear(); typesum=0;
			count_typesum(1,n,1);
			printf("%d\n",typesum);
		}
	}
}

抱歉!评论已关闭.