ZOJ 题目1610 Count the Colors(线段树求颜色及有多少段)

来源:互联网 发布:数据人生完本 编辑:程序博客网 时间:2024/05/10 23:58
Count the Colors

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

水题

ac代码

#include<stdio.h>#include<string.h>int node[8080<<2],flag[8080],ans[8080];void build(int l,int r,int tr){node[tr]=-1;if(l==r)return;int mid=(l+r)>>1;build(l,mid,tr<<1);build(mid+1,r,tr<<1|1);}/*void insert(int L,int R,int l,int r,int c,int tr){if(L<=l&&R>=r){node[tr]=c;return;}if(node[tr]!=-1){node[tr<<1]=node[tr<<1|1]=node[tr];node[tr]=-1;}int mid=(l+r)>>1;if(L>mid){insert(L,R,mid+1,r,c,tr<<1|1);}elseif(R<=mid){insert(L,R,l,mid,c,tr<<1);}else{insert(L,R,l,mid,c,tr<<1);insert(L,R,mid+1,r,c,tr<<1|1);}if(node[tr<<1]!=-1&&node[tr<<1]==node[tr<<1|1])node[tr]=node[tr<<1];}*/void insert(int L,int R,int l,int r,int c,int tr)  {      if(L<=l&&r<=R)      {          node[tr]=c;          return;      }      if(node[tr]!=-1)      {          node[tr<<1]=node[tr<<1|1]=node[tr];          node[tr]=-1;      }      int mid=(l+r)>>1;      if(L<=mid)          insert(L,R,l,mid,c,tr<<1);      if(R>mid)          insert(L,R,mid+1,r,c,tr<<1|1);if(node[tr<<1]!=-1&&node[tr<<1]==node[tr<<1|1])node[tr]=node[tr<<1];}void count(int l,int r,int tr){if(node[tr]>=0){for(int i=l;i<=r;i++){flag[i]=node[tr];}return;}if(l==r)return;int mid=(l+r)>>1;count(l,mid,tr<<1);count(mid+1,r,tr<<1|1);}int main(){int n;while(scanf("%d",&n)!=EOF){build(1,8010,1);int i,maxn=-1;memset(ans,0,sizeof(ans));memset(flag,-1,sizeof(flag));for(i=0;i<n;i++){int a,b,c;scanf("%d%d%d",&a,&b,&c);if(a>=b)continue;if(c>maxn)maxn=c;insert(a+1,b,1,8010,c,1);}count(1,8010,1);for(i=0;i<8010;i++){if(flag[i]!=-1&&flag[i+1]!=flag[i]){ans[flag[i]]++;//printf("%d %d %d\n",i,flag[i],ans[flag[i]]);}}for(i=0;i<=maxn;i++){if(ans[i])printf("%d %d\n",i,ans[i]);}printf("\n");}}


0 0
原创粉丝点击