1. 程式人生 > 實用技巧 >Count the Colors

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\n",i,num[i]);
        }
        printf("\n");
    }
}
View Code