Mayor's posters POJ - 2528(线段树 + 离散化)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 74745 | Accepted: 21574 |
Description
- 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 解析:
题目大意:给你一个无限长的板子,然后依次往上面贴n张等高的海报,问你最后能看到多少张海报。
思路分析:线段树区间更新问题,但是要注意,给的长度的可能非常大,有1e9,不加处理直接维护一个线段树肯定会
MLE,TLE,但是我们注意到一共最多只有2e4个点,因此我们可以用离散化的思想先对区间进行预处理,所谓的离散化,
在我理解看来就是将一个很大的区间映射为一个很小的区间,而不改变原有的大小覆盖关系,但是注意简单的离散化可能
会出现错误,给出下面两个简单的例子应该能体现普通离散化的缺陷:
例子一:1-10 1-4 5-10
例子二:1-10 1-4 6-10
普通离散化后都变成了[1,4][1,2][3,4]
线段2覆盖了[1,2],线段3覆盖了[3,4],那么线段1是否被完全覆盖掉了呢?
例子一是完全被覆盖掉了,而例子二没有被覆盖
解决的办法则是对于距离大于1的两相邻点,中间再插入一个点,本题还用到了Lazy标记的思想
直接更新区间进行标记而先不对子节点进行处理,如果需要往下更新再将标记下传一层。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cmath>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = , INF = 0x7fffffff;
int t[maxn], a1[maxn], a2[maxn], vis[maxn];
int a, b, x, y, ans;
struct node{
int l, r, w, f;
}Node[maxn*]; void build(int k, int ll, int rr)
{
Node[k].l = ll, Node[k].r = rr;
Node[k].w = ;
if(Node[k].l == Node[k].r) return;
int m = (ll + rr) / ;
build(k*, ll, m);
build(k*+, m+, rr);
} void down(int k)
{
Node[k*].f = Node[k].f;
Node[k*+].f = Node[k].f;
Node[k*].w = Node[k].f;
Node[k*+].w = Node[k].f;
Node[k].f = ;
} void chinter(int k)
{
if(Node[k].l >= a && Node[k].r <= b)
{
Node[k].w = y;
Node[k].f = y;
return;
}
if(Node[k].f) down(k);
int m = (Node[k].l + Node[k].r) / ;
if(a <= m) chinter(k*);
if(b > m) chinter(k*+);
// Node[k].w = Node[k*2].w + Node[k*2+1].w;
} void qp(int k)
{
if(Node[k].l == Node[k].r)
{
ans = Node[k].w;
return;
}
if(Node[k].f) down(k);
int m = (Node[k].l + Node[k].r) / ;
if(a <= m) qp(k*);
else qp(k*+);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
mem(vis, );
int n, cnt = , ret = ;
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%d%d",&a1[i],&a2[i]);
t[cnt++] = a1[i];
t[cnt++] = a2[i];
}
sort(t+, t+cnt);
int m = unique(t+, t+cnt) - (t+);
int len = m;
for(int i=; i<len; i++)
if(t[i+] - t[i] > ) t[++m] = t[i] + ;
sort(t+, t+m+);
build(, , m);
for(int i=; i<=n; i++)
{
a = lower_bound(t+, t+m+, a1[i]) - t;
b = lower_bound(t+, t+m+, a2[i]) - t;
y = i;
chinter();
}
for(int i=; i<=m; i++)
{
a = i;
qp();
// cout<< ans;
if(!vis[ans] && ans != )
{
vis[ans] = ;
ret++;
}
}
// cout<< endl;
printf("%d\n",ret);
}
return ;
}
Mayor's posters POJ - 2528(线段树 + 离散化)的更多相关文章
- Mayor's posters POJ - 2528 线段树(离散化处理大数?)
题意:输入t组数据,输入n代表有n块广告牌,按照顺序贴上去,输入左边和右边到达的地方,问贴完以后还有多少块广告牌可以看到(因为有的被完全覆盖了). 思路:很明显就是线段树更改区间,不过这个区间的跨度有 ...
- Mayor's posters POJ - 2528 线段树区间覆盖
//线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algori ...
- POJ 2528 (线段树 离散化) Mayor's posters
离散化其实就是把所有端点放在一起,然后排序去个重就好了. 比如说去重以后的端点个数为m,那这m个点就构成m-1个小区间.然后给这m-1个小区间编号1~m-1,再用线段树来做就行了. 具体思路是,从最后 ...
- poj 2528(线段树+离散化) 市长的海报
http://poj.org/problem?id=2528 题目大意是市长竞选要贴海报,给出墙的长度和依次张贴的海报的长度区间(参考题目给的图),问最后你能看见的海报有几张 就是有的先贴的海报可能会 ...
- poj 2528 线段树+离散化
题意:在墙上贴一堆海报(只看横坐标,可以抽象成一线段),新海报可以覆盖旧海报.求最后能看到多少张海报 sol:线段树成段更新.铺第i张海报的时候更新sg[i].x~sg[i].y这一段为i. 然而坐标 ...
- poj 2528 线段树 离散化的小技巧
题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报思路:直接搞超时+超内存,需要离散化.离散化简单的来说就是只取我们需要的值来 用,比如说区间[1000,2000],[1990,2012] ...
- poj 2528 线段树区间修改+离散化
Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...
- POJ 2528 Mayor's posters 贴海报 线段树 区间更新
注意离散化!!!线段树的叶子结点代表的是一段!!! 给出下面两个简单的例子应该能体现普通离散化的缺陷: 1-10 1-4 5-10 1-10 1-4 6-10 普通离散化算出来的结果都会是2,但是第二 ...
- Picture POJ - 1177 线段树+离散化+扫描线 求交叉图像周长
参考 https://www.cnblogs.com/null00/archive/2012/04/22/2464876.html #include <stdio.h> #include ...
随机推荐
- SQL Server中删除用户时报错,提示:The database principal owns a schema in the database, and cannot be dropped(转载)
Description of the problem: When you tried to drop a user, you got this message: Error: 15138 The da ...
- CF258D Little Elephant and Broken Sorting/AGC030D Inversion Sum 期望、DP
传送门--Codeforces 传送门--Atcoder 考虑逆序对的产生条件,是存在两个数\(i,j\)满足\(i < j,a_i > a_j\) 故设\(dp_{i,j}\)表示\(a ...
- assert_param函数的用法
在STM32的固件库和提供的例程中,到处都可以见到assert_param()的使用.如果打开任何一个例程中的stm32f10x_conf.h文件,就可以看到实际上assert_param是一个宏定义 ...
- CentOS搭建V~P~N服务,实现虚拟专用网络
什么是V-P-N V-P-N即虚拟专用网络,它的功能是:在公用网络上建立专用网络,进行加密通讯. V-P-N网关通过对数据包的加密和数据包目标地址的转换实现远程访问.V-P-N有多种分类方式,主要是按 ...
- (6)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- AOP框架
AOP 框架基础 要求懂的知识:AOP.Filter.反射(Attribute). 如果直接使用 Polly,那么就会造成业务代码中混杂大量的业务无关代码.我们使用 AOP (如果不了解 AOP,请自 ...
- ExtJS初探:在项目中使用ExtJS
注意:本文写作时间是 2013 年,所讲的 ExtJS 如今早已过时,请勿学习! -------------------------------- 今天ExtJS官网发布了ExtJS最新正式版4.2. ...
- ruby安装及升级
在centos6.x下执行上面的"gem install redis"操作可能会报错,坑很多!默认yum安装的ruby版本是1.8.7,版本太低,需要升级到ruby2.2以上,否则 ...
- Centos下添加静态路由(临时和永久有效)的操作记录
公司IDC机房服务器上部署了一套外网LB环境,默认配置的是外网ip的路由地址,由于要和其他内网机器通信,所以需要配置内网ip的路由地址.整个操作过程,记录如下,以供以后参考学习: 1)内网网卡绑定 [ ...
- this.$http.post ||this.$http.put||vue 获取url参
getClasslist() { this.$http.get('/xxxxx/childlist', { params: { ServiceUnitId: localStorage.getItem( ...
- M2 终审
1.团队成员简介 左边:马腾跃 右边:陈谋 左上:李剑锋 左下:仉伯龙 右:卢惠明 团队成员及博客: 李剑锋: Blog: http://www.cnblogs.com/Po ...