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. Invalid environment specified: http://datatables.org/alltables.env

    获取Yahoo股票的API会报错:http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes ...

  2. code force 424 A - Office Keys

    There are n people and k keys on a straight line. Every person wants to get to the office which is l ...

  3. poj 2345 Central heating

    Central heating Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 678   Accepted: 310 Des ...

  4. 0_Simple__matrixMul + 0_Simple__matrixMul_nvrtc

    矩阵乘法,使用一维线程块和共享内存.并且在静态代码和运行时编译两种条件下使用. ▶ 源代码:静态使用 #include <stdio.h> #include <assert.h> ...

  5. js中的事件缓存机制

    异步任务指的是,不进入主线程.而进入"任务队列"(task queue)的任务,只有"任务队列"通知主线程,某个异步任务可以执行了,该任务才会进入主线程执行. ...

  6. 区分window8中 ie10 window phone8

    Internet Explorer 10 并没有对 屏幕的宽度 和 视口(viewport)的宽度 进行区分   @-webkit-viewport   { width: device-width; ...

  7. linux-cp

    cp 更改时间: 2017-10-26 - 21:00:54 cp:用来复制文件或者目录的命令,当源文件与目标文件名字相同的时候,当cp 没有参数,源文件会覆盖目标文件 参数 -p:保留源文件或者目录 ...

  8. 一起写框架-Ioc内核容器的实现-基础功能-容器对象名默认首字母小写(八)

    实现功能 --前面实现的代码-- 默认的对象名就类名.不符合Java的命名规范.我们希望默认的对象名首字母小写. 实现思路 创建一个命名规则的帮助类.实现将对大写开头的对象名修改为小写开头. 实现步骤 ...

  9. UWP Composition API - RadialMenu

    用Windows 8.1的童鞋应该知道OneNote里面有一个RadialMenu.如下图,下图是WIn10应用Drawboard PDF的RadialMenu,Win8.1的机器不好找了.哈哈,由于 ...

  10. SSM框架搭建(Spring+SpringMVC+MyBatis)与easyui集成并实现增删改查实现

    一.用myEclipse初始化Web项目 新建一个web project: 二.创建包 controller        //控制类 service //服务接口 service.impl //服务 ...