首页 > 其他 > 详细

POJ 2777 Count Color(线段树 )

时间:2014-10-24 16:42:13      阅读:263      评论:0      收藏:0      [点我收藏+]

Language:
Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 36180   Accepted: 10927

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




题意:给你n个板子,num个颜色,m个操作,c代表把 le 到  ri ,涂成va颜色,p代表询问le到ri 有几种颜色


线段树+lazy思想,颜色用二进制保存, 注意 |  运算的应用


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
using namespace std;

#define N  100005

int n,m;
int sum;

struct stud{
int le,ri;
int color;
int cover;
}f[N*4];

void pushdown(int pos)
{
	if(!f[pos].cover) return ;

	f[L(pos)].color=f[R(pos)].color=f[pos].color;
	f[L(pos)].cover=f[R(pos)].cover=1;
	f[pos].cover=0;
}

void pushup(int pos)
{
	f[pos].color=f[L(pos)].color|f[R(pos)].color;
}

void build(int pos,int le,int ri)
{
	f[pos].le=le;
	f[pos].ri=ri;
	f[pos].color=1<<1;
	f[pos].cover=1;
	if(le==ri) return ;

	int mid=MID(le,ri);
	build(L(pos),le,mid);
	build(R(pos),mid+1,ri);
}

void update(int pos,int le,int ri,int va)
{
	if(f[pos].le==le&&f[pos].ri==ri)
	{
		f[pos].color=va;
		f[pos].cover=1;
		return ;
	}
    pushdown(pos);

    int  mid=MID(f[pos].le,f[pos].ri);

    if(mid>=ri)
		update(L(pos),le,ri,va);
	else
		if(mid<le)
		update(R(pos),le,ri,va);
	else
	{
		update(L(pos),le,mid,va);
		update(R(pos),mid+1,ri,va);
	}
    pushup(pos);
}

void query(int pos,int le,int ri)
{
	if(f[pos].le==le&&f[pos].ri==ri)
	{
		sum|=f[pos].color;
		return ;
	}

    pushdown(pos);

    int mid=MID(f[pos].le,f[pos].ri);

    if(mid>=ri)
		query(L(pos),le,ri);
	else
		if(mid<le)
		query(R(pos),le,ri);
	else
	{
		query(L(pos),le,mid);
		query(R(pos),mid+1,ri);
	}
   pushup(pos); //这个题收获之处  不pushup也对
}

void print()
{
	int ans=0;
	for(int i=1;i<31;i++)
		if(sum&(1<<i))
		ans++;
	printf("%d\n",ans);

}

int main()
{
    int i,j,num;
	while(~scanf("%d%d%d",&n,&num,&m))
	{
		build(1,1,n);
		char c[10];
		int le,ri,va;
		while(m--)
		{
			scanf("%s",c);
			if(c[0]=='C')
			{
				scanf("%d%d%d",&le,&ri,&va);
				update(1,le,ri,1<<va);
			}
            else
			{
				sum=0;
				scanf("%d%d",&le,&ri);
				if(le>ri) {i=le;le=ri;ri=i;}
				query(1,le,ri);
				print();
			}
		}
	}
   return 0;
}








POJ 2777 Count Color(线段树 )

原文:http://blog.csdn.net/u014737310/article/details/40427129

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!