Mayor's posters
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 li 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 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+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
Source
Alberta Collegiate Programming Contest 2003.10.18
题目类型:离散化+线段树(区间染色)
算法分析:这是一道会发生覆盖的区间染色问题,由于输入数据范围比较大,所以需要使用离散化操作。然后使用Lazy-Tag标记,lazy数组表示区间被覆盖到的海报的标号。最后累加整个离散化后的区间中存在的不同的海报的标号的个数即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 20000 + 6666; struct Node { int x, y; }; Node a[maxn]; int xx[maxn*3], xxlen, lazy[maxn<<4]; bool have[maxn]; long long res; void PushDown (int rt, int l, int m, int r) { if (lazy[rt] != -1) { lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt]; lazy[rt] = -1; } } void UpDate (int rt, int l, int r, int L, int R, int id) { if (L <= l && r <= R) { lazy[rt] = id; return ; } int m = (l + r) >> 1; PushDown (rt, l, m, r); if (L <= m) UpDate (lson, L, R, id); if (R > m) UpDate (rson, L, R, id); } void Query (int rt, int l, int r) { if (lazy[rt] != -1) { if (!have[lazy[rt]]) { have[lazy[rt]] = true; res++; } return ; } if (l == r) return ; int m = (l + r) >> 1; // PushDown (rt, l, m, r); Query (lson); Query (rson); } int main() { // ifstream cin ("aaa.txt"); // freopen ("aaa.txt", "r", stdin); int t; scanf ("%d", &t); while (t--) { int n; scanf ("%d", &n); xxlen= 0; for (int i = 1; i<= n; i++) { scanf ("%d%d", &a[i].x, &a[i].y); xx[xxlen++] = a[i].x, xx[xxlen++] = a[i].y; } sort (xx, xx + xxlen); xxlen = unique (xx, xx + xxlen) - xx; for (int i = xxlen - 1; i >= 1; i--) if (xx[i] != xx[i-1] + 1) xx[xxlen++] = xx[i-1] + 1; sort (xx, xx + xxlen); // for (int i = 0; i < xxlen;i++) // cout << xx[i] << endl; fill (lazy, lazy + (maxn<<4), -1); for (int i = 1; i <= n; i++) { int sx = lower_bound (xx, xx + xxlen, a[i].x) - xx; int sy = lower_bound (xx, xx + xxlen, a[i].y) - xx; // cout << sx << " " << sy << endl; UpDate (1, 0, xxlen, sx, sy, i); } memset (have, false, sizeof (have)); res = 0; Query (1, 0, xxlen); printf ("%lld\n", res); } return 0; } |
- « 上一篇:poj2513
- poj2559:下一篇 »