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

POJ 2777 Count Color 线段树

2017年11月20日 ⁄ 综合 ⁄ 共 2578字 ⁄ 字号 评论关闭
Count Color
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 33932
Accepted: 10231

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

传送门:POJ 2777 Count Color

题目大意:
给你长度为L的区间,O次操作,以及T种颜色。 L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000).
有两个操作,每次选其中一个执行:
1.将一段区间覆盖成一种颜色。
2.询问一段区间内有多少种不同的颜色。

题目分析:不超过30种颜色,那么用二进制表示该区间是否存在该种颜色即可,简单hash+线段树。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define lson l , m , o << 1
#define rson m + 1 , r , o << 1 | 1

const int maxN = 1000005 ;

int hash[maxN] , mark[maxN] ;

void swap ( int &A , int &B ) {
	int tmp = A ; A = B ; B = tmp ;
}

void Build ( int l , int r , int o ) {
	hash[o] =  1 ;
	mark[o] = -1 ;
	if ( l == r ) return ;
	int m = ( l + r ) >> 1 ;
	Build ( lson ) ;
	Build ( rson ) ;
}

void PushUp ( int o ) {
	hash[o] = hash[o << 1] | hash[o << 1 | 1] ;
}

void PushDown ( int o ) {
	if ( ~mark[o] ) {
		mark[o << 1] = mark[o << 1 | 1] = mark[o] ;
		hash[o << 1] = hash[o << 1 | 1] = ( 1 << mark[o] ) ;
		mark[o] = -1 ;
	}
}

void Update ( int L , int R , int v , int l , int r , int o ) {
	if ( L <= l && r <= R ) {
		mark[o] = v ;
		hash[o] = ( 1 << v ) ;
		return ;
	}
	PushDown ( o ) ;
	int m = ( l + r ) >> 1 ;
	if ( L <= m ) Update ( L , R , v , lson ) ;
	if ( m <  R ) Update ( L , R , v , rson ) ;
	PushUp ( o ) ;
}

int Query ( int L , int R , int l , int r , int o ) {
	if ( L <= l && r <= R ) return hash[o] ;
	PushDown ( o ) ;
	int ans = 0 , m = ( l + r ) >> 1 ;
	if ( L <= m ) ans |= Query ( L , R , lson ) ;
	if ( m <  R ) ans |= Query ( L , R , rson ) ;
	return ans ;
}

void work () {
	int n , m , t , L , R , V ;
	char ch[5] ;
	while ( ~scanf ( "%d%d%d" , &n , &t , &m ) ) {
		Build ( 1 , n , 1 ) ;
		while ( m -- ) {
			scanf ( "%s%d%d" , ch , &L , &R ) ;
			if ( L > R ) swap ( L , R ) ;
			if ( ch[0] == 'C' ) {
				scanf ( "%d" , &V ) ;
				Update ( L , R , V - 1 , 1 , n , 1 ) ;
			}
			else {
				int ans = Query ( L , R , 1 , n , 1 ) , count = 0 ;
				for ( int i = t - 1 ; i >= 0 ; -- i ) {
					if ( ans & ( 1 << i ) ) ++ count ;
				}
				printf ( "%d\n" , count ) ;
			}
		}
	}
}

int main () {
	work () ;
	return 0 ;
}

抱歉!评论已关闭.