首页 > 代码库 > 非结构体线段树版 ZJU 1610 Count the Colors (线段树区间更新)

非结构体线段树版 ZJU 1610 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,而是8000.

这道题昨天夜里写的,当时脑子已经完全糊涂一片,主要问题出在如何 判断当前颜色与前面的颜色 的关系。 关系应该是当前的颜色等于与它相邻的前面的区间 的颜色,那么这种颜色是记为1段,其余的情况全部记为一种新颜色,这样做即使不是新颜色,也把它当做一种in颜色,因为它肯定不是只有一段了。

我也看了一些网上的线段树写法,几乎都是用结构体存的树。。那种方法至今不会用,我感觉HH牛的风格更好,写起来也方便。

昨天晚上调试半天脑子很乱,早上醒来就A了。。。看来脑子乱的时候睡觉才是明智的


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <ctype.h>
#define lson o << 1, l, m
#define rson o << 1|1, m+1, r
using namespace std;
typedef long long LL;
const int MAX = 0x3f3f3f3f;
const int maxn = 8888;
int n, a, b, c;
int cnt[maxn<<2], vis[maxn<<2];
struct C {
    int c, r;
} top;
void down(int o) {
    if(cnt[o] != -1) {
        cnt[o<<1] = cnt[o<<1|1] = cnt[o];
        cnt[o] = -1;
    }
}
void update(int o, int l, int r) {
    if(a <=l && r <=b) {
        cnt[o] = c;
        return;
    }
    down(o);
    int m = (l+r) >> 1;
    if(a <= m) update(lson);
    if(m < b ) update(rson);
}
void query(int o, int l, int r) {
    if(cnt[o] != -1) {
        if(top.c == cnt[o] && top.r == l-1) top.r = r;
        else {
            vis[ cnt[o] ]++;
            top.r = r;
            top.c = cnt[o];
        }
        return ;
    }
    if(l == r) return;
    int m = (l+r) >> 1;
    query(lson);
    query(rson);
}
int main()
{
    while(~scanf("%d", &n)) {
        memset(cnt, -1, sizeof(cnt));
        for(int i = 0; i < n; i++) {
            scanf("%d%d%d", &a, &b, &c);
            if(a == b) continue;
            if(a > b) swap(a, b);
            a++;
            update(1, 1, 8000);
        }
        memset(vis, 0, sizeof(vis));
        top.c = top.r = -1;
        query(1, 1, 8000);
        for(int i = 0; i < 8001; i++)
            if(vis[i])  printf("%d %d\n", i, vis[i]);
        printf("\n");
    }
    return 0;
}