ZOJ1610 线段树

Description

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长的线段,有n个染色操作,每次染[a,b]区间(染线段,不是染点),问最后没有被覆盖的颜色有多少,他们各有几个区间段。
代码:
//类似于贴海报的题,可以把每个染色区间左值+1,变成不连续,这样就可以看成点了。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=8003;
int val[maxn*4],num[maxn],n,last;
void Pushdown(int rt)
{
    if(val[rt]!=-1)
        val[rt<<1]=val[rt<<1|1]=val[rt];
    val[rt]=-1;
}
void Update(int ql,int qr,int c,int l,int r,int rt)
{
    if(ql<=l&&qr>=r){
        val[rt]=c;
        return;
    }
    Pushdown(rt);
    int m=(l+r)>>1;
    if(ql<=m) Update(ql,qr,c,l,m,rt<<1);
    if(qr>m) Update(ql,qr,c,m+1,r,rt<<1|1);
}
void Query(int l,int r,int rt)
{
    if(l==r){//算颜色段
        if(val[rt]!=-1&&val[rt]!=last)
            num[val[rt]]++;
        last=val[rt];
        return;
    }
    Pushdown(rt);
    int m=(l+r)>>1;
    Query(l,m,rt<<1);
    Query(m+1,r,rt<<1|1);
}
int main()
{
    while(scanf("%d",&n)==1){
        memset(val,-1,sizeof(val));
        memset(num,0,sizeof(num));
        int a,b,c;
        for(int i=0;i<n;i++){
            scanf("%d%d%d",&a,&b,&c);
            if(a<b) Update(a+1,b,c,1,8000,1);//判断!
        }
        last=-1;
        Query(1,8000,1);
        for(int i=0;i<=8000;i++){
            if(num[i]) printf("%d %d\n",i,num[i]);
        }
        printf("\n");
    }
    return 0;
}

 

posted @ 2017-03-23 20:59  luckilzy  阅读(1754)  评论(0编辑  收藏  举报