ZOJ

来源:互联网 发布:下载word2010软件 编辑:程序博客网 时间:2024/05/21 10:17

ZOJ - 1610 count the color

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,之前一直以为就是把a~b的所有点都染成c, 其实不是这样的,要染色的不是点,而是区间,例如要染[0,1],并不是把0,1两点染色,而是把[0,1]这一个单位区间进行染色。 假设有一个样例:

1  2  1

3  4  1

那么这个样例应该输出1  2. 只需要在纸上画一下就可以发现,区间【2,3】是没有被染色的,所以有两个间断的区间颜色是1

解决方法是在建树时,建立右子树递归时,不是传入的mid+1,而是mid,建树直到l比r小于1就结束了,在纸上画一画就知道为什么了

#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;const int MAXN=8005;struct NODE{int l,r;int color;}segTree[MAXN<<2];int color[MAXN],ans[MAXN];int cnt;void build(int num,int l,int r){segTree[num].l=l;segTree[num].r=r;segTree[num].color=-1;if(l+1==r) return;int mid=(l+r)>>1;build(num<<1,l,mid);build(num<<1|1,mid,r);}void pushdown(int num){if(segTree[num].color!=-1){segTree[num<<1].color=segTree[num<<1|1].color=segTree[num].color;segTree[num].color=-1;}}void update(int num,int l,int r,int c){if(l==r) return;if(segTree[num].color==c) return;if(segTree[num].l==l&&segTree[num].r==r){segTree[num].color=c;return;}    pushdown(num);int mid=(segTree[num].l+segTree[num].r)>>1;if(r<=mid) update(num<<1,l,r,c);else if(l>=mid) update(num<<1|1,l,r,c);else{update(num<<1,l,mid,c);update(num<<1|1,mid,r,c);}}void query(int num,int l,int r){if(l+1==r){   color[cnt++]=segTree[num].color;   return;    } pushdown(num);int mid=(l+r)>>1;query(num<<1,l,mid);    query(num<<1|1,mid,r);}int main(void){int n;int MAX;while(scanf("%d",&n)!=EOF){memset(ans,0,sizeof(ans));memset(color,-1,sizeof(color));    MAX=-1;build(1,0,8000);int x,y,c;while(n--){    scanf("%d%d%d",&x,&y,&c);    update(1,x,y,c);    MAX=max(c,MAX);    }    cnt=0;    query(1,0,8000);    if(color[0]!=-1) ans[color[0]]++;     for(int i=1;i<=8000;i++)       if(color[i]!=color[i-1]&&color[i]!=-1)          ans[color[i]]++;    for(int i=0;i<=MAX;i++)    if(ans[i])  printf("%d %d\n",i,ans[i]);printf("\n");            }    return 0;}

其实这个题还有一种解法,就是暴力
#include<stdio.h>#include<string.h>const int MAXN=8005;int main(void){int n;int color[MAXN],count[MAXN];while(scanf("%d",&n)!=EOF){memset(color,-1,sizeof(color));memset(count,0,sizeof(count));int start,end,c;for(int i=0;i<n;i++){scanf("%d%d%d",&start,&end,&c);for(int j=start;j<end;j++)        color[j]=c;}if(color[0]!=-1) count[color[0]]++;for(int i=1;i<=8000;i++)   if(color[i]!=-1&&color[i]!=color[i-1])      count[color[i]]++;for(int i=0;i<=8000;i++)   if(count[i])     printf("%d %d\n",i,count[i]);printf("\n");       }return 0;}

原创粉丝点击