ZOJ1610Count the Colors

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

区间染色

让你对0~8000的区间进行染色,颜色编号从0开始包括0,第一行输入n表示要进行染色的区间段数为n,接下来n每行输入

x , y , z表示区间x到y染成颜色z,然后让你输出0~8000区内的颜色及该被染成颜色的区间段数。。。

可以把每个染色区间左值+1,变成不连续,这样就可以看成点,即区间[0,1]被染成的染色让点1来代替

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#define N 8000*4+1
#define lson(x) (x<<1)
#define rson(x) ((x<<1)|1)
using namespace std;

struct node
{
    int l;
    int r;
    int color;//线段颜色兼懒惰标记
}tree[N];
int ans[N];//ans[i]=cnt:颜色为的i的线段(区间)为cnt段
int pre;//前一个点的颜色
void pushdown(int pos)
{
    tree[lson(pos)].color=tree[pos].color;
    tree[rson(pos)].color=tree[pos].color;
    tree[pos].color=-1;
}

void build(int pos,int ll,int rr)
{
    tree[pos].l=ll;
    tree[pos].r=rr;
    tree[pos].color=-1;//color同时为懒惰标记,因此赋值时要在这里
    if(ll==rr)
    {
        //tree[pos].color=-1;//这里,只对叶子结点进行赋值
        //color同时为懒惰标记,如果在这里进行赋值则非叶子结点的懒惰标记没有被赋值
        return ;
    }
    int mid=(ll+rr)>>1;
    build(lson(pos),ll,mid);
    build(rson(pos),mid+1,rr);
}

void update(int pos,int ll,int rr,int c)//区间更新
{
    if(ll<=tree[pos].l&&tree[pos].r<=rr)
    {
        tree[pos].color=c;
        return ;
    }
    if(tree[pos].color!=-1)
        pushdown(pos);
    int mid=(tree[pos].l+tree[pos].r)>>1;
    if(ll<=mid)
        update(lson(pos),ll,rr,c);
    if(rr>mid)
        update(rson(pos),ll,rr,c);
}

void query(int pos,int ll,int rr)//查询每个点的颜色
{
    if(ll==rr)
    {
        if(tree[pos].color!=-1&&tree[pos].color!=pre)//该点有颜色且不能与前一个点的颜色相同
            ans[tree[pos].color]++;//该颜色的段数加1
        pre=tree[pos].color;
        return;
    }
    if(tree[pos].color!=-1)
        pushdown(pos);
    int mid=(tree[pos].l+tree[pos].r)>>1;
    query(lson(pos),ll,mid);
    query(rson(pos),mid+1,rr);
}

int main()
{
    int n,x,y,c;
    //freopen("in.txt","r",stdin);
    while(~scanf("%d",&n))
    {
       build(1,1,8000);
       for(int i=0;i<n;i++)
       {
           scanf("%d%d%d",&x,&y,&c);
           update(1,x+1,y,c);//别忘了左端点加1
       }
       pre=-1;
       memset(ans,0,sizeof(ans));
       query(1,1,8000);//查询0~8000区间内每个点的颜色
       //因为0~8000连续区间被点1~8000代替,所以查询点1~8000的颜色
       for(int i=0;i<=8000;i++)
       {
           if(ans[i])
           {
               printf("%d %d\n",i,ans[i]);
           }
       }
       printf("\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值