【区间覆盖+输出方案】F - Count the Colors ZOJ - 1610

 

F - 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

//给你8000米的黑板,让你刷颜色,颜色是0-8000各种
//问你最后可见的颜色有几块不连续的
#include <bits/stdc++.h>
using namespace std;
const int maxn=8005;
int col[maxn*4],num[maxn];
int n,x,y,z,pre_color;

void pushdown(int rt)
{
    if(col[rt]!=-1)
    {
        col[2*rt]=col[2*rt+1]=col[rt];
        col[rt]=-1;
    }
}

void update(int L,int R,int w,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        col[rt]=w;
        return;
    }
    pushdown(rt);
    int mid=(l+r)/2;
    if(L<=mid) update(L,R,w,l,mid,2*rt);  //!!这里的L,R老师搞成l,r,LR是我要更新的区间
    if(R>mid)  update(L,R,w,mid+1,r,2*rt+1);
}

void query(int l,int r,int rt)
{
    if(l==r)
    {
        if(col[rt]!=-1&&col[rt]!=pre_color)  //有颜色而且与之前的颜色不同
            num[col[rt]]++;
        pre_color=col[rt];    //更新前一个的颜色
        return;     //!!!注意不要忘记return
    }
    pushdown(rt);
    int mid=(l+r)/2;
    query(l,mid,2*rt);
    query(mid+1,r,2*rt+1);
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        pre_color=-1;   //初始化pre_color
        memset(col,-1,sizeof(col));
        memset(num,0,sizeof(num));
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            update(x+1,y,z,1,8000,1); //!!!!因为是区间覆盖所以从后一个找
        }
        query(1,8000,1);
        for(int i=0; i<=8000; i++)
        {
            if(num[i]) printf("%d %d\n",i,num[i]);  //颜色是在0-8000这些
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41037114/article/details/81187004
今日推荐