POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】
任意门:http://poj.org/problem?id=2528
Mayor's posters
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 77814 | Accepted: 22404 |
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
Source
题目大意:
贴海报,给出每张海报的起点和终点,问最后能看见多少张海报(后来的海报会把前面的覆盖);
解题思路:
海报的数据范围太大了,如果按照海报的数据范围开线段树会放不下而且浪费内存。所以考虑离散化,但是这不同于单点离散化,这是区间离散化,意思就是说离散化原来数据的同时还要保留他们的区间的性质。(大神的栗子:原数据“1-10, 1-4,6-10”,常规单点离散化之后“1-4,1-2,3-4”,原本不相邻的点变成邻居了,如果继续按照这种错误的数据建树,那么后果是直接失去了区间的性质) 如果我们想缩小这两点的数据范围,但又想保留不相邻两点表示一个区间的性质,区间离散化要这样搞:缩小数据范围就是按原本数据大小进行排序,他们的先后性保持不变,新下标暂时替代他们,而保留不相邻两点的性质嘛,小脑洞:在他们之间加一个值把他们隔离,这样他们排序之后就不会相邻啦。
离散化之后就还是常规的线段树区间更新,但最后求解的区间查询操作如果暴力单点查询找出有几种不同的海报这样太委屈线段树了,这里的查询只要这张海报类型出现了,那么我们的答案肯定就要加加,然后这张海报我们就可以book一下,下次如果见到有book的海报类型也不需要再更新答案了,这些判重的操作可以直接在Query()里面做,这样才能更好的发挥线段树的本领。
AC code:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define INF 0x3f3f3f3f
#define LL long long int
#define lson l, mid, root<<1
#define rson mid+1, r, root<<1|1
using namespace std;
const int MAXN = 1e4+;
struct date
{
int L, R;
}node[MAXN];
int num[MAXN<<];
int lazy_tag[MAXN<<];
bool book[MAXN<<];
int res, cnt;
int read()
{
int f=, res=;char s=getchar();
while(s<''||s>''){if(s=='-')f=-;s=getchar();}
while(s>=''&&s<=''){res=res*+s-'';s=getchar();}
res*=f;
return res;
}
void PushD(int root)
{
if(lazy_tag[root]){
lazy_tag[root<<] = lazy_tag[root];
lazy_tag[root<<|] = lazy_tag[root];
lazy_tag[root] = ;
}
} void Update(int st, int ed, int l, int r, int root, int val)
{
if(st <= l && ed >= r){lazy_tag[root] = val; return;}
PushD(root);
int mid = (l+r)>>;
if(st <= mid) Update(st, ed, lson, val);
if(ed > mid) Update(st, ed, rson, val);
} void Query(int l, int r, int root)
{
if(lazy_tag[root]){
if(!book[lazy_tag[root]]) res++;
book[lazy_tag[root]] = true;
return;
}
if(l == r) return;
int mid = (l+r)>>;
Query(lson); Query(rson);
}
void init()
{
cnt = ; res = ;
memset(lazy_tag, , sizeof(lazy_tag));
memset(book, false, sizeof(book));
}
int main()
{
int T_case, N;
T_case = read();
while(T_case--)
{
init();
N = read();
for(int i = ; i < N; i++){
node[i].L = read();node[i].R = read();
num[cnt++] = node[i].L;
num[cnt++] = node[i].R;
}
sort(num, num+cnt);
cnt = unique(num, num+cnt)-num;
int top = cnt;
for(int i = ; i < top-; i++){
if(num[i+]!=num[i]+) num[cnt++] = num[i]+;
} sort(num, num+cnt);
/*
for(int i = 0; i < cnt; i++)
printf("%d ", num[i]);
puts("");
*/
for(int i = ; i < N; i++){
int st = lower_bound(num, num+cnt, node[i].L)-num;
int ed = lower_bound(num, num+cnt, node[i].R)-num;
Update(st+, ed+, , cnt, , i+);
}
Query(, cnt, );
printf("%d\n", res);
}
return ;
}
POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】的更多相关文章
- POJ - 2528 Mayor's posters (离散化+线段树区间修改)
https://cn.vjudge.net/problem/POJ-2528 题意 给定一些海报,可能相互重叠,告诉你每个海报的宽度(高度都一样的)和先后叠放顺序,问没有被完全盖住的有多少张? 分析 ...
- POJ 2528 Mayor's posters 贴海报 线段树 区间更新
注意离散化!!!线段树的叶子结点代表的是一段!!! 给出下面两个简单的例子应该能体现普通离散化的缺陷: 1-10 1-4 5-10 1-10 1-4 6-10 普通离散化算出来的结果都会是2,但是第二 ...
- Mayor's posters (离散化线段树+对lazy的理解)
题目 题意: n(n<=10000) 个人依次贴海报,给出每张海报所贴的范围 li,ri(1<=li<=ri<=10000000) .求出最后还能看见多少张海报. 思路: 由于 ...
- POJ - 2528Mayor's posters (离散化+线段树区间覆盖)
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...
- [POJ2528]Mayor's posters(离散化+线段树)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 70365 Accepted: 20306 ...
- poj 2528 Mayor’s posters 【离散化】+【线段树】
<题目链接> 题目大意: 往一堵墙上贴海报,依次输出这些海报张贴的范围,这些海报能够相互覆盖,问最后能够看见几张海报? 解题分析: 由于是给出每张海报的区间,所以在这些区间内的很多点可能用 ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
随机推荐
- DB Intro - MongoDB Basic
mongoDB basic from:http://www.tutorialspoint.com/mongodb prject:https://github.com/chenxing12/l4mong ...
- Beam编程系列之Python SDK Quickstart(官网的推荐步骤)
不多说,直接上干货! https://beam.apache.org/get-started/quickstart-py/ Beam编程系列之Java SDK Quickstart(官网的推荐步骤)
- 触发Full GC的时机
由于Full GC的耗时是Minor GC的十倍左右,所以Full GC的频率设计得比Minor GC低得多.现总结一下触发Full GC的情况. 在那些实现了CMS的比较新的虚拟机中,如果配置了-X ...
- a标签的 onclick 和 href 哪个先执行?
以下这种写法,onclick 事件先执行, href 属性下的动作后执行(页面跳转或 javascript 伪链接),如果不想执行 href 属性下的动作,onclick 需要返回 false. &l ...
- WPF 父子窗体联动
问题: 近段时间,由于项目上的一些原因,设计到在WPF项目使用引用COM组件的问题,部分WPF元素浮动在COM组件之上,并且实现拖.停靠.放大等功能(子窗体不要求等比缩放,只要位置跟随主窗体即可),如 ...
- jQuery 菜单小练习(实现点击和移动鼠标效果)
这个代码的练习是点击事件后 如何用jQuery联动的方式找到相关标签 实现的结果是点击菜单一或者菜单二等 会出现相关菜品,并隐藏其他菜品.鼠标移动才菜品上会在右侧框内出现相关菜品的价格.实现特殊的效果 ...
- [转]Show parameter & Table Not exists
本文转自:http://www.cnblogs.com/fangwenyu/archive/2011/01/06/1926774.html 问题描述 在尝试通过show parameter来查看一个参 ...
- 7、侧边栏:Menu
1.单个侧边栏 导航的代码在分析源码的时候已经分析过了,下面只看他的一些应用与方法. /* ---示例代码----*/ <ion-menu [content]="mycontent&q ...
- JavaScript获取url参数
声明:以下内容转自网络 方法一 String.prototype.getUrlString = function(name) { var reg = new RegExp("(^|& ...
- C# ADO.NET面向对象想法
我认为的面向对象就是把各种问题拆分开来 逐一解决, 我想的是先是数据库,到底有什么, 然后新建一个类,类里面先是private的私有的,但是可以有无数个可以连接private的pubilc的属性 可 ...