poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters
题目链接:
http://poj.org/problem?id=2528
思路:
线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来)
假设给出:
1~10
1~4
7-10
最后可以看见三张海报
如果离散化的时候不注意,就会变成
1 4 7 10(原始)
1 2 3 4 (离散化)
转化为:
1~4
1~2
3~4
这样的话最后只能看见两张海报
解决办法,如果原数据去重排序后相互之间差值大于1,则在他们之间再插入一个数值,使得大于前者小于后者即可
我感觉这道题目用线段树还不够快,因为最后查询,遍历了整整一棵树...
代码:
#include <iostream>
#include <stdio.h>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 10000005;
struct node {
int l,r,color,lazy;
inline void update(int val) {
color=val;
lazy=val;
}
} tr[maxn*4];
int a[maxn],b[maxn],c[maxn*2];
set<int> se;
inline void push_down(int s) {
int lazyval = tr[s].lazy;
if(!lazyval) return;
tr[s<<1].update(lazyval);
tr[s<<1|1].update(lazyval);
tr[s].lazy=0;
}
void build(int s, int l, int r) {
tr[s].l=l;tr[s].r=r;
tr[s].lazy=tr[s].color=0;
if(l==r) return;
int mid=(l+r)>>1;
build(s<<1,l,mid);
build(s<<1|1,mid+1,r);
}
void update(int s, int l, int r, int color) {
if(tr[s].l==l&&tr[s].r==r) {
tr[s].update(color);
return;
}
push_down(s);
int mid=(tr[s].l+tr[s].r)>>1;
if(r<=mid) update(s<<1,l,r,color);
else if(l>mid) update(s<<1|1,l,r,color);
else {
update(s<<1,l,mid,color);
update(s<<1|1,mid+1,r,color);
}
}
void query(int s) {
if(tr[s].l==tr[s].r) {
if(!tr[s].color) return;
se.insert(tr[s].color);
return;
}
push_down(s);
query(s<<1);
query(s<<1|1);
}
inline void init() {
se.clear();
}
int main() {
int t,m,tot,index;
scanf("%d",&t);
while(t--) {
init();
tot=1;
scanf("%d",&m);
for(int i=1;i<=m;++i) {
scanf("%d %d",&a[i],&b[i]);
c[tot]=a[i];++tot;
c[tot]=b[i];++tot;
}
/*离散化开始*/
index=tot;
--tot;
sort(c+1,c+index);
for(int i=2;i<=tot;++i) {
if(c[i]-c[i-1]>1) {
c[index]=c[i-1]+1;
index++;
}
}
sort(c+1,c+index);
tot=unique(c+1,c+index)-c;
--tot;
build(1,1,tot);
for(int i=1;i<=m;++i){
a[i]=lower_bound(c+1,c+tot+1,a[i])-(c+1)+1;
b[i]=lower_bound(c+1,c+tot+1,b[i])-(c+1)+1;
update(1,a[i],b[i],i);
}
/*离散化完成*/
query(1);
printf("%d\n",se.size());
}
return 0;
}
poj 2528 Mayor's posters 线段树+离散化技巧的更多相关文章
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化
Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...
- POJ 2528 Mayor's posters (线段树+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions:75394 Accepted: 21747 ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- 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 线段树区间更新
Mayor's posters Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...
- poj 2528 Mayor's posters(线段树)
题目:http://poj.org/problem?id=2528 题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度.现在往墙上贴N张海报,每张海报的宽度是任意的, 但是必定是单位宽度的整数 ...
- POJ 2528 Mayor's posters (线段树)
题目链接:http://poj.org/problem?id=2528 题目大意:有一个很上的面板, 往上面贴海报, 问最后最多有多少个海报没有被完全覆盖 解题思路:将贴海报倒着想, 对于每一张海报只 ...
随机推荐
- Jquery的树插件jqxTreeGrid的使用小结
一.引入相应的js <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" t ...
- MongoDB全文检索
1. 全文检索概念: 全文检索是对每一个词建立一个索引,指明该词在文章中出现的次数和位置,当用户查询时,检索程序就根据事先建立的索引进行查找,并将查找的结果反馈给用户的检索方式. (暂时不支持中文) ...
- LeetCode 226. Invert Binary Tree (反转二叉树)
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- [PGM] Bayes Network and Conditional Independence
2 - 1 - Semantics & Factorization 2 - 2 - Reasoning Patterns 2 - 3 - Flow of Probabilistic Influ ...
- 火狐浏览器打开html文件,中文乱码
当html文件为: 解决方法: 将 <meta http-equiv="Content-Type" content="text/html; charset=UT ...
- Vue源码后记-vFor列表渲染(1)
钩子函数比较简单,没有什么意思,这一节搞点大事情 => 源码中v-for的渲染过程. vue的内置指令包含了v-html.v-if.v-once.v-bind.v-on.v-show等,先从一个 ...
- 版本控制之一:SVN服务器搭建与安装(转)
Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上了,下载地址: http:// ...
- Vue.js之render函数基础
刚才翻了一下博客,才发现,距离自己写的第一篇Vue的博客vue.js之绑定class和style(2016-10-30)已经过去一年零两天.这一年里,自己从船厂的普通技术员,成为了一个微型不靠谱创业公 ...
- [Scikit-learn] 1.2 Dimensionality reduction - Linear and Quadratic Discriminant Analysis
Ref: http://scikit-learn.org/stable/modules/lda_qda.html Ref: http://bluewhale.cc/2016-04-10/linear- ...
- 0_Simple__matrixMulCUBLAS
使用CUDA的线性代数库cuBLAS来计算矩阵乘法.这里主要记录调用规则,关于乘法函数中详细的参数说明和调用规则见另一篇随笔. ▶ 源代码: #include <assert.h> #in ...