题目链接:http://poj.org/problem?id=2528
Time Limit: 1000MS Memory Limit: 65536K
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:
- 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 liand 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
Problem solving report:
Description: 给N板子依次放上去,问叠起来后能看到几种不一样的。
Problem solving: 因为数据非常大,所以得离散化。离散化就是将一个很大的区间映射到一个较小的区间之中,区间问题小数据可以贪心,大数据就得线段树+离散;这个题本质就是一个区间更新问题。只不过简单的离散化是不对的,例如1~10 1~3 5~10,进行离散化就是:p[1]=1,p[2]=3,p[3]=5,p[4]=10。
- 贴第一张时:墙的1~4被染为1;
- 贴第二张时:墙的1~2被染为2,3~4仍为1;
- 贴第三张时:墙的3~4被染为3,1~2仍为2。
最终,第一张海报就显示被完全覆盖了,于是输出2,但实际上明显不是这样,正确输出为3。
我们改一下离散方法:在相差大于1的数之间加一个数(用来表示这两个中间有数),例如在上面1 3 5 10中间加一个4(都应该加,在这里省略了)。那么就是:p[1]=1,p[2]=3,p[3]=4,p[4]=5,p[5]=10。
这样之后,第一次是1~5被染成1;第二次1~2被染成2;第三次4~5被染成3最终,1~2为2,3为1,4~5为3,于是输出正确结果3。
还有一种解题方法就是我们从后往前来推,在更新的时候就可以判断有没有被贴过,如果被贴过,那么肯定会被挡住,反之就可以看见,所以ans++,用这种方法还是比较简单的。
Accepted Code:
//正着贴
/*
* @Author: lzyws739307453
* @Language: C++
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 10005;
struct edge {
int l, r;
}p[MAXN];
bool vis[MAXN];
int ans, hash_[MAXN << 2], segTree[MAXN << 4];
void PushDown(int rt) {
if (segTree[rt]) {
segTree[rt << 1] = segTree[rt];
segTree[rt << 1 | 1] = segTree[rt];
segTree[rt] = 0;
}
}
void Update(int Ql, int Qr, int v, int l, int r, int rt) {
if (Ql <= l && Qr >= r) {
segTree[rt] = v;
return ;
}
PushDown(rt);
int mid = l + (r - l >> 1);
if (Ql <= mid)
Update(Ql, Qr, v, l, mid, rt << 1);
if (Qr > mid)
Update(Ql, Qr, v, mid + 1, r, rt << 1 | 1);
}
void Query(int l, int r, int rt) {
if (segTree[rt]) {
if (!vis[segTree[rt]]) {
ans++;
vis[segTree[rt]] = 1;
}
return ;
}
if (!(l != r))
return ;
PushDown(rt);
int mid = l + (r - l >> 1);
Query(l, mid, rt << 1);
Query(mid + 1, r, rt << 1 | 1);
}
int main() {
int t, n, cnt;
scanf("%d", &t);
while (t--) {
cnt = 0;
scanf("%d", &n);
memset(vis, 0, sizeof(vis));
memset(segTree, 0, sizeof(segTree));
for (int i = 1; i <= n; i++) {
scanf("%d%d", &p[i].l, &p[i].r);
hash_[cnt++] = p[i].l;
hash_[cnt++] = p[i].r;
}
sort(hash_, hash_ + cnt);
cnt = unique(hash_, hash_ + cnt) - hash_;
int tmp = cnt;
for (int i = 1; i < tmp; i++)
if (hash_[i] - hash_[i - 1] > 1)
hash_[cnt++] = hash_[i - 1] + 1;
sort(hash_, hash_ + cnt);
for (int i = 1; i <= n; i++) {
int l = lower_bound(hash_, hash_ + cnt, p[i].l) - hash_;
int r = lower_bound(hash_, hash_ + cnt, p[i].r) - hash_;
Update(l, r, i, 0, cnt - 1, 1);
}
ans = 0;
Query(0, cnt - 1, 1);
printf("%d\n", ans);
}
return 0;
}
//反着贴
/*
* @Author: lzyws739307453
* @Language: C++
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 10005;
struct edge {
int l, r;
}p[MAXN];
int hash_[MAXN << 2], segTree[MAXN << 4];
bool Update(int Ql, int Qr, int l, int r, int rt) {
if (segTree[rt])
return false;
if (!(l != r)) {
segTree[rt] = true;
return true;
}
int mid = l + (r - l >> 1);
bool res1 = false, res2 = false;
if (Ql <= mid)
res1 = Update(Ql, Qr, l, mid, rt << 1);
if (Qr > mid)
res2 = Update(Ql, Qr, mid + 1, r, rt << 1 | 1);
if (segTree[rt << 1] && segTree[rt << 1 | 1])
segTree[rt] = true;
return res1 || res2;
}
int main() {
int t, n;
scanf("%d", &t);
while (t--) {
int cnt = 0, ans = 0;
scanf("%d", &n);
memset(segTree, 0, sizeof(segTree));
for (int i = 1; i <= n; i++) {
scanf("%d%d", &p[i].l, &p[i].r);
hash_[cnt++] = p[i].l;
hash_[cnt++] = p[i].r;
}
sort(hash_, hash_ + cnt);
cnt = unique(hash_, hash_ + cnt) - hash_;
int tmp = cnt;
for (int i = 1; i < tmp; i++)
if (hash_[i] - hash_[i - 1] > 1)
hash_[cnt++] = hash_[i - 1] + 1;
sort(hash_, hash_ + cnt);
for (int i = n; i >= 0; i--) {
int l = lower_bound(hash_, hash_ + cnt, p[i].l) - hash_;
int r = lower_bound(hash_, hash_ + cnt, p[i].r) - hash_;
if (Update(l, r, 0, cnt - 1, 1))
ans++;
}
printf("%d\n", ans);
}
return 0;
}