您的位置:首页 > 编程语言 > C语言/C++

ZOJ 1610 Count the Colors 多次更新一次查询,只需要一一标记

2016-04-22 21:42 507 查看
Count the Colors
Crawling in process...
Crawling failed

Time Limit:2000MS     Memory Limit:65536KB    
64bit IO Format:%lld & %llu
    ZOJ
1610

Description
Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.

Your task is counting the segments of different colors you can see at last.

Input

The first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.

Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:

x1 x2 c

x1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment.

All the numbers are in the range [0, 8000], and they are all integers.

Input may contain several data set, process to the end of file.

Output

Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this color, they should be printed according to the color index.

If some color can't be seen, you shouldn't print it.

Print a blank line after every dataset.

Sample Input

5

0 4 4

0 3 1

3 4 2

0 2 2

0 2 3

4

0 1 1

3 4 1

1 3 2

1 3 1

6

0 1 0

1 2 1

2 3 1

1 2 0

2 3 0

1 2 1

Sample Output

1 1

2 1

3 1

1 1

0 2

1 1

给你代号为0-8000的8001种颜色,然后给n个操作,每个操作是一个区间加一种颜色(数字代号),然后把这个区间的所有颜色覆盖换成这种颜色,最后问你一共能看见多少种不同的颜色并从前往后出现了多少次,PS:从前往后出现了多少次是指不连续出现,比如1-10是01色 11-20是02色 21-25是01色 26-30是03色,则01色出现2次,02色出现1次,

03色出现一次,类似的,只不过原题需要求0-8000里面共有多少种颜色及出现多少次。

>>AC代码:

#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
int col[8100],cnt[8100];
struct xzs{
int col,l,r;
}tree[8100*4];
void build(int id,int l,int r){
tree[id].l=l;
tree[id].r=r;
tree[id].col=-1;
if(l==r)return;
int mid=(l+r)/2;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
}
void push_down(int id){
if(tree[id].col!=-1){
tree[id*2].col=tree[id*2+1].col=tree[id].col;
tree[id].col=-1;
}

}
void update(int id,int l,int r,int val){
if(tree[id].l>=l&&tree[id].r<=r){
tree[id].col=val;//
return;//注意,这个一定要加上
}
push_down(id);
int mid=(tree[id].l+tree[id].r)/2;
if(l>mid)update(id*2+1,l,r,val);
else if(r<=mid)update(id*2,l,r,val);
else{
update(id*2,l,mid,val);
update(id*2+1,mid+1,r,val);
}
}
void query(int id,int l,int r){
if(tree[id].l>=l&&tree[id].r<=r){
if(tree[id].col!=-1){
for(int i=tree[id].l;i<=tree[id].r;i++)
col[i]=tree[id].col;
return;//注意,这个一定要加上!!
}
}
push_down(id);
if(l==r)return;
int mid=(tree[id].l+tree[id].r)/2;
if(l>mid)query(id*2+1,l,r);
else if(r<=mid) query(id*2,l,r);
else{
query(id*2,l,mid);
query(id*2+1,mid+1,r);
}
}
int main(){
int n,x,y,c;
while(~scanf("%d",&n)){
memset(col,-1,sizeof(col));
memset(cnt,0,sizeof cnt);
build(1,1,8000);
for(int i=0;i<n;i++)
{scanf("%d%d%d",&x,&y,&c);
update(1,x+1,y,c);}
query(1,1,8000);
int pre=-1;//引入pre,为了防止是连续区间出现,也就是说连续的只能算出现一次。
for(int i=1;i<=8000;i++){
if(col[i]==-1){
pre=-1;
continue;}
else if(pre!=col[i]){
cnt[col[i]]++;
pre=col[i];
}
}
for(int i=0;i<=8000;i++){
if(cnt[i]){
cout<<i<<" "<<cnt[i]<<endl;
}
}
cout<<endl;//注意,每个case间会有空多一行
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM算法 C++ 线状树 zoj