zoj Count the Colors 1610 (数组模拟&&线段树)好题

来源:互联网 发布:阿里云域名解析 速度 编辑:程序博客网 时间:2024/05/22 10:29
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

//题意::在一条线段上涂颜色,先涂上的会被后涂上的遮盖(替换),//  每行有三个数x1,x2,c;表示从x1到x2上涂颜色c,最后输出//  颜色及此颜色的段数(连在一起的几个相同颜色的线段算一段) #include<stdio.h>#include<string.h>int s[10000],v[10000];int main(){int n,b,m;while(scanf("%d",&n)!=EOF){int a,b,c;memset(v,0,sizeof(v));memset(s,-1,sizeof(s));while(n--){scanf("%d%d%d",&a,&b,&c);for(int i=a;i<b;i++)s[i]=c;}for(int i=0;i<=8000;i++){if(s[i]!=s[i+1]&&s[i]!=-1)v[s[i]]++;}for(int i=0;i<=8000;i++)if(v[i]!=0){printf("%d %d",i,v[i]);printf("\n");}printf("\n");}return 0;}
 
<pre class="cpp" name="code">#include<stdio.h>#include<string.h>#include<algorithm>#define N 8010using namespace std;int tmp;int col[N];struct zz{int l;int r;int cc;}q[N<<2];void build(int gen,int l,int r){q[gen].l=l;q[gen].r=r;q[gen].cc=-1;//-1表示为空(没有颜色) if(l+1==r)return ;int mid=(l+r)/2;build(gen<<1,l,mid);build(gen<<1|1,mid,r);}void update(int gen,int l,int r,int val){if(l==r)return ;if(q[gen].cc==val)return ;if(q[gen].l>=l&&q[gen].r<=r){q[gen].cc=val;return ;}if(q[gen].cc>=0){q[gen<<1].cc=q[gen].cc;q[gen<<1|1].cc=q[gen].cc;q[gen].cc=-2;//-2表示有多种颜色 }int mid=(q[gen].l+q[gen].r)/2;if(r<=mid)update(gen<<1,l,r,val);else if(l>mid)update(gen<<1|1,l,r,val);else{update(gen<<1,l,mid,val);update(gen<<1|1,mid,r,val);}q[gen].cc=-2;}void query(int gen){if(q[gen].cc==-1){tmp=-1;return ;}if(q[gen].cc!=-2){if(q[gen].cc!=tmp){col[q[gen].cc]++;tmp=q[gen].cc;}return ;}if(q[gen].l+1!=q[gen].r){query(gen<<1);query(gen<<1|1);}}int main(){int n,x,y,c;int mm;while(scanf("%d",&n)!=EOF){build(1,0,8000);mm=0;while(n--){scanf("%d%d%d",&x,&y,&c);if(c>mm)mm=c;update(1,x,y,c);}tmp=-1;memset(col,0,sizeof(col));query(1);for(int i=0;i<=mm;i++){if(col[i]){printf("%d %d\n",i,col[i]);}}printf("\n");}return 0;}


0 0
原创粉丝点击