[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



給你一個區間最大是1 ~ 8000
每次選取一個區間塗色
也是最多8000次操作

最後問你全區間有多少種顏色


剛開始手一抖就想上分塊

看見區間更新

忍住了

因爲只是最後查詢一發
而且數據範圍很小

所以線段樹用 lazy 標記維護一下區間的顏色狀況
最後直接打到底就好了

然後再暴力掃一遍

因爲顏色有從 0 標號開始

所以 lazy 標記初始要爲 -1

剛開始因爲這個WA了

然後 PE

生氣

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

const int N = 10010;

struct Tree
{
	int l;
	int r;
	int c;
	int lazy;
};

Tree tree[N * 4];
int n;
int ma[N];
int cnt[N];

void build(int x, int l, int r)
{
	tree[x].l = l;
	tree[x].r = r;
	tree[x].c = -1;
	tree[x].lazy = -1;
	if(l == r){
		return;
	}
	else{
		int mid = l + ((r - l) >> 1);

		build(x << 1, l, mid);
		build(x << 1 | 1, mid + 1, r);
	}
}

void down(int x)
{
	int t = tree[x].lazy;

	if(t != -1){
		tree[x].lazy = -1;
		tree[x << 1].c = t;
		tree[x << 1 | 1].c = t;
		tree[x << 1].lazy = t;
		tree[x << 1 | 1].lazy = t;
	}
}

void update(int x, int l, int r, int v)
{
	int ll = tree[x].l;
	int rr = tree[x].r;

	if(l == ll && r == rr){
		tree[x].lazy = v;
		tree[x].c = v;
		return ;
	}

	if(l > r){
		return ;
	}
	else{
		int mid = ll + ((rr - ll) >> 1);

		down(x);
		update(x << 1, l, min(mid, r), v);
		update(x << 1 | 1, max(l, mid + 1), r, v);
	}
}

void quary(int x, int l, int r)
{
	if(l == r){
		ma[l] = tree[x].c;
	}
	else{
		int mid = l + ((r - l) >> 1);

        down(x);
		quary(x << 1, l, mid);
		quary(x << 1 | 1, mid + 1, r);
	}
}

int main(int argc, char const *argv[])
{
    int mark;

	while(scanf("%d", &n) == 1){
        memset(cnt, 0, sizeof(cnt));
		build(1, 0, N);
		for(int i = 0; i < n; i ++){
			int l, r, v;

			scanf("%d%d%d", &l, &r, &v);
			update(1, l, r - 1, v);
		}
		quary(1, 0, N);
		if(ma[0] != -1){
            cnt[ma[0]] ++;
		}
		for(int i = 1; i < N; i ++){
            if(ma[i] == -1){
                continue;
            }
            while(ma[i] == ma[i - 1]){
                i ++;
            }
            cnt[ma[i]]++;
		}
		for(int i = 0; i < N; i ++){
            if(cnt[i]){
                printf("%d %d\n", i, cnt[i]);
            }
		}
		printf("\n");
	}

	return 0;
}



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