您的位置:首页 > 其它

POJ 2777 Count Color

2016-03-03 15:04 274 查看
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<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
struct wakaka{
int l,r,num;
}tree[5000000];
bool b[31],bo;
int n,m,x,ll,rr,v,ans,t;
char ch;
int maxx(int a,int b){
if (a>b)
return a;
else
return b;
}
void build(int l,int r,int i){
tree[i].l=l;
tree[i].r=r;
tree[i].num=1;
if (l!=r){
int mid=(l+r)>>1;
build(l,mid,i*2);
build(mid+1,r,i*2+1);
}
}
void change(int i,int l,int r,int v){

if(tree[i].num==v)
return ;
int ll=tree[i].l;
int rr=tree[i].r;
if (ll==l&&rr==r){
tree[i].num=v;
return ;
}
if (tree[i].num!=-1)
{
tree[i*2].num=tree[i*2+1].num=tree[i].num;
tree[i].num=-1;
}

int mid=(ll+rr)>>1;
if (l>mid)
change(2*i+1,l,r,v);
else if (mid>=r)
change(2*i,l,r,v);
else {
change(2*i,l,mid,v);
change(2*i+1,mid+1,r,v);
}
}
void search1(int i,int l,int r)
{
if (tree[i].num!=-1)
{
b[tree[i].num]=1;
return ;
}
int mid=tree[i].l+tree[i].r;
mid/=2;
if (l>mid)
search1(i*2+1,l,r);
else if (r<=mid)
search1(i*2,l,r);
else{
search1(i*2,l,mid);
search1(i*2+1,mid+1,r);
}
}

int main(){
bo=0;
scanf("%d %d %d",&n,&t,&m);
build(1,n,1);
for (int i=1;i<=t;++i){
b[i]=0;
}
for (int i=1;i<=m;++i){
scanf("%c",&ch);
while(ch!='C'&&ch!='P'){
scanf("%c",&ch);
}
if (ch=='C'){
scanf("%d %d %d",&ll,&rr,&x);
change(1,min(ll,rr),max(ll,rr),x);
}
else {
ans=0;
scanf("%d %d",&ll,&rr);
search1(1,min(ll,rr),max(ll,rr));
for (int j=1;j<=t;++j)
{
if (b[j]==1)
ans++;
b[j]=0;
}
printf("%d\n",ans);
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: