POJ2528 Mayor's posters —— 线段树染色 + 离散化

题目链接:https://vjudge.net/problem/POJ-2528

 

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules: 
  • Every candidate can place exactly one poster on the wall. 
  • All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown). 
  • The wall is divided into segments and the width of each segment is one byte. 
  • Each poster must completely cover a contiguous number of wall segments.

They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections. 
Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall. 

Input

The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers l i and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+1 ,... , ri.

Output

For each input data set print the number of visible posters after all the posters are placed. 

The picture below illustrates the case of the sample input. 

Sample Input

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4


 

题解:

1.经典的区间染色问题,可利用线段树的区间修改进行维护。

2.由于区间的范围很大,1e7。但是输入的数据最多只有2e4个,所有需要进行离散化。

3.那是否意味着只需要对输入的数据进行离散呢?

答:不是的。例如一组数据只有三张post:[1,3] 和 [6,10] 和 [1,10],实际答案为3张。如果只对上述的数字进行离散化,则变成:[1,2] 和 [3,4] 和 [1, 4],则答案就变成2张了。为什么会出现这种现象?原因是中间那一段区域[4,5]被忽略掉了。所以,如果两个相邻的数据的差值大于1,则需要对他们之间的区域也进行离散化。

注:根据题目意思,每个数字都代表着一个区域,而不是一个点。再加上没有出现的数字,某些连续的数字有代表着一个区域。所以这题离散的本质对象就是一段段区域,且这些区域是连续的。

 

数组离散(手写二分):

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <vector>
  7 #include <queue>
  8 #include <stack>
  9 #include <map>
 10 #include <string>
 11 #include <set>
 12 using namespace std;
 13 typedef long long LL;
 14 const double EPS = 1e-8;
 15 const int INF = 2e9;
 16 const LL LNF = 2e18;
 17 const int MAXN = 1e4+10;
 18 
 19 //叶子结点最多有4e4个
 20 int color[MAXN<<4];
 21 int le[MAXN], ri[MAXN];
 22 int tmp[MAXN<<1], M[MAXN<<2], visible[MAXN];
 23 
 24 void push_down(int u, int l, int r)
 25 {
 26     if(color[u]!=0)
 27     {
 28         color[u*2] = color[u*2+1]  = color[u];
 29         color[u] = 0;
 30     }
 31 }
 32 
 33 void set_val(int u, int l, int r, int x, int y, int val)
 34 {
 35     if(x<=l && r<=y)
 36     {
 37         color[u] = val;
 38         return;
 39     }
 40 
 41     push_down(u, l, r);
 42     int mid = (l+r)/2;
 43     if(x<=mid) set_val(u*2, l, mid, x, y, val);
 44     if(y>=mid+1) set_val(u*2+1, mid+1, r, x, y, val);
 45 }
 46 
 47 void query(int u, int l, int r)
 48 {
 49     if(l==r)
 50     {
 51         visible[color[u]] = 1;
 52         return;
 53     }
 54 
 55     push_down(u, l, r);
 56     int mid = (l+r)/2;
 57     query(u*2, l, mid);
 58     query(u*2+1, mid+1, r);
 59 }
 60 
 61 int binsearch(int x, int m)
 62 {
 63     int l = 1, r = m;
 64     while(l<=r)
 65     {
 66         int mid = (l+r)/2;
 67         if(M[mid]<=x) l = mid+1;
 68         else r = mid-1;
 69     }
 70     return r;
 71 }
 72 
 73 int main()
 74 {
 75     int T, n;
 76     scanf("%d", &T);
 77     while(T--)
 78     {
 79         scanf("%d", &n);
 80         for(int i = 1; i<=n; i++)
 81         {
 82             scanf("%d%d", &le[i], &ri[i]);
 83             tmp[i*2-1] = le[i];
 84             tmp[i*2] = ri[i];
 85         }
 86 
 87         int m = 0;
 88         sort(tmp+1, tmp+1+2*n);
 89         for(int i = 1; i<=2*n; i++)
 90         {
 91             if(i!=1 && tmp[i]-tmp[i-1]>1) M[++m] = tmp[i]-1;
 92             if(i==1 || tmp[i]!=tmp[i-1]) M[++m] = tmp[i];
 93         }
 94 
 95         memset(color, 0, sizeof(color));
 96         for(int i = 1; i<=n; i++)
 97         {
 98             int l = binsearch(le[i], m);
 99             int r = binsearch(ri[i], m);
100             set_val(1, 1, m, l, r, i);
101         }
102 
103         memset(visible, 0, sizeof(visible));
104         query(1, 1, m);
105         int ans = 0;
106         for(int i = 1; i<=n; i++)
107             if(visible[i]) ans++;
108 
109         printf("%d\n", ans);
110     }
111 }
View Code

 

map离散(超时):

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const double EPS = 1e-8;
15 const int INF = 2e9;
16 const LL LNF = 2e18;
17 const int MAXN = 1e4+10;
18 
19 int val[MAXN*4];
20 int le[MAXN], ri[MAXN];
21 int tmp[MAXN*4],  visible[MAXN];
22 map<int, int>M;
23 
24 void push_down(int u, int l, int r)
25 {
26     if(val[u]!=0)
27     {
28         val[u*2] = val[u*2+1]  = val[u];
29         val[u] = 0;
30     }
31 }
32 
33 void set_val(int u, int l, int r, int x, int y, int v)
34 {
35     if(x<=l && r<=y)
36     {
37         val[u] = v;
38         return;
39     }
40 
41     push_down(u, l, r);
42     int mid = (l+r)/2;
43     if(x<=mid) set_val(u*2, l, mid, x, y, v);
44     if(y>=mid+1) set_val(u*2+1, mid+1, r, x, y, v);
45 }
46 
47 void query(int u, int l, int r)
48 {
49     if(l==r)
50     {
51         visible[val[u]] = 1;
52         return;
53     }
54 
55     push_down(u, l, r);
56     int mid = (l+r)/2;
57     query(u*2, l, mid);
58     query(u*2+1, mid+1, r);
59 }
60 
61 int main()
62 {
63     int T, n;
64     scanf("%d", &T);
65     while(T--)
66     {
67         scanf("%d", &n);
68         for(int i = 1; i<=n; i++)
69         {
70             scanf("%d%d", &le[i], &ri[i]);
71             tmp[i*2-1] = le[i];
72             tmp[i*2] = ri[i];
73         }
74 
75         int m = 0;
76         sort(tmp+1, tmp+1+2*n);
77         M.clear();
78         for(int i = 1; i<=2*n; i++)
79         {
80             if(i!=1 && tmp[i]-tmp[i-1]>1) M[tmp[i]-1] = ++m;
81             if(i==1 || tmp[i]!=tmp[i-1]) M[tmp[i]] = ++m;
82         }
83 
84         memset(val, false, sizeof(val));
85         for(int i = 1; i<=n; i++)
86             set_val(1, 1, m, M[le[i]], M[ri[i]], i);
87 
88         memset(visible, false, sizeof(visible));
89         query(1, 1, m);
90         int ans = 0;
91         for(int i = 1; i<=n; i++)
92             if(visible[i]) ans++;
93 
94         printf("%d\n", ans);
95     }
96 }
View Code

 

posted on 2017-10-24 21:24  h_z_cong  阅读(289)  评论(0编辑  收藏  举报

导航