POJ2528 Mayor's posters(线段树染色问题+离散化)
题目大意:有t组数据,每组数据给你n张海报(1<=n<=10000),下面n组数据分别给出每张海报的左右范围(1 <= l <= r <= 10000000),下一张海报会覆盖前一张海报,求最后可见(包括完全和不完全可见)的海报有几张。
例如:
1
5
1 4
2 6
8 10
3 4
7 10

如上图所示,答案为4。
解题思路:其实这是一道区间染色问题,但是由于查找区间太大,显然直接建树会导致MLE,所以这里通过使用对区间的离散化来缩小查找范围。参考了一些大牛博客,简单说一下。
通俗点说,离散化就是压缩区间,使原有的长区间映射到新的短区间,但是区间压缩前后的覆盖关系不变。举个例子:
有一条1到10的数轴(长度为9),给定4个区间[2,4] [3,6] [8,10] [6,9]。现在我们抽取这4个区间的8个端点,2 4 3 6 8 10 6 9。删除相同点并对其升序排序,得2 3 4 6 8 9 10
然后建立映射
2 3 4 6 8 9 10
↓ ↓ ↓ ↓ ↓ ↓ ↓
1 2 3 4 5 6 7
那么新的4个区间为 [1,3] [2,4] [5,7] [4,6],覆盖关系没有被改变。新数轴为1到7,即原数轴的长度从9压缩到6。
但是对于这道题目来说,这样简单的离散化是不行的,比如三张海报为:1~10、1~4、7~10,就有区间[1,10],[1,4],[7,10]。
按照上述方法离散化后的到区间[1,4],[1,2],[3,4]。答案为2,实际上答案为3。
因为上述做法忽略了区间[2,3]的存在,也就是原来的5~6这一块。
新的离散方法为:在相差大于1的数间加一个数(我选择加入两个数的中间值),这样的含义是用一个数字来表示其中间的那块区间,解决某块区间被忽略的问题。
例如在上面1 4 6 10中,
X[ 1 ] = 1, X[ 2 ]=2,X[ 3 ] = 4,X[ 4 ] = 5, X[ 5 ] = 7,X[ 6 ]=8, X[ 7 ] = 10
这样之后,第一次是1~7被染成1;第二次1~3被染成2;第三次5~7被染成3
最终,1~3为2,4为1,5~7为3,于是输出正确结果3。
代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#define LC(a) ((a<<1))
#define RC(a) ((a<<1)+1)
#define MID(a,b) ((a+b)>>1)
using namespace std;
typedef long long ll;
const int N=5e4*; struct node{
ll l,r;
ll color;//颜色为-1表示混合色
}tree[N]; struct knode{
ll l,r;//用来存储题目给定的区间
}tmp[N]; ll ans[N];//记录颜色i是否存在
ll Hash[N];//存储坐标映射
ll point[N];//存储坐标序列
//下推
void pushdown(ll p){
tree[RC(p)].color=tree[LC(p)].color=tree[p].color;
}
//上推
void pushup(ll p){
tree[p].color=(tree[LC(p)].color==tree[RC(p)].color?tree[LC(p)].color:-);
} void build(ll p,ll l,ll r){
tree[p].l=l;
tree[p].r=r;
tree[p].color=;
if(l==r){
return;
}
build(LC(p),l,MID(l,r));
build(RC(p),MID(l,r)+,r);
} void update(ll p,ll l,ll r,ll color){
if(r<tree[p].l||l>tree[p].r)
return;
if(l<=tree[p].l&&r>=tree[p].r){
tree[p].color=color;
return;
}
//**释放lazy标记
if(tree[p].color!=-){
pushdown(p);
}
update(LC(p),l,r,color);
update(RC(p),l,r,color);
pushup(p);
} void query(ll p,ll l,ll r){
if(r<tree[p].l||l>tree[p].r)
return;
//纯色,不用再找下去
if(tree[p].color!=-){
ans[tree[p].color]=;
return;
}
query(LC(p),l,r);
query(RC(p),l,r);
} //二分查找
ll Bin_search(ll l,ll r,ll x){
ll mid;
while(l<=r){
mid=(l+r)/;
if(x==Hash[mid]) return mid;
if(x<Hash[mid]) r=mid-;
else if(x>Hash[mid]) l=mid+;
}
return -;
} int main(){
ios::sync_with_stdio(false);
ll t,n;
cin>>t;
while(t--){
cin>>n;
//***对输入数据进行离散化
ll m1=; for(int i=;i<=n;i++){
ll l,r;
cin>>tmp[i].l>>tmp[i].r;
point[++m1]=tmp[i].l;
point[++m1]=tmp[i].r;
}
sort(point+,point++m1);
ll m2=;
for(int i=;i<=m1;i++){
if(point[i]!=point[i-]){
if(point[i]-point[i-]>&&i!=){
Hash[++m2]=(point[i]+point[i-])>>;
Hash[++m2]=point[i];
}
else
Hash[++m2]=point[i];
}
}
//**建树
build(,,m2);
for(int i=;i<=n;i++){
ll l=Bin_search(,m2,tmp[i].l);
ll r=Bin_search(,m2,tmp[i].r);
update(,l,r,i);
}
memset(ans,,sizeof(ans));
query(,,m2);
ll cnt=;
for(int i=;i<=m2;i++){
if(ans[i]){
cnt++;
}
}
cout<<cnt<<endl;
}
}
POJ2528 Mayor's posters(线段树染色问题+离散化)的更多相关文章
- POJ2528 Mayor's posters —— 线段树染色 + 离散化
题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...
- POJ2528:Mayor's posters(线段树区间更新+离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- 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(线段树之成段更新)
Mayor's posters Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 37346Accepted: 10864 Descr ...
- 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: 1000MS Memory Limit: 65536K Total Submissions: 50888 Accepted: 14737 ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- POJ 2528 Mayor’s posters (线段树段替换 && 离散化)
题意 : 在墙上贴海报, n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000).求出最后还能看见多少张海报. 分析 ...
- POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)
题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...
随机推荐
- [学习笔记]FHQ-Treap及其可持久化
感觉范浩强真的巨 博主只刷了模板所以就讲基础 fhq-treap 又形象的称为非旋转treap 顾名思义 保留了treap的随机数堆的特点,并以此作为复杂度正确的条件 并且所有的实现不用旋转! 思路自 ...
- 传说中的 SonarLint
Sonar是一个用于代码质量管理的开源平台,用于管理源代码的质量 通过插件形式,可以支持包括java,C#,C/C++,PL/SQL,Cobol,JavaScrip,Groovy等等二十几种编程语言的 ...
- LAMP安全加固
LAMP安全: 1.BIOS:设置BIOS密码,禁用从CD-ROM和软盘引导 2.SSH安全:修改/etc/ssh/sshd_configPermitRootLogin = no //禁止root访问 ...
- vim,删除所有
vim 删除所有内容:方法1: 按ggdG方法2: :%d
- libxml2在mingw下编译
1.配置MingW路径,在环境变量path中加入/mingw32/bin2.解压libxml,进入win32目录3.记事本打开configure.js,找到var compiler = "m ...
- 枚举工具类 EnumUtils
这是一个枚举工具类,有时候一个枚举自身为空,我们程序在调用他的方法时(如:name(),ordinal()),就会报空指针异常:下面写这个工具类就是为了来避免这些问题的,有些外部引用的没有贴出来,不过 ...
- AJAX 状态值与状态码详解
1- AJAX状态值与状态码区别 AJAX状态值是指,运行AJAX所经历过的几种状态,无论访问是否成功都将响应的步骤,可以理解成为AJAX运行步骤.如:正在发送,正在响应等,由AJAX对象与服务器交互 ...
- 2017 Multi-University Training Contest - Team 1
1006(6038) 就是对a,b分别求循环节,先统计一下b中所有长度循环节的出现次数,再对a求循环节时只要满足: a的循环节长度 % b的循环节长度=0,那么这个b的循环节就可以计入答案,尼玛只要是 ...
- 将oh-my-zsh编程真正的my zsh
环境: Ubuntu 32位 oh-my-zsh安装: 1.安装zsh: sudo apt-get install zsh 2.将当前用户的shell环境修改为zsh: chsh -s /bin/z ...
- 原生js写的一个简单slider
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...