ZOJ - 1610 Count the Colors (線段樹區間染色)

Count the Colors

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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



Author: Standlove

Source: ZOJ Monthly, May 2003


#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;
int n;
int ans[N], aa[N];
struct xx{
    int l, r, v;
} T[N<<2], a[N];

void Pushdown(int k){
    if(T[k].v != -1){
        T[k<<1].v = T[k<<1|1].v = T[k].v;
        T[k].v = -1;
    }
}

void Pushup(int k){
    if(T[k<<1].v == T[k<<1|1].v){
        T[k].v = T[k<<1].v;
    }
}

void Build(int l, int r, int k){
    T[k].l = l, T[k].r = r, T[k].v = -1;
    if(l == r) return;
    int mid = (l+r)>>1;
    Build(l, mid, k<<1);
    Build(mid+1, r, k<<1|1);
}

void Update(int l, int r, int v, int k){
    if(l <= T[k].l && r >= T[k].r){
        T[k].v = v;
        return;
    }
    Pushdown(k);
    int mid = (T[k].l+T[k].r)>>1;
    if(l > mid) Update(l, r, v, k<<1|1);
    else if(r <= mid) Update(l, r, v, k<<1);
    else{
        Update(l, mid, v, k<<1);
        Update(mid+1, r, v, k<<1|1);
    }
    Pushup(k);
}

void Query(int k){
    if(T[k].v != -1){
        for(int i = T[k].l; i <= T[k].r; i++){
            aa[i] = T[k].v;
        }
        return;
    }
    if(T[k].l == T[k].r) return;
    Query(k<<1);
    Query(k<<1|1);
}

int main(){
    while(scanf("%d", &n) == 1){
        memset(aa, -1, sizeof(aa));
        memset(ans, 0, sizeof(ans));
        int m = 0;
        for(int i = 0; i < n; i++){
            scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].v);
            m = max(m, a[i].r);
        }
        Build(1, m, 1);
        for(int i = 0; i < n; i++){
            Update(a[i].l+1, a[i].r, a[i].v, 1);
        }
        Query(1);
        aa[0] = -1;
        for(int i = 1; i <= m; i++){
            if(aa[i] == -1) continue;
            if(aa[i] != aa[i-1]) ans[aa[i]]++;
        }
        for(int i = 0; i <= 8000; i++){
            if(ans[i]) printf("%d %d\n", i, ans[i]);
        }
        printf("\n");
    }
}


發佈了133 篇原創文章 · 獲贊 5 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章