C语言线段树(5)____Count the Colors(zoj 1610)

来源:互联网 发布:雀神软件 编辑:程序博客网 时间:2024/06/12 18:06

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




题意:读入 a,b,c 表示用c颜色去涂 a,b区间.问最后表面色各个颜色各有几段.


注意:这里因为是区间不是 a~b的点.所以更新的时候更新 [a+1,b]即可


代码:

#include <cstdio>#include <cstring>#include <algorithm>#define MAX 8010using namespace std;struct node{int l,r,mid,data;};node tree[MAX<<2];int book[MAX];int ans[MAX];void CreatTree(int rt,int l,int r){tree[rt].l=l,tree[rt].r=r;tree[rt].mid=(l+r)>>1;tree[rt].data=-1;if(l==r){return;}CreatTree(rt<<1,l,tree[rt].mid);CreatTree(rt<<1|1,tree[rt].mid+1,r);}void change(int rt,int l,int r,int v){if(tree[rt].data==v)return;if(tree[rt].l==l && tree[rt].r==r){tree[rt].data=v;//printf("%d %d %d\n",l,r,v); return;}if(tree[rt].data!=-1){tree[rt<<1].data=tree[rt<<1|1].data=tree[rt].data;tree[rt].data=-1;}if(l>tree[rt].mid)change(rt<<1|1,l,r,v);else if(r<=tree[rt].mid)change(rt<<1,l,r,v);else{change(rt<<1,l,tree[rt].mid,v);change(rt<<1|1,tree[rt].mid+1,r,v);}}void Solve(int rt){if(tree[rt].l==tree[rt].r){ans[tree[rt].l]=tree[rt].data;return;}if(tree[rt].data!=-1){tree[rt<<1].data=tree[rt<<1|1].data=tree[rt].data;tree[rt].data=-1;}Solve(rt<<1);Solve(rt<<1|1);}int main(){int t,i,n,m,left,right,v,cas=0;while(scanf("%d",&n)!=EOF){CreatTree(1,1,8000);for(i=1;i<=n;i++){scanf("%d%d%d",&left,&right,&v);change(1,left+1,right,v);}memset(book,0,sizeof(book));Solve(1);for(i=1;i<=8000;i++){if(i==1 || ans[i]!=ans[i-1])book[ans[i]]++;}for(i=0;i<=8000;i++){if(book[i])printf("%d %d\n",i,book[i]); }printf("\n");}return 0;}


0 0