首页 > 其他 > 详细

ZOJ 1610 Count the Colors

时间:2014-07-18 23:05:38      阅读:532      评论:0      收藏:0      [点我收藏+]

线段树:延迟标记+暴力更新

记得刚学线段树的时候做这题WA了一版。。。。。现在分分钟搞定。。。。


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>
#include <algorithm>

using namespace std;

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

const int maxn=8200;

int color[maxn],cov[maxn<<2],ans[maxn];

void init()
{
    memset(color,-1,sizeof(color));
    memset(ans,0,sizeof(ans));
    memset(cov,-1,sizeof(cov));
}

void push_down(int l,int r,int rt)
{
    if(cov[rt]!=-1)
    {
        cov[rt<<1]=cov[rt<<1|1]=cov[rt];
        if(l==r)
        {
            color[l]=cov[rt];
        }
        cov[rt]=-1;
    }
}

void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        cov[rt]=c;
        return ;
    }
    push_down(l,r,rt);
    int m=(l+r)/2;
    if(L<=m) update(L,R,c,lson);
    if(R>m) update(L,R,c,rson);
}

void debug(int l,int r,int rt)
{
    cout<<rt<<": "<<l<<" <---> "<<r<<" color: "<<color[rt]<<"  cov: "<<cov[rt]<<endl;
    if(l==r) return ;
    int m=(l+r)/2;
    debug(lson); debug(rson);
}

void over_tree(int l,int r,int rt)
{
    push_down(l,r,rt);
    if(l==r) return ;
    int m=(l+r)/2;
    over_tree(lson); over_tree(rson);
}

int n;

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        init();
        while(n--)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            update(a,b-1,c,0,8100,1);
        }
        over_tree(0,8100,1);
        int last=-1;
        for(int i=0;i<8100;i++)
        {
            if(last==color[i]) continue;
            else
            {
                last=color[i];
                if(last!=-1)
                {
                    ans[last]++;
                }
            }
        }
        for(int i=0;i<=8000;i++)
        {
            if(ans[i])
            {
                printf("%d %d\n",i,ans[i]);
            }
        }
        putchar(10);
    }
    return 0;
}


ZOJ 1610 Count the Colors,布布扣,bubuko.com

ZOJ 1610 Count the Colors

原文:http://blog.csdn.net/ck_boss/article/details/37889489

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!