ZOJ 1610 Count the Colors

 图片

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610

仅仅只是一道线段树水题。。。。。
本来想根据先序遍历找线段个数。。。。但是最后越调越麻烦。。。。。。 
最后改成,整个扫一遍。。。。。。


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 <cstring>

using namespace std;

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

const int MAXN=8008;
int col[MAXN<<2],ak[8008],q[8008];
int mmmx=-1;
void pushDOWN(int rt)
{
    if(col[rt]!=-1)
    {
        col[rt<<1]=col[rt<<1|1]=col[rt];
        col[rt]=-1;
    }
}

void build(int l,int r,int rt)
{
    col[rt]=-1;
    if(l==r) return ;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}

void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        col[rt]=c;
        return;
    }
    int m=(l+r)>>1;
    if(col[rt]!=-1)
       pushDOWN(rt);
    if(L<=m) update(L,R,c,lson);
    if(R>m) update(L,R,c,rson);
    return ;
}

void query(int l,int r,int rt)
{
    if(col[rt]!=-1)
    {
        for(int i=l;i<=r;i++)
        {
            q=col[rt];
        }
        return ;
    }
    if(l!=r&&col[rt]==-1)
    {
        int m=(l+r)>>1;
        query(lson);
        query(rson);
    }
}

int main()
{

    int t;
while(scanf("%d",&t)!=EOF)
{
    memset(col,-1,sizeof(col));
    memset(q,-1,sizeof(q));
    memset(ak,0,sizeof(ak));
    int n=8008;
    build(1,n,1);
    int a,b,c;
for(int i=0;i<t;i++)
{
    scanf("%d%d%d",&a,&b,&c);
    update(a+1,b,c,1,n,1);
}

//show(1,n,1);
    query(1,n,1);

    for(int i=0;i<=8000;i++)
    {
        if(q[i+1]!=q&&q!=-1)
        {
            ak[q]++;
        }
    }

    for(int i=0;i<=8000;i++)
    {
        if(ak)
        {
            printf("%d %d\n",i,ak);
        }
    }
    putchar(10);
}
    return 0;
}
* This source code was highlighted by YcdoiT. ( style: Emacs )


友情提供测试数据:
2
0 1 2
3 4 2

4
789 864 521
727 742 8
100 8000 9
24 766 0
 
8
123 324 0
213 545 0
23 7884 0
2314 2939 888
623 3324 32
313 5445 2
233 7884 0
2314 2939 888 

答案:  2 2
            
             0 1 
             9 1

             0  2
             888  1 



posted @ 2013-08-14 20:40  码代码的猿猿  阅读(177)  评论(0编辑  收藏  举报