ZOJ-1610 Count the Colors 【线段树+区间更新】

Description

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

 


 

思路:

线段树区间更新,用一个vector统计每段的颜色,注意没有着色的段也要统计,因为它隔开的两端若是同色如果不统计那么就合成了一段

 

Code:

 

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<set>
 6 #include<vector>
 7 #include<string>
 8 #include<queue>
 9 using namespace std;
10 #define lson o<<1
11 #define rson o<<1|1
12 #define M(a, b) memset(a, b, sizeof(a))
13 #define INF 0x3f3f3f3f
14 const int maxn = 8000 + 5;
15 int val[maxn<<2], qL, qR, v, ans[maxn];
16 vector<int> vec;
17 
18 void init() {
19     M(val, -1);
20     M(ans, 0);
21     vec.clear();
22 }
23 
24 void pushdown(int o) {
25     if (~val[o]) {
26         val[lson] = val[rson] = val[o];
27         val[o] = -1;
28     }
29 }
30 
31 void update(int o, int L, int R) {
32     if (qL <= L && R <= qR) val[o] = v;
33     else {
34         if (val[o] == v) return;
35         pushdown(o);
36         int M = (L + R) >> 1;
37         if (qL <= M) update(lson, L, M);
38         if (M < qR) update(rson, M+1, R);
39     }
40 }
41 
42 void query(int o, int L, int R) {
43     if (~val[o]) vec.push_back(val[o]);
44     else {
45         if (L == R) {
46             vec.push_back(-1);
47             return;
48         }
49         int M = (L + R) >> 1;
50         query(lson, L, M);
51         query(rson, M+1, R);
52     }
53 }
54 
55 int main() {
56     int n;
57     while (~scanf("%d", &n)) {
58         init();
59         while (n--) {
60             scanf("%d%d%d", &qL, &qR, &v);
61             if (qL >= qR) continue;
62             qL++;
63             update(1, 1, maxn);
64         }
65         query(1, 1, maxn);
66         int temp = -1;
67         for (int i = 0; i < vec.size(); ++i) {
68             if (temp == vec[i]) continue;
69             else {
70                 temp = vec[i];
71                 if (~temp) ++ans[temp];
72             }
73         }
74         for (int i = 0; i < maxn; ++i)
75             if (ans[i]) printf("%d %d\n", i, ans[i]);
76         printf("\n");
77     }
78 
79     return 0;
80 }

 

转载于:https://www.cnblogs.com/robin1998/p/6596448.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值