Count the Colors

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次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分?

思路:染色问题,区间修改,我没按照大神的思路直接用tree数组代用pushdown中的add数组。具体看代码就行了

#include <iostream>
#include <string.h>
#include <string>
#include<cstdio>
#include <algorithm>
#define ll long long
using namespace std;
ll tree[40001],a[40001],add[40001];
int num[80005],flage=-5;
void push_up(ll rt)
{
    tree[rt] = tree[rt*2+1] + tree[rt*2+2];
}
void push_down(ll rt)
{
    if(add[rt]!=-1)
    {
        add[rt*2]=add[rt]; //add更新,根据题目不同,更新的代码也不同,这里需要深入理解一下
        add[rt*2+1]=add[rt];
        tree[rt*2]=add[rt];
        tree[rt*2+1]= add[rt];
        add[rt] = -1;
    }
}
void bulid_tree(int node,int start,int end)
{
    if(start==end)
    {
        tree[node]=-1;
    }
    else
    {
        ll mid=(start+end)/2;
        ll left_node=2*node;
        ll right_node=2*node+1;
        bulid_tree(left_node,start,mid);
        bulid_tree(right_node,mid+1,end);
        tree[node]=tree[left_node]+tree[right_node];
    }
}
void  update_tree(int L, int R, ll key, int l, int r, int rt) //区间更新
{
    if(L <= l && R >= r)
    {
        tree[rt] = key;
        add[rt] = key;
        return;
    }
    push_down(rt);//向下更新
    ll mid=(l+r)/2;
    ll left_node=2*rt;
    ll right_node=2*rt+1;
    if(L <= mid)
        update_tree(L, R, key,l,mid,left_node);
    if(R > mid)
        update_tree(L, R, key,mid+1,r,right_node);
    // push_up(rt);//向上更新
}
void query_tree( int l, int r, int rt)
{
    if(l==r)
    {
        if(tree[rt]!=-1&&tree[rt]!=flage) //tree[rt]要不等于-1,并且不能和上一段的重复了,(要是这两段的颜色相同只能算一次),
        {
            num[tree[rt]]++;
        }
        flage=tree[rt];//标记上一段是什么颜色。
        return;
    }
    push_down(rt);
    ll mid=(l+r)/2;
    ll left_node=2*rt;
    ll right_node=2*rt+1;
    query_tree(l,mid,left_node);
    query_tree(mid+1,r,right_node);
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int l,r,c;//注意这次是从节点1开始的
        flage=-1;
        memset(tree,-1,sizeof(tree));//初始化
        memset(add,-1,sizeof(add));
        memset(num,0,sizeof(num));
        bulid_tree(1,1,8000);//建树
        for(int i=0; i<n; i++)
        {
            scanf("%d%d%d",&l,&r,&c);
            update_tree(l+1,r,c,1,8000,1);//左开右闭区间 l要+1.
        }
        query_tree(1,8000,1);//区间查询
        for(int i=0; i<=8001; i++)
        {
            if(num[i]!=0)
                printf("%d %d
",i,num[i]);
        }
        printf("
");
    }
}
View Code
原文地址:https://www.cnblogs.com/sszywq/p/13370182.html