1. 程式人生 > >ZOJ 1610 Count the Colors【題意+線段樹區間更新&&單點查詢】

ZOJ 1610 Count the Colors【題意+線段樹區間更新&&單點查詢】

統計 dex close inf task ESS struct lin right

任意門: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\n", i, cnt[i]);
73         }
74         puts("");
75     }
76     return 0;
77 }
View Code

ZOJ 1610 Count the Colors【題意+線段樹區間更新&&單點查詢】