POJ2528 Mayor's posters —— 线段树染色 + 离散化
题目链接:https://vjudge.net/problem/POJ-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.
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.经典的区间染色问题,可利用线段树的区间修改进行维护。
2.由于区间的范围很大,1e7。但是输入的数据最多只有2e4个,所有需要进行离散化。
3.那是否意味着只需要对输入的数据进行离散呢?
答:不是的。例如一组数据只有三张post:[1,3] 和 [6,10] 和 [1,10],实际答案为3张。如果只对上述的数字进行离散化,则变成:[1,2] 和 [3,4] 和 [1, 4],则答案就变成2张了。为什么会出现这种现象?原因是中间那一段区域[4,5]被忽略掉了。所以,如果两个相邻的数据的差值大于1,则需要对他们之间的区域也进行离散化。
注:根据题目意思,每个数字都代表着一个区域,而不是一个点。再加上没有出现的数字,某些连续的数字有代表着一个区域。所以这题离散的本质对象就是一段段区域,且这些区域是连续的。
数组离散(手写二分):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e4+; //叶子结点最多有4e4个
int color[MAXN<<];
int le[MAXN], ri[MAXN];
int tmp[MAXN<<], M[MAXN<<], visible[MAXN]; void push_down(int u, int l, int r)
{
if(color[u]!=)
{
color[u*] = color[u*+] = color[u];
color[u] = ;
}
} void set_val(int u, int l, int r, int x, int y, int val)
{
if(x<=l && r<=y)
{
color[u] = val;
return;
} push_down(u, l, r);
int mid = (l+r)/;
if(x<=mid) set_val(u*, l, mid, x, y, val);
if(y>=mid+) set_val(u*+, mid+, r, x, y, val);
} void query(int u, int l, int r)
{
if(l==r)
{
visible[color[u]] = ;
return;
} push_down(u, l, r);
int mid = (l+r)/;
query(u*, l, mid);
query(u*+, mid+, r);
} int binsearch(int x, int m)
{
int l = , r = m;
while(l<=r)
{
int mid = (l+r)/;
if(M[mid]<=x) l = mid+;
else r = mid-;
}
return r;
} int main()
{
int T, n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i<=n; i++)
{
scanf("%d%d", &le[i], &ri[i]);
tmp[i*-] = le[i];
tmp[i*] = ri[i];
} int m = ;
sort(tmp+, tmp++*n);
for(int i = ; i<=*n; i++)
{
if(i!= && tmp[i]-tmp[i-]>) M[++m] = tmp[i]-;
if(i== || tmp[i]!=tmp[i-]) M[++m] = tmp[i];
} memset(color, , sizeof(color));
for(int i = ; i<=n; i++)
{
int l = binsearch(le[i], m);
int r = binsearch(ri[i], m);
set_val(, , m, l, r, i);
} memset(visible, , sizeof(visible));
query(, , m);
int ans = ;
for(int i = ; i<=n; i++)
if(visible[i]) ans++; printf("%d\n", ans);
}
}
map离散(超时):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e4+; int val[MAXN*];
int le[MAXN], ri[MAXN];
int tmp[MAXN*], visible[MAXN];
map<int, int>M; void push_down(int u, int l, int r)
{
if(val[u]!=)
{
val[u*] = val[u*+] = val[u];
val[u] = ;
}
} void set_val(int u, int l, int r, int x, int y, int v)
{
if(x<=l && r<=y)
{
val[u] = v;
return;
} push_down(u, l, r);
int mid = (l+r)/;
if(x<=mid) set_val(u*, l, mid, x, y, v);
if(y>=mid+) set_val(u*+, mid+, r, x, y, v);
} void query(int u, int l, int r)
{
if(l==r)
{
visible[val[u]] = ;
return;
} push_down(u, l, r);
int mid = (l+r)/;
query(u*, l, mid);
query(u*+, mid+, r);
} int main()
{
int T, n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i<=n; i++)
{
scanf("%d%d", &le[i], &ri[i]);
tmp[i*-] = le[i];
tmp[i*] = ri[i];
} int m = ;
sort(tmp+, tmp++*n);
M.clear();
for(int i = ; i<=*n; i++)
{
if(i!= && tmp[i]-tmp[i-]>) M[tmp[i]-] = ++m;
if(i== || tmp[i]!=tmp[i-]) M[tmp[i]] = ++m;
} memset(val, false, sizeof(val));
for(int i = ; i<=n; i++)
set_val(, , m, M[le[i]], M[ri[i]], i); memset(visible, false, sizeof(visible));
query(, , m);
int ans = ;
for(int i = ; i<=n; i++)
if(visible[i]) ans++; printf("%d\n", ans);
}
}
POJ2528 Mayor's posters —— 线段树染色 + 离散化的更多相关文章
- poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 43507 Accepted: 12693 ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- POJ2528:Mayor's posters(线段树区间更新+离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- poj2528 Mayor's posters(线段树区间修改+特殊离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- poj2528 Mayor's posters(线段树之成段更新)
Mayor's posters Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 37346Accepted: 10864 Descr ...
- poj2528 Mayor's posters(线段树区间覆盖)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 50888 Accepted: 14737 ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- Mayor's posters(线段树+离散化POJ2528)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
随机推荐
- URI跟URL的区别
关于URL和URI的区别,个人见解. 初学java,最近被一个概念搞得头晕脑胀,就是url和uri的概念和区别,网上查了一大通,发现各种回答眼花缭乱,有百科直接粘贴的,有胡说八道的,有故意绕来绕 ...
- BZOJ 3926: [Zjoi20150]诸神眷顾的幻想乡
3926: [Zjoi20150]诸神眷顾的幻想乡 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 438 Solved: 273 Descripti ...
- 一篇文章告诉你,TLS 1.3 如何用性能为 HTTPS 正名
序•魔戒再现 几天前,OpenSSL 官方宣布即将发布的新版本 (OpenSSL 1.1.1) 将会提供 TLS 1.3 的支持,而且还会和之前的 1.1.0 版本完全兼容,这当然是个好消息. ...
- HDU 4499
题目大意: N*M的棋盘上摆了一些棋子,在剩余位置上尽可能多的摆上炮,使所有炮不能互吃 dfs+回溯 #include <iostream> #include <cstdio> ...
- Method, apparatus and system for acquiring a global promotion facility utilizing a data-less transaction
A data processing system includes a global promotion facility and a plurality of processors coupled ...
- typeof、constructor和instanceof
在JavaScript中,我们经常使用typeof来判断一个变量的类型,使用格式为:typeof(data)或typeof data.typeof返回的数据类型有六种:number.string.bo ...
- PAT (Advanced Level) 1031. Hello World for U (20)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- 洛谷——P1038 神经网络
P1038 神经网络 题目背景 人工神经网络(Artificial Neural Network)是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款风险评估等诸多领域有广泛的应用.对神 ...
- IOS开发 AFN和ASI
做项目有一段时间了,项目过程中处理网络请求难免的,而对于选择第三方来处理网络请求肯定是个明智的选择! AFNetworking和ASIHTTPRequest 这两个第三方该如何选择 我 ...
- 说说Android应用的persistent属性(转)
1 启动persistent应用 在Android系统中,有一种永久性应用.它们对应的AndroidManifest.xml文件里,会将persistent属性设为true,比如: <appli ...