Count the Colors

来源:互联网 发布:dcs 化工仿真软件 编辑:程序博客网 时间:2024/05/16 14:40

线段树,对区间建树,维护区间颜色

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

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <map>#include <set>#include <stack>#include <cmath>#include <string>#include <iostream>#include <vector>#define LL long long#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn=1e5+10;const int SIZE=8888;int temp;int cnt[SIZE];struct seg{    int sum;}a[SIZE<<2];void pushup(int rt){    if(a[rt<<1].sum==a[rt<<1|1].sum)a[rt].sum=a[rt<<1].sum;    else a[rt].sum=-1;}void pushdown(int rt){    if(a[rt].sum>=0){        a[rt<<1].sum=a[rt<<1|1].sum=a[rt].sum;        a[rt].sum=-1;    }}void build(int l,int r,int rt){    a[rt].sum=-2;    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){        a[rt].sum=c;        return;    }    pushdown(rt);    int m=(l+r)>>1;    if(L<=m)update(L,R,c,lson);    if(R>m)update(L,R,c,rson);    pushup(rt);}void query(int l,int r,int rt){    if(a[rt].sum>=0){        if(temp==a[rt].sum)return;        temp=a[rt].sum;        cnt[a[rt].sum]++;        return;    }    if(a[rt].sum==-2){        temp=-1;        return;    }    int m=(l+r)>>1;    query(lson);    query(rson);}int main(){    int n,x,y,c;    while(scanf("%d",&n)!=EOF){        memset(cnt,0,sizeof(cnt));        build(0,SIZE,1);        for(int i=0;i<n;i++){            scanf("%d%d%d",&x,&y,&c);            update(x,y-1,c,0,SIZE,1);        }        temp=-1;        query(1,SIZE,1);        for(int i=0;i<SIZE;i++){            if(cnt[i]){                printf("%d %d\n",i,cnt[i]);            }        }        printf("\n");    }    return 0;}


0 0
原创粉丝点击