求染色块

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

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>;
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
#define mod 1000000007
#define PI acos(-1)
using namespace std;
typedef long long ll ;
const int N = 100009;
int n , last;
int col[8010<<2] , ans[8010];

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


void update(int L , int R , int l , int r , int rt , int c)
{
    if(l >= L && r <= R)
    {
        col[rt] = c ;
        return ;
    }
    if(col[rt] == c) return ;
    pushdowm(rt);
    int mid = ( l + r ) >> 1 ;
    if(L <= mid)
        update(L , R , l , mid , rt*2 , c);
    if(mid < R)
        update(L , R , mid+1 , r , rt*2+1 , c);
}

void query(int l , int r , int rt)
{
    if(l == r)
    {
        if(col[rt] != -1 && col[rt] != last)//有颜色且与上一个颜色不同
        {
            ans[col[rt]]++ ;//该种颜色块加一
        }
        last = col[rt];
        return ;
    }
    pushdowm(rt);
    if(l == r) return ;
    int mid = ( l + r ) >> 1 ;
    query(l , mid , rt*2);
    query(mid+1 , r , rt*2+1);
}



int main()
{
    while(~scanf("%d" , &n))
    {
        memset(col , -1 , sizeof(col));
        for(int i = 0 ; i < n ; i++)
        {
            int x1 , x2 , c ;
            scanf("%d%d%d" , &x1 , &x2 ,&c);
            update(x1+1 , x2 , 1 , 8000 , 1 , c);
        }
        memset(ans , 0 , sizeof(ans));
        last = -1 ;
        query(1 , 8000 , 1);//暴力搜索每一叶子
        for(int i = 0 ; i <= 8000 ; i++)//暴力搜索每一种颜色
            if(ans[i])
                printf("%d %d
" , i , ans[i]);//输出颜色,及多少块
        cout << endl ;

    }

    return 0;
}


原文地址:https://www.cnblogs.com/nonames/p/11609257.html