码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 1610 Count the Colors

时间:2015-10-24 12:58:09      阅读:279      评论: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;
}


版权声明:来自: 代码代码猿猿AC路 http://blog.csdn.net/ck_boss

ZOJ 1610 Count the Colors

标签:

原文地址:http://www.cnblogs.com/gcczhongduan/p/4906456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!