ZOJ 1610 Count the Colors [ 线段树 + 区间染色 ]

题目链接:

POJ - 2528 Count the Colors

题意概括:

按顺序给出一些线段,后出现的线段会覆盖前出现线段的重叠部分。求剩下线段中,有哪些颜色,同时求出每种颜色线段数量

数据范围:

1 <= n <= 8000

0 <= x1 , x2 , c <= 8000

题解分析:

区间更新的问题,很明显应该用线段树来做,但是有很多细节要注意

origin数组维护的原数据是线段的颜色,并且用右节点的下标作为它的下标

如:下标为 0,1 两个节点的线段,下标是 1

线段树维护的值是区间的颜色:

  • 如果区间内的所有线段颜色相同,则该区间的颜色就是这个颜色
  • 反之,该区间的颜色编号就是 INF,表示区间颜色不一致

这里需要遍历到每个点,也就是最下层的位置。按遍历的顺序可以知道,我们可以从左到右依次遍历每条单位线段

定义一个 last变量 记录上一条单位线段的颜色,防止同一条线段重复计数

AC代码:

#include <stdio.h>
#include <memory.h>
using namespace std;
const int MAXN = 8e3 + 10;
const int INF = 0x3f3f3f3f;
int origin[MAXN], tree[MAXN<<2], lazy[MAXN<<2], ans[MAXN], last = INF;

void pushup(int p) {
    if (tree[p << 1] == tree[p << 1 | 1])
        tree[p] = tree[p << 1];
    else
        tree[p] = INF;
}

void pushdown(int p, int l, int r) {
    if (lazy[p] != INF) {
        lazy[p << 1] = lazy[p];
        lazy[p << 1 | 1] = lazy[p];
        tree[p << 1] = lazy[p];  //左右儿子结点均按照需要加的值总和更新结点信息
        tree[p << 1 | 1] = lazy[p];
        lazy[p] = INF;
    }
}

void build(int p, int l, int r) {
    if (l == r) {
        tree[p] = origin[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
    pushup(p);
}

void update(int p, int l, int r, int ql, int qr, int v) {   //区间更新
    if (ql <= l && r <= qr) {       //当前区间在更新区间内
        tree[p] = v;
        lazy[p] = v;
        return;
    }
    int mid = (l + r) >> 1;
    pushdown(p, l, r);
    if (ql <= mid)  update(p << 1, l, mid, ql, qr, v);
    if (qr > mid)  update(p << 1 | 1, mid + 1, r, ql, qr, v);
    pushup(p);
}


void query(int p, int l, int r, int ql, int qr) {
    if (l == r) {
    if (tree[p] != INF && tree[p] != last)
            ans[tree[p]] ++;
        last = tree[p];
        return;
    }
    int mid = (l + r) >> 1;
    pushdown(p, l, r);        //有区间更新时才需要
    
    query(p << 1, l, mid, ql, qr);
    query(p << 1 | 1, mid + 1, r, ql, qr);    //分块切割出有效的部分(已忽略无效部分)
}

int main () {

    int n;
    while(scanf("%d", &n)!= EOF) {
    int i, j, c;
        memset(lazy, INF, sizeof(lazy));
        memset(ans, 0, sizeof(ans));
        last = INF;
        
        for (int i = 1; i <= 8001; i++)
            origin[i] = INF;
        build(1, 1, 8001);
        
        while(n--) {
            scanf("%d%d%d", &i, &j, &c);
                update(1, 1, 8001, i + 1, j, c);
            }
        query(1, 1, 8001, 1, 8001);
        for (int i = 0; i <= 8000; i ++)
            if (ans[i]) printf("%d %d\n", i, ans[i]);
         putchar('\n');
        }
    }

 

                                                    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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章