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 ...
随机推荐
- 看进程的启动时间长度 + vmstat + jstack 应用
1.显示进程已运行的时间 $ ps -e -o pid,comm,etime 2.dd不断的向磁盘写入数据,所以bo的值会骤然提高,而cpu的wait数值也变高,说明由于大量的IO操作,系统的瓶径出现 ...
- java JDK安装教程
JAVA_HOME G:\JDK\java7\jdk1.7.0_80 根据自己的哈 ;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin 然后找到CLASSPATH ...
- highcharts中数据列点击事件
Highcharts.chart('container', { xAxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul ...
- 源码分享篇:使用Python进行QQ批量登录
直接上源码 1 #coding=utf-8 2 __author__ = 'Eagle' 3 import os 4 import time 5 import win32gui 6 import wi ...
- OpenStack中的虚拟机(/dev/mapper/centos-root)进行磁盘扩容
一.虚拟机上先扩展分区: 二.centos系统root登入,新建分区 2.1 [fdisk -l] 最大分区为/dev/sda2,说明新创建的分区将会是sda3(在后面的步骤会进行选择) 2.2 输入 ...
- Ionic app升级插件开发
终于走到了写插件的这个地方了,插件的过程: 1.安装plugman插件,管理我们的程序 npm install -g plugman 2.创建插件项目appUpgrade,cd 到你的目标目录下,执行 ...
- Ionic下的JPush缺少统计代码问题解决方法
用Ionic打包apk后安装到手机,收到缺少统计代码的提示,解决方法如下: 1. 找到了 platforms/android/src/com/ionichina/ioniclub/MainActiov ...
- C# 获取文件MD5值的方法
可用于对比文件是否相同 /// <summary> /// 获取文件MD5值 /// </summary> /// <param name="fileName& ...
- 解决项目无法添加VBIDE问题
为了方便大家引用,我直接把写好的文件放上来,方便大家下载.要注意的是:解压后需要把文件 VBIDE.DLL放入系统的(如果你的系统是32位,那就是 C:\windows\system32\..) 目录 ...
- 对于League of Legends的分析
League of Legends是一款在国内甚至国际上都很受欢迎的一款网络竞技行的游戏.它是由美国Riot Games开发,腾讯游戏运营的英雄对战网游.<英雄联盟>除了即时战略.团队作战 ...