ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】

任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610

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

 题意概括:

多测试样例,每个有 N 次操作,对区间 [a, b] 涂色,是涂区间是涂区间,不是涂区间的点!

是很别扭,举个栗子:涂 1-2 和 3-4,如果是涂点那么 1、2、3、4都涂了,如果是涂区间那么 2-3 区间没有涂。

解题思路:

线段树更新修改区间(把原来的数组看成一个点,两点之间的差是区间),暴力单点查询每点的颜色值,统计每种可以看得见的颜色有几段。

AC code:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 #define INF 0x3f3f3f3f
 6 #define LL long long int
 7 #define lson l, mid, root<<1
 8 #define rson mid+1, r, root<<1|1
 9 using namespace std;
10 const int MAXN = 8008;
11 struct date{
12     int L, R, val;
13 }node[MAXN];
14 int lyt[MAXN<<2];
15 int cnt[MAXN];
16 int N, max_R, max_C;
17 
18 void PushDown(int root)
19 {
20     if(lyt[root] >= 0){
21         lyt[root<<1] = lyt[root<<1|1] = lyt[root];
22         lyt[root] = -1;
23     }
24 }
25 void Update(int L, int R, int l, int r, int root, int val)
26 {
27     if(L <= l && r <= R){lyt[root] = val;return;}
28     int mid = (l+r)>>1;
29     PushDown(root);
30     if(L <= mid) Update(L, R, lson, val);
31     if(R > mid)  Update(L, R, rson, val);
32 }
33 int Query(int pos, int l, int r, int root)
34 {
35     if(l == r) return lyt[root];
36     int ret;
37     PushDown(root);
38     int mid = (l+r)>>1;
39     if(pos <= mid) ret = Query(pos, lson);
40     if(pos > mid)  ret = Query(pos, rson);
41     return ret;
42 }
43 void init()
44 {
45     max_R = 0; max_C = 0;
46     memset(cnt, 0, sizeof(cnt));
47     memset(lyt, -1, sizeof(lyt));
48 }
49 int main()
50 {
51     while(~scanf("%d", &N)){
52         init();                       //初始化
53         for(int i = 0; i < N; i++){   //输入
54             scanf("%d%d%d", &node[i].L, &node[i].R, &node[i].val);
55             node[i].L++;
56             max_R = max(node[i].R, max_R);
57             max_C = max(max_C, node[i].val);
58         }
59         for(int i = 0; i < N; i++){    //建树
60             if(node[i].R >= node[i].L)
61                 Update(node[i].L, node[i].R, 1, max_R, 1, node[i].val);
62         }
63         int last_color=-1, now_color;
64         for(int i = 1; i <= max_R; i++)
65         {
66             now_color = Query(i, 1, max_R, 1);
67             if(now_color == -1){last_color = -1; continue;} ///没有染色
68             if(now_color != last_color) cnt[now_color]++;   ///该颜色断层
69             last_color = now_color;
70         }
71         for(int i = 0; i <= max_C; i++){
72             if(cnt[i]>0) printf("%d %d
", i, cnt[i]);
73         }
74         puts("");
75     }
76     return 0;
77 }
View Code
原文地址:https://www.cnblogs.com/ymzjj/p/9556590.html