Count the Colors ZOJ - 1610

本文介绍了一种通过构建树状数据结构解决颜色覆盖问题的方法,实现了统计不同颜色可见线段数量的功能。通过将线段转化为点,巧妙地解决了连续相同颜色只计一次的问题。

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.


<b< dd="">

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.


<b< dd="">

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

题意:给了每一线段的颜色,存在颜色覆盖,求表面上看能看到的颜色种类和各种颜色的段数。

题解:和POJ - 2528 有相似之处。这题因为数据较小所以不需要离散化。数该颜色有几条可见时,注意连续的区间有相同的颜色只算一次,所以有了temp这个标记。

为了实现temp的功能,将区间分为二类,一次都没被涂过和至少涂过一次。

将单位线段转化成一个点 !!!!!

 1 #pragma warning(disable:4996)
 2 #include<cstdio>
 3 #include<string>
 4 #include<cstring>
 5 #include<iostream>
 6 #include<algorithm>
 7 using namespace std;
 8 
 9 #define lson root<<1
10 #define rson root<<1|1
11 #define ll long long 
12 
13 const int maxn = 200005;
14 
15 int n, temp;
16 int use[maxn];
17 
18 struct node { int l, r, color; } Tree[maxn];
19 
20 void Pushdown(int root) {
21     Tree[lson].color = Tree[root].color;
22     Tree[rson].color = Tree[root].color;
23     Tree[root].color = -2;
24 }
25 
26 void Build(int l, int r, int root) {
27     Tree[root].l = l;
28     Tree[root].r = r;
29     Tree[root].color = -1;   //一次都没涂过
30     if (l == r) return;
31     int mid = (l + r) >> 1;
32     Build(l, mid, lson);
33     Build(mid + 1, r, rson);
34 }
35 
36 void Update(int L, int R, int l, int r, int root, int x) {
37     if (l > R || r < L) return;
38     if (L <= l && r <= R) {
39         Tree[root].color = x;
40         return;
41     }
42     if (Tree[root].color >= 0) Pushdown(root);
43     int mid = (l + r) >> 1;
44     Update(L, R, l, mid, lson, x);
45     Update(L, R, mid + 1, r, rson, x);
46     Tree[root].color = -2;      //至少涂过一次。
47 }
48 
49 void Query(int l, int r, int root) {
50     if (Tree[root].color == -1) {
51         temp = -1;
52         return;
53     }
54     if (Tree[root].color >= 0) {
55         if (Tree[root].color != temp) {
56             use[Tree[root].color]++;
57             temp = Tree[root].color;
58         }
59         return;
60     }
61     if (l == r) return;
62 
63     int mid = (l + r) >> 1;
64     Query(l, mid, lson);
65     Query(mid + 1, r, rson);
66 }
67 
68 int main()
69 {
70     while (scanf("%d", &n) != EOF) {
71         memset(use, 0, sizeof(use));
72         Build(1, 8000, 1);
73         for (int i = 1; i <= n; i++) {
74             int x, y, d;
75             scanf("%d%d%d", &x, &y, &d);
76             Update(x + 1, y, 1, 8000, 1, d);  //将线段转化成点的区间
77         }
78         temp = -1;
79         Query(1, 8000, 1);
80         for (int i = 0; i <= 8000; i++) if (use[i]) printf("%d %d\n", i, use[i]);
81         printf("\n");
82     }
83     return 0;
84 }

 

转载于:https://www.cnblogs.com/zgglj-com/p/8849935.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值