您的位置:首页 > 其它

POJ 2777-Count Color-(线段树)

2017-09-28 10:22 330 查看
Problem 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

PKU

题意是给长为L的 区间,有o个操作,t种颜色,C(a,b,c)操作是给区间(a,b)涂上颜色c,P(a,b)操作时求出区间(a,b)中不同颜色的种类

题目维
c483
护一个线段树tree,tree[i]>0代表区间中有相同某个颜色,tree[i]<0表示区间中颜色不一致。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define M 100100
bool vis[35]; //记录要查询区间中出现的颜色
int cnt; //记录要查询区间中出现的颜色种类数
int tree[M<<2];
void build(int l,int r,int rt) //建树,初始值置为1
{
tree[rt]=1;
if(l==r) return;
int m=(l+r)>>1;
build(lson);
build(rson);
}
void pushup(int rt) //利用子节点更新父节点
{
if(tree[rt<<1]==-1||tree[rt<<1|1]==-1) //两个子节点中有一个是不同的颜色,则父节点中颜色也不一致
tree[rt]=-1;
else if(tree[rt<<1]==tree[rt<<1|1]) //两个子节点都是一致颜色的,且两个子节点颜色相同
tree[rt]=tree[rt<<1];
else tree[rt]=-1; //两个子节点都是一致颜色的,但两个子节点颜色不同
}
void pushdown(int rt) //如果父节点中颜色一致,则其子节点中颜色也一致
{
if(tree[rt]>0)
tree[rt<<1]=tree[rt<<1|1]=tree[rt];
}
void update(int ql,int qr,int c,int l,int r,int rt)
{
if(ql<=l&&qr>=r)//如果要更新区间包含当前区间,则更新当前区间
{
tree[rt]=c;
return ;
}
pushdown(rt); //如果要更新区间没有包含当前区间,则先pushdown子区间再更新子区间
int m=(l+r)>>1;
if(ql<=m) update(ql,qr,c,lson);
if(qr>m) update(ql,qr,c,rson);
pushup(rt); //更新完子区间后pushup父区间
}
void query(int ql,int qr,int l,int r,int rt)
{
if(ql<=l&&qr>=r) //如果要查询区间包含当前区间
{
if(tree[rt]>0) //如果当前区间颜色一致,并且是没有出现过的颜色,记录下来,并返回
{
if(vis[tree[rt]]==false)
{
cnt++;
vis[tree[rt]]=true;
}
return;
}
//注意如果单钱区间颜色不一致是不能返回的,因为此时要判断次区间到底包含多少颜色,也就是还得query它的子区间
}
pushdown(rt);
int m=(l+r)>>1;
if(ql<=m) query(ql,qr,lson);
if(qr>m) query(ql,qr,rson);
pushup(rt);
}
inline void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
int main()
{
int L,T,O,a,b,c;
char ch;
scanf("%d%d%d",&L,&T,&O);
build(1,L,1);
while(O--)
{
getchar();
ch=getchar();
if(ch=='C')
{
scanf("%d%d%d",&a,&b,&c);
if(a>b) swap(a,b);
update(a,b,c,1,L,1);
}
else if(ch=='P')
{
memset(vis,0,sizeof(vis));
cnt=0;
scanf("%d%d",&a,&b);
if(a>b) swap(a,b);
query(a,b,1,L,1);
printf("%d\n",cnt);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: