天天看点

ZOJ 1610 Count the Colors (线段树-区间覆盖)

题意:

给你 n 个棍子的区间, 和颜色c,要求把这些区间染色c,求最后每种颜色能看见几段?

思路:

直接按照给的顺序区间覆盖即可,最后一个很深的dfs , 把 线段树上所有区间的懒惰标记都往下传。

最后统计每个叶子结点(单节点)的值即可。

另外注意  区间 是左闭右开 或者左开右闭  反正不是 全闭!!!!

很蛋疼的是建树 竟然建成了1,n,1  应该是1,8000,1    RE了一节课了= =~[吐血]

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <stack>
using namespace std;

const int maxn = 8000 + 10;

int setv[maxn<<2];

void pushdown(int o,int l,int r){
    if (setv[o] != -1){
        int lson = o<<1;
        int rson = o<<1|1;
        setv[lson] = setv[rson] = setv[o];
        setv[o] = -1;
    }
}

void update(int L,int R,int c, int l,int r,int o){
    if (L <=l && r <= R){
        setv[o] = c;
        return;
    }
    int m = l + r >> 1;
    pushdown(o,l,r);
    if (m >= L){
        update(L,R,c, l,m,o<<1);
    }
    if (m < R){
        update(L,R,c, m+1, r, o<<1|1);
    }
}

int hs[maxn];
void query(int l,int r,int o){
    if (l == r){
        hs[l] = o;
        return;
    }
    int m = l + r >> 1;
    pushdown(o,l,r);
    query(l,m,o<<1);
    query(m+1,r,o<<1|1);
}

int cnt[maxn];

int main(){
    int n;
    while(~scanf("%d",&n)){
        memset(cnt,0,sizeof cnt);
        memset(setv,-1,sizeof setv);
        for (int i = 0; i < n; ++i){
            int x,y,c;
            scanf("%d %d %d",&x, &y, &c);
            update(x,y-1,c,0,8000,1);
        }
        query(0,8000,1);
//        for (int i = 0; i <= 4; ++i){
//            printf("%d\n", setv[hs[i] ]);
//        }

        for (int i = 0; i <= 8000; ++i){
            if (setv[hs[i] ] == -1) continue;
            cnt[setv[hs[i]] ]++;
            int j;
            for (j = i+1; j <= 8000; ++j){
                if (setv[hs[j] ] == setv[hs[i] ]);
                else break;
            }
            i = j-1;
        }
        for (int i = 0; i < maxn; ++i){
            if (cnt[i]){
                printf("%d %d\n", i, cnt[i]);
            }

        }
        putchar('\n');
    }
    return 0;
}
           

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