Count the Colors ZOJ - 1610 (线段树,区间覆盖求染色段个数)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhangxiaoduoduo/article/details/82973903

Count the Colors

 ZOJ - 1610 

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

题意:给出n次染色操作,输入a,b,c 即:把a-b染为颜色c。问:整个线段上的各个颜色的线段的个数

思路:线段树(觉得...比较难理解...),线段树真的是一个强大的算法,但是有些地方用起来还是比较难理解的...

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<utility>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#define maxn 8010
#define INF 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
#define E 1e-8
#define mod 1000000007
#define P pair<int,int>
#define MID(l,r) (l+(r-l)/2)
#define lson(o) (o<<1) //o*2
#define rson(o) (o<<1|1) //o*2+1
using namespace std;

struct node
{
    int l,r;
    int color;
}tree[maxn<<2];
int color[maxn];
int temp;
void build(int o,int l,int r)
{
    tree[o].l = l;
    tree[o].r = r;
    tree[o].color = -1; //开始没有颜色
    if(l+1 == r) {  // !!线段而不是点
        return ;
    }
    int m = MID(l,r);
    int lc = lson(o),rc = rson(o);
    build(lc,l,m);
    build(rc,m,r);
}
void update(int o,int l,int r,int c)
{
    if(l == r) return ;
    if(tree[o].color == c) return;
    if(l<=tree[o].l&&r>=tree[o].r){
        tree[o].color = c;
        return ;
    }
    int lc = lson(o),rc = rson(o);
    if(tree[o].color>=0){ //存在颜色,往下更新
        tree[lc].color = tree[o].color;
        tree[rc].color = tree[o].color;
        tree[o].color = -2; //表示有多种颜色
    }
    int m = MID(tree[o].l,tree[o].r);
    if(r<=m) update(lc,l,r,c);
    else if(l>=m) update(rc,l,r,c);
    else{
        update(lc,l,m,c);
        update(rc,m,r,c);
    }
    tree[o].color = -2;
}
void acount(int o)
{
    if(tree[o].color == -1){    //没有颜色
        temp = -1;
        return ;
    }
    if(tree[o].color != -2){  //只有一种颜色
        if(tree[o].color != temp){ //temp存的是前一段的颜色
            color[tree[o].color]++;
            temp = tree[o].color;
        }
        return;
    }
    if(tree[o].l+1 != tree[o].r){
        acount(o<<1);
        acount((o<<1)|1);
    }
}
int main()
{
    int n,a,b,c;
    int Max;
    while(scanf("%d",&n)!=EOF){
        build(1,0,8000);
        Max = 0;
        while(n--){
            scanf("%d %d %d",&a,&b,&c);
            update(1,a,b,c);
            if(c > Max) Max = c;
        }
        temp = -1;
        memset(color,0,sizeof(color));
        acount(1);
        for(int i=0;i<=Max;++i){
            if(color[i]) printf("%d %d\n",i,color[i]);
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhangxiaoduoduo/article/details/82973903