POJ 2528 Mayor's posters
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:
题目链接 http://poj.org/problem?id=2528
- 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.
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
题意
一面墙很长,贴海报于上。海报互遮挡,可见有几张?
被敲代码耽误的诗人
思路
离散化、线段树,赋给每个海报区间一个数字,最后找有几个不同的数字。”海报被覆盖“在代码中体现为“对应区间被赋予新值”,那就有个数字被覆盖了,意味着最终可数的数字减少
注意
简单的离散化可能会出现错误,给出下面两个简单的例子应该能体现普通离散化的缺陷:
例子一:1-10 1-4 5-10
例子二:1-10 1-4 6-10
普通离散化后都变成了[1,4][1,2][3,4]
线段2覆盖了[1,2],线段3覆盖了[3,4],那么线段1是否被完全覆盖掉了呢?
例子一是完全被覆盖掉了,而例子二没有被覆盖
解决的办法则是对于距离大于1的两相邻点,中间再插入一个点,本题还用到了Lazy标记的思想
直接更新区间进行标记而先不对子节点进行处理,如果需要往下更新再将标记下传一层。
原文出处 https://www.cnblogs.com/xuejianye/p/5694750.html
Code of improved 离散化
///改良版离散化
sort(tem, tem + tot);
///m代表多少种数字;unique函数使得容器前m个数字不重复,
///而后面的数字保持为原来的值
m = unique(tem, tem + tot) - tem;
int temporary = m;
for(int i = 0; i < temporary - 1; ++i)
if(tem[i + 1] - tem[i] > 1)
tem[m++] = tem[i] + 1;
sort(tem, tem + m);///离散化后的值>=tem[0]
单点更新与区间更新
区间更新涉及数据向下传递问题
void down(int x)///数据下传
{
if(a[x].val != -1)
{
a[x << 1].val = a[x].val;
a[x << 1 | 1].val = a[x].val;
a[x].val = -1;
}
}
Code of this problem(2019/3/2)
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100005;
struct node
{
int L, R;
int val;
} a[N << 2];
int m, ans;
int b[N];
int c[N];
int tem[N << 1];
bool vis[N << 1];
void down(int x)///数据下传
{
if(a[x].val != -1)
{
a[x << 1].val = a[x].val;
a[x << 1 | 1].val = a[x].val;
a[x].val = -1;
}
}
void init(int num, int l, int r)
{
a[num].L = l;
a[num].R = r;
a[num].val = -1;///数字从0开始传的缘故
if(l == r)
return ;
int mid = (l + r) >> 1;
init(num << 1, l, mid);
init(num << 1 | 1, mid + 1, r);
}
///针对不同的题目,更新函数必有所修改
void update(int num, int l, int r, int t)
{
if(a[num].L == l && a[num].R == r)
{
a[num].val = t;
return ;
}
if(a[num].L == a[num].R)
return ;
down(num);///此节点非目标节点,数据下传
int mid = (a[num].L + a[num].R) >> 1;
if(mid >= r)
update(num << 1, l, r, t);
else if(mid < l)
update(num << 1 | 1, l, r, t);
else
{
update(num << 1, l, mid, t);
update(num << 1 | 1, mid + 1, r, t);
}
}
void slove(int num, int l, int r)
{
// cout << num << ' ' << a[num].L << " " << a[num].R << ' ' << a[num].val << ' ' << vis[ a[num].val ] << '\n';
if(a[num].val != -1 && !vis[ a[num].val ])
{
vis[ a[num].val ] = 1;
ans++;
}
if(a[num].L == a[num].R)///到底了,返回
return ;
down(num);
int mid = (a[num].L + a[num].R) >> 1;
slove(num << 1, l, mid);
slove(num << 1 | 1, mid + 1, r);
}
int main()
{
int t;
cin >> t;
while(t--)
{
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
memset(tem, 0, sizeof(tem));
memset(vis, 0, sizeof(vis));
int n;
cin >> n;
int tot = 0;
for(int i = 0; i < n; ++i)
{
scanf("%d%d", &b[i], &c[i]);
tem[tot++] = b[i];
tem[tot++] = c[i];
}
///改良版离散化
sort(tem, tem + tot);
m = unique(tem, tem + tot) - tem;
int temporary = m;
for(int i = 0; i < temporary - 1; ++i)
if(tem[i + 1] - tem[i] > 1)
tem[m++] = tem[i] + 1;
sort(tem, tem + m);
init(1, 1, m);
for(int i = 0; i < n; ++i)///数字是从0开始传
{
int tl = lower_bound(tem, tem + m, b[i]) - tem + 1;
int tr = lower_bound(tem, tem + m, c[i]) - tem + 1;
update(1, tl, tr, i);
}
ans = 0;
slove(1, 1, m + 1);
cout << ans << '\n';
}
return 0;
}
第二次做(2019/4/15)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100005;
struct node
{
int L, R, val;
}a[N << 2 | 1];
int ans, b[N], c[N], tem[N << 1];
bool vis[N << 1];
void down(int x)
{
if(a[x].val != -1)///0被占用
{
a[x << 1].val = a[x].val;
a[x << 1 | 1].val = a[x].val;
a[x].val = -1;
}
}
void init(int num, int l, int r)
{
a[num].L = l;
a[num].R = r;
a[num].val = -1;///数字从0开始传的缘故
if(l == r)
return ;
int mid = (l + r) >> 1;
init(num << 1, l, mid);
init(num << 1 | 1, mid + 1, r);
}
void update(int num, int l, int r, int t)
{
if(a[num].L > r || a[num].R < l)
return ;
if(a[num].L >= l && a[num].R <= r)
{
a[num].val = t;
return ;
}
if(a[num].L == a[num].R)
return ;
down(num);
update(num << 1, l, r, t);
update(num << 1 | 1, l, r, t);
}
void slove(int num, int l, int r)///区间[l,r]内有多少种数字
{
if(a[num].val != -1 && !vis[ a[num].val ])
{
vis[ a[num].val ] = 1;
ans++;///为了遍历到底,这里没有 return
}
if(a[num].L == a[num].R)///到底了,返回
return ;
down(num);
int mid = (a[num].L + a[num].R) >> 1;
slove(num << 1, l, mid);
slove(num << 1 | 1, mid + 1, r);
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
memset(tem, 0, sizeof(tem));
memset(vis, 0, sizeof(vis));
int n;
scanf("%d", &n);
int tot = 0;
for(int i = 0; i < n; ++i)
{
scanf("%d%d", &b[i], &c[i]);
tem[tot++] = b[i];
tem[tot++] = c[i];
}
sort(tem, tem + tot);
///m代表多少种数字,函数把重复的数字后放
int m = unique(tem, tem + tot) - tem;
int tempory = m;
for(int i = 0; i + 1 < tempory; ++i)
if(tem[i + 1] - tem[i] > 1)
tem[m++] = tem[i] + 1;
sort(tem, tem + m);///离散化后的值>=tem[0]
init(1, 1, m);
for(int i = 0; i < n; ++i)
{ /// + 1 是因为线段树的左界是从1开始的
int tl = lower_bound(tem, tem + m, b[i]) - tem + 1;
int tr = lower_bound(tem, tem + m, c[i]) - tem + 1;
update(1, tl, tr, i);///区间赋值为i
}
ans = 0;
slove(1, 1, m);
cout << ans << '\n';
}
return 0;
}