ZOJ1610-Count the Colors

来源:互联网 发布:梦里花落知多少的含义 编辑:程序博客网 时间:2024/05/17 12:50

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



Author: Standlove
Source: ZOJ Monthly, May 2003


题意:在一条线上画出一些有颜色的线条,先画出来的可能会被后画出来的给覆盖掉。现在给你一些线条的区间和颜色,按颜色值从小到大输出能看见的线条的颜色以及条数,不能看见的不输出。

解题思路:动态建点


#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;const int MAXN=200000;int tot,L[MAXN],R[MAXN],f[MAXN],cnt[9000],ls[MAXN],rs[MAXN];int node(){    tot++;    L[tot]=R[tot]=0;    ls[tot]=rs[tot]=f[tot]=-1;    return tot;}void add(int k,int l,int r,int ll,int rr,int v){    if(l>=ll&&r<=rr)    {        f[k]=ls[k]=rs[k]=v;        return ;    }    int mid=(l+r)>>1;    if(f[k]!=-1)    {        if(!L[k]) L[k]=node();        if(!R[k]) R[k]=node();        f[L[k]]=f[R[k]]=f[k];        ls[L[k]]=rs[L[k]]=ls[R[k]]=rs[R[k]]=f[k];        f[k]=-1;    }    if(ll<=mid)    {        if(!L[k]) L[k]=node();        add(L[k],l,mid,ll,rr,v);    }    if(rr>mid)    {        if(!R[k]) R[k]=node();        add(R[k],mid+1,r,ll,rr,v);    }    if(L[k]) ls[k]=ls[L[k]];    if(R[k]) rs[k]=rs[R[k]];}void Find(int k,int l,int r){    if(f[k]!=-1) {cnt[f[k]]++;return ;}    int mid=(l+r)>>1;    if(L[k]) Find(L[k],l,mid);    if(R[k]) Find(R[k],mid+1,r);    if(L[k]&&R[k]&&ls[R[k]]==rs[L[k]]&&ls[R[k]]!=-1) cnt[ls[R[k]]]--;}int main(){    int n;    while(~scanf("%d",&n))    {        int x,y,c;        L[1]=R[1]=0;        tot=1;        ls[1]=rs[1]=f[1]=-1;        memset(cnt,0,sizeof cnt);        for(int i=1;i<=n;i++)        {            scanf("%d %d %d",&x,&y,&c);            add(1,0,8000,x,y-1,c);        }        Find(1,0,8000);        for(int i=0;i<=8000;i++)            if(cnt[i]) printf("%d %d\n",i,cnt[i]);        printf("\n");    }    return 0;}

0 0
原创粉丝点击