[ZOJ] 1610

来源:互联网 发布:java基础在线测试 编辑:程序博客网 时间:2024/06/04 19:43
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;}