poj~2528 Mayor's posters
Mayor's posters
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 73869 | Accepted: 21303 |
Description
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.
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
这题注意要离散化就OK了 ，线段树离散化 ，
但是要注意一个坑点
if (sum[i] - sum[i - 1] > 1) sum[m++] = sum[i - 1] + 1;
要加一个点，避免区间不正确覆盖。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 2e4 + 10; 6 int tree[maxn << 4], a[maxn], b[maxn]; 7 int sum[3 * maxn], vis[3 * maxn], ans; 8 void init() { 9 memset(tree, -1, sizeof(tree)); 10 memset(vis, 0, sizeof(vis)); 11 } 12 void pushdown(int rt) { 13 tree[rt << 1] = tree[rt << 1 | 1] = tree[rt]; 14 tree[rt] = -1; 15 } 16 void updata(int l, int r, int x, int y, int rt, int v) { 17 if (x <= l && r <= y ) { 18 tree[rt] = v; 19 return ; 20 } 21 if (tree[rt] != -1) pushdown(rt); 22 int m = (l + r) >> 1; 23 if (x <= m) updata(l, m, x, y, rt << 1, v); 24 if (y > m ) updata(m + 1, r, x, y, rt << 1 | 1, v); 25 } 26 void query(int l, int r, int rt) { 27 if (tree[rt] != -1 ) { 28 if (!vis[tree[rt]]) { 29 vis[tree[rt]] = 1; 30 ans++; 31 } 32 return ; 33 } 34 if (l == r) return ; 35 int m = (l + r) >> 1; 36 query(l, m, rt << 1); 37 query(m + 1, r, rt << 1 | 1); 38 } 39 int main() { 40 int t, n; 41 scanf("%d", &t); 42 while(t--) { 43 init(); 44 scanf("%d", &n); 45 int tot = 0; 46 for (int i = 0 ; i < n ; i++) { 47 scanf("%d%d", &a[i], &b[i]); 48 sum[tot++] = a[i]; 49 sum[tot++] = b[i]; 50 } 51 sort(sum, sum + tot); 52 int m = unique(sum, sum + tot) - sum; 53 int t = m; 54 for (int i = 1 ; i < t ; i++) { 55 if (sum[i] - sum[i - 1] > 1) sum[m++] = sum[i - 1] + 1; 56 } 57 sort(sum, sum + m); 58 for (int i = 0 ; i < n ; i++) { 59 int x = lower_bound(sum, sum + m, a[i]) - sum; 60 int y = lower_bound(sum, sum + m, b[i]) - sum; 61 updata(0, m - 1, x, y, 1, i); 62 } 63 ans = 0; 64 query(0, m - 1, 1); 65 printf("%d\n", ans); 66 } 67 return 0; 68 }