Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 50888 | Accepted: 14737 |
Description
- 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
Output
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, 10), (1, 6), (8, 10) 这样6和8之间,只有加点才会使7被考虑在内
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int xx[], yy[];
int sor[], cnt, deal[], col[];
int ans, vis[];
int hi;
void hash() {
int last = sor[];
hi = ;
memset(deal, , sizeof(deal));
deal[++hi] = last;
for (int i = ; i <= cnt; i++ ) {
if (sor[i] == last) continue;
if (sor[i] - last > ) deal[++hi] = last + , deal[++hi] = sor[i], last = sor[i];
else deal[++hi] = sor[i], last = sor[i];
}
sort(deal + , deal + + hi);
return;
}
int Bsearch(int x) {
int low = , high = hi;
while (low <= high) {
int mid = low + high >> ;
if (deal[mid] == x) return mid;
if (deal[mid] < x) low = mid + ;
else high = mid - ;
}
return -;
}
void Push_down(int o) {
if (col[o] != -) {
col[o << ] = col[o << | ] = col[o];
col[o] = -;
}
}
void query(int o, int l, int r) {
if (col[o] != -) {
if (!vis[col[o]]) ans++;
vis[col[o]] = true;
return;
}
if (l == r) return;
int mid = (l + r) >> ;
query(o << , l, mid);
query(o << | , mid + , r);
}
void update(int o, int l, int r, int ql, int qr, int v) {
if (ql <= l && r <= qr) {
col[o] = v;
return;
}
Push_down(o);
int mid = (l + r)>> ;
if (ql <= mid) update(o << , l, mid, ql, qr, v);
if (qr > mid) update(o << | , mid + , r, ql, qr, v);
}
int main() {
int t, n;
scanf("%d", &t);
while (t--) {
ans = ;
scanf("%d", &n);
cnt = ;
memset(sor, , sizeof(sor));
for (int i = ; i <= n; i++) {
scanf("%d%d", &xx[i], &yy[i]);
sor[++cnt] = xx[i], sor[++cnt] = yy[i];
}
sort(sor + , sor + cnt + );
hash();
memset(col, -, sizeof(col));
for (int i = ; i <= n; i++) {
int ql = Bsearch(xx[i]), qr = Bsearch(yy[i]);
update(, , hi, ql, qr, i);
}
memset(vis, , sizeof(vis));
query(, , hi);
printf("%d\n", ans);
}
return ;
}