ZOJ1610 Count the Colors 线段树

正常区间修改,然后最后一起暴力查一遍就行了。

区间修改有0,需要用-1做lzy标记....

 1 #include <cstdio>
 2 #include <map>
 3 #include <algorithm>
 4 using namespace std;
 5 int col[40000],lzy[40000],vec[10000],cnt[10000];
 6 int n;
 7 void build(int k,int l,int r)
 8 {
 9     col[k] = -1;
10     lzy[k] = -1;
11     if (l == r)
12     {
13         return;
14     }
15     int mid = l + r >> 1;
16     build(k << 1,l,mid);
17     build(k << 1 | 1,mid + 1,r);
18 }
19 void down(int k,int l,int r)
20 {
21     if (l == r)
22         return;
23     lzy[k << 1] = lzy[k];
24     lzy[k << 1 | 1] = lzy[k];
25     col[k << 1] = lzy[k];
26     col[k << 1 | 1] = lzy[k];
27     lzy[k] = -1;
28 }
29 void change(int k,int l,int r,int x,int y,int c)
30 {
31     if (x <= l && r <= y)
32     {
33         lzy[k] = c;
34         col[k] = c;
35         return;
36     }
37     if (lzy[k] != -1) 
38         down(k,l,r);
39     int mid = l + r >> 1;
40     if (x <= mid) change(k << 1,l,mid,x,y,c);
41     if (y >= mid + 1) change(k << 1 | 1,mid + 1,r,x,y,c);
42 }
43 int query(int k,int l,int r,int x)
44 {
45     if (l == r) 
46         return col[k];
47     if (lzy[k] != -1) 
48         down(k,l,r);
49     int mid = l + r >> 1;
50     if (x <= mid) 
51         return query(k << 1,l,mid,x);
52     if (x >= mid + 1) 
53         return query(k << 1 | 1,mid + 1,r,x);
54 }
55 int main()
56 {
57     while (scanf("%d",&n) > 0)
58     {
59         build(1,1,8001);
60         for (int i = 0;i <= 8000;i++)
61             cnt[i] = 0;
62         int tx,ty,tc;
63         for (int i = 1;i <= n;i++)
64         {
65             scanf("%d%d%d",&tx,&ty,&tc);
66             tx++;
67             change(1,1,8001,tx,ty,tc);
68         }
69         for (int i = 0;i <= 8000;i++)
70             vec[i] = query(1,1,8001,i + 1);
71         if (vec[0] != -1)
72             cnt[vec[0]]++;
73         for (int i = 1;i <= 8000;i++)
74             if (vec[i] != -1 && vec[i] != vec[i - 1])
75                 cnt[vec[i]]++;
76         for (int i = 0;i <= 8000;i++)
77             if (cnt[i] != 0)
78                 printf("%d %d\n",i,cnt[i]);
79         printf("\n");
80     }
81     return 0;
82 }
posted @ 2020-01-13 18:58  IAT14  阅读(112)  评论(0编辑  收藏  举报