poj2528 线段树+离散化 (倒序)
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:
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.
- 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.
![](https://odzkskevi.qnssl.com/85d8df2191db8df82ba259ebe2bfe93e?v=1505473219)
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 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 #define lson l , m , rt << 1 6 #define rson m + 1 , r , rt << 1 | 1 7 8 const int maxn = 11111; 9 bool hash[maxn]; 10 int li[maxn] , ri[maxn]; 11 int X[maxn*3]; /*最多3倍~*/ 12 int col[maxn<<4]; /*2X的空间复杂度是普通的四倍*/ 13 int cnt; 14 15 void PushDown(int rt) { 16 if (col[rt] != -1) { 17 col[rt<<1] = col[rt<<1|1] = col[rt]; /*直接赋值 覆盖之~*/ 18 col[rt] = -1; 19 } 20 } 21 void update(int L,int R,int c,int l,int r,int rt) { 22 if (L <= l && r <= R) { 23 col[rt] = c; 24 return ; /*盖了果断return*/ 25 } 26 PushDown(rt); 27 int m = (l + r) >> 1; 28 if (L <= m) update(L , R , c , lson); 29 if (m < R) update(L , R , c , rson); 30 } 31 void query(int l,int r,int rt) { 32 if (col[rt] != -1) { 33 if (!hash[col[rt]]) cnt ++; 34 hash[ col[rt] ] = true; 35 return ; /*由于这里是直接return,在最顶层的mark处直接跳过此区间,所以不用在下面加PushDown*/ 36 } 37 if (l == r) return ; 38 int m = (l + r) >> 1; 39 query(lson); 40 query(rson); 41 } 42 int Bin(int key,int n,int X[]) { /*离散化哈希函数*/ 43 int l = 0 , r = n - 1; 44 while (l <= r) { /*离散化哈希--二分映射*/ 45 int m = (l + r) >> 1; 46 if (X[m] == key) return m; 47 if (X[m] < key) l = m + 1; 48 else r = m - 1; 49 } 50 return -1; /*注意key值一定要在X中,否则各种跪*/ 51 } 52 int main() { 53 int T , n; 54 scanf("%d",&T); 55 while (T --) { 56 scanf("%d",&n); 57 int nn = 0; 58 for (int i = 0 ; i < n ; i ++) { /*把所有出现的数装在X里*/ 59 scanf("%d%d",&li[i] , &ri[i]); 60 X[nn++] = li[i]; 61 X[nn++] = ri[i]; 62 } 63 sort(X , X + nn); 64 int m = 1; 65 for (int i = 1 ; i < nn; i ++) { /*排序之后去重*/ 66 if (X[i] != X[i-1]) X[m ++] = X[i]; 67 } 68 for (int i = m - 1 ; i > 0 ; i --) { /*离散化技巧:凸显间隔(可避免上文的数据2出错)*/ 69 if (X[i] != X[i-1] + 1) X[m ++] = X[i-1] + 1; 70 } 71 sort(X , X + m); /*再次排序,便于之后设计映射时用二分高效hash*/ 72 memset(col , -1 , sizeof(col)); 73 for (int i = 0 ; i < n ; i ++) { 74 int l = Bin(li[i] , m , X); /*Bin为离散哈希函数*/ 75 int r = Bin(ri[i] , m , X); /*Bin为离散哈希函数*/ 76 update(l , r , i , 0 , m , 1); /*以离散后的键值更新线段树*/ 77 } 78 cnt = 0; 79 memset(hash , false , sizeof(hash)); 80 query(0 , m , 1); 81 printf("%d\n",cnt); 82 } 83 return 0; 84 }