ZOJ1610 Count the Colors(计数能看到的不同颜色段及段数)

题目链接

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=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

题意

给定m个区间和要染成的颜色,问染完后能看到的每个颜色的段数。

分析

线段树区间修改,最后暴力统计每个叶子结点的颜色。因为染色的特殊性,需要将区间与线段树的对应进行特殊处理。比如[1,4]染1,[1,2]染2,[3,4]染3.正确答案是有1,2,3三种颜色段。但是如果将区间与线段树维护的区间直接对应的话,答案是2,3两种颜色段。即区间[2,3]的颜色因为叶子结点2、3被染成其他颜色而计数不出来。
故将区间[l,r]对应到线段树中的区间[2l,2r]。

AC代码

//60ms 13.6MB
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
#define lson p<<1
#define rson p<<1|1
using namespace std;

const int maxn=2e5+100;
struct node
{
    int l,r;//结点所维护的区间
    int mark;//延迟标记
} T[maxn<<2]; //线段树要开四倍空间
int x[maxn],y[maxn],a[maxn];
int vis[maxn];//叶子染色的颜色

void down(int p)
{
    if(T[p].mark==-1)
        return ;
    T[lson].mark=T[rson].mark=T[p].mark;
    T[p].mark=-1;
}
void build(int p,int l,int r)//p表示结点编号,l、r表示结点p所表示的区间
{
    T[p].mark=-1;
    T[p].l=l,T[p].r=r;//确定结点p所表示的区间[l,r]
    if(l==r) //确定叶子结点所表示的信息
    {
        return ;
    }
    int mid=(l+r)>>1;
    build(lson,l,mid); //递归创建左子树
    build(rson,mid+1,r); //递归创建右子树
}
void update(int p,int x,int y,int v)
{
    if(T[p].l==x && T[p].r==y) //区间恰好重合
    {
        T[p].mark=v; //标记信息修改到此结点打止
        return ;
    }
    //区间在p的子树结点中,要调用子树结点信息,必须先下传延迟标记
    down(p); //下传标记将p的左右子结点修改,只修改了这两个结点,延迟标记放在了这两个结点上
    int mid=(T[p].l+T[p].r)>>1;
    if(y<=mid) //区间[x,y]一定在左子树上
        update(lson,x,y,v);
    else if(x>mid) //区间[x,y]一定在右子树上
        update(rson,x,y,v);
    else
    {
        //必须将区间分成两份小区间[x,mid]和[mid+1,y]
        update(lson,x,mid,v);
        update(rson,mid+1,y,v);
    }
}
void query(int p)
{
    if(T[p].l==T[p].r)
    {
        vis[T[p].l]=T[p].mark;
        return ;
    }
    down(p);
    query(lson);
    query(rson);
}
int ans[maxn];
int main()
{
    int m,n,tot;
    while(~scanf("%d",&m))
    {
        n=16000;
        tot=0;
        memset(vis,-1,sizeof(vis));
        build(1,0,n);
        for(int i=1; i<=m; i++)
        {
            int l,r,c;
            scanf("%d%d%d",&l,&r,&c);
            l*=2;
            r*=2;
            update(1,l,r,c);
        }
        query(1);//将延迟标记传递到每个叶子结点
        memset(ans,0,sizeof(ans));
        int pre=-1;//上一个颜色段的颜色
        for(int i=0; i<=n; i++)
        {
            if(vis[i]==pre) continue;
            pre=vis[i];
            if(vis[i]==-1) continue;
            ans[pre]++;
        }
        for(int i=0;i<=n;i++)
            if(ans[i])
            printf("%d %d\n",i,ans[i]);
        putchar(10);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37685156/article/details/80512086