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 线段树+离散化技巧的更多相关文章

  1. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  2. poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

  3. POJ 2528 Mayor's posters (线段树+离散化)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:75394   Accepted: 21747 ...

  4. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  5. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  6. POJ 2528 Mayor’s posters (线段树段替换 && 离散化)

    题意 : 在墙上贴海报, n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000).求出最后还能看见多少张海报. 分析 ...

  7. poj 2528 Mayor's posters 线段树区间更新

    Mayor's posters Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...

  8. poj 2528 Mayor's posters(线段树)

    题目:http://poj.org/problem?id=2528 题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度.现在往墙上贴N张海报,每张海报的宽度是任意的, 但是必定是单位宽度的整数 ...

  9. POJ 2528 Mayor's posters (线段树)

    题目链接:http://poj.org/problem?id=2528 题目大意:有一个很上的面板, 往上面贴海报, 问最后最多有多少个海报没有被完全覆盖 解题思路:将贴海报倒着想, 对于每一张海报只 ...

随机推荐

  1. PHP常用配置

    Php配置文件:php.ini(使用‘;’表示注释) Php的配置项可以在配置文件中配置,也可以在脚本中使用ini_set()函数临时配置. 语言相关配置: 1. engine:设置PHP引擎是否可用 ...

  2. Memcached理解

    一.Memcached简介: 1. Memcached是一个自由开源的,高性能的,分布式内存对象缓存系统. 2. Memcached是一种基于内存的key-value存储,用来存储小块的任意数据(字符 ...

  3. LeetCode 339. Nested List Weight Sum (嵌套列表重和)$

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  4. Vue源码终笔-VNode更新与diff算法初探

    写完这个就差不多了,准备干新项目了. 确实挺不擅长写东西,感觉都是罗列代码写点注释的感觉,这篇就简单阐述一下数据变动时DOM是如何更新的,主要讲解下其中的diff算法. 先来个正常的html模板: & ...

  5. js判断值为null

    今天在做项目的时候,犯了一个着实不应该的错误,拿到data为null,然后判断如果为null执行A,否则执行B 我错误的代码是 if(data===null){ A; }else{ B; } 怎么调试 ...

  6. java中的缓冲流BufferedWriter和BufferedReader

    java中的缓冲流有BufferedWriter和BufferedReader 在java api 手册中这样说缓冲流: 从字符输入流中读取文本,缓冲各个字符,从而实现字符.数组和行的高效读取.可以指 ...

  7. Linux替换命令

    :s/^.*$/\L&/100 ##将100行内的小写转换成大写 vi/vim 中可以使用 :s 命令来替换字符串. :s/vivian/sky/ 替换当前行第一个 vivian 为 sky ...

  8. JavaScript系列-----对象基于哈希存储(<Key,Value>之Value篇) (3)

    JavaScript系列-----Objectj基于哈希存储<Key,Value>之Value 1.问题提出 在JavaScript系列-----Object之基于Hash<Key, ...

  9. Android开发之漫漫长途 Ⅳ——Activity的显示之ViewRootImpl初探

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  10. 如何编写通用的 Helper Class

    Github: https://github.com/nzbin/snack-helper Docs: https://nzbin.github.io/snack-helper 前言 什么是 help ...