POJ2528 线段树离散化
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 62771 | Accepted: 18120 |
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,1000],[500,2000],[1500,2500].那么
//把所有区间端点1,500,1000,1500,2000,2500离散化后就是1,2,3,4,5,6.离散化
//后所得区间为:[1,3],[2,5],[4,6].可以知道离散化前可见区间有3个,但是离散
//化后只有区间[1,3]和区间[4,6]可见.所以离散化一的方式是有问题的
//离散化二:对于区间端点的离散化,如果离散化之前相邻的两个数不是类似于a与a+1的差距1关系,
//那么就自动在后面的这个数的离散化结果上加1.比如:[1,10],[1,5],[7,10] 离散化后
//的区间为[1,7][1,3],[5,7]
//离散化方式二主要就是让本来不相邻的数继续保持不相邻即可. //离散化之后用二分查找对应的离散化后的点,本题val数组要开大。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=;
bool vis[maxn*+];
int mp[maxn*+],val[maxn*+],t,n,ans;
struct node
{
int l,r;
}nodes[maxn*+];
int Bsearch(int a,int b,int *c)
{
int l=,r=b-,mid;
while(l<=r){
mid=(l+r)>>;
if(c[mid]==a) return mid;
else if(c[mid]<a) l=mid+;
else r=mid-;
}
return -;
}
void Pushdown(int rt)
{
if(val[rt]>=){
val[rt<<]=val[rt<<|]=val[rt];
}
val[rt]=-;
}
void Update(int ql,int qr,int c,int l,int r,int rt)
{
if(ql<=l&&qr>=r){
val[rt]=c;
return ;
}
Pushdown(rt);
int m=(l+r)>>;
if(ql<=m) Update(ql,qr,c,l,m,rt<<);
if(qr>m) Update(ql,qr,c,m+,r,rt<<|);
}
void Query(int l,int r,int rt)
{
if(val[rt]>=){
if(!vis[val[rt]]) ans++;
vis[val[rt]]=;
return;
}
if(l==r) return;
Pushdown(rt);
int m=(l+r)>>;
Query(l,m,rt<<);
Query(m+,r,rt<<|);
}
int main()
{
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int m=;
for(int i=;i<n;i++){
scanf("%d%d",&nodes[i].l,&nodes[i].r);
mp[m++]=nodes[i].l;mp[m++]=nodes[i].r;
}
sort(mp,mp+m);
m=unique(mp,mp+m)-mp;//去重,加入相关的点
for(int i=m-;i>=;i--){
if(mp[i]-mp[i-]>) mp[m++]=mp[i]-;
}
sort(mp,mp+m);
memset(vis,,sizeof(vis));
memset(val,-,sizeof(val));
for(int i=;i<n;i++){
nodes[i].l=Bsearch(nodes[i].l,m,mp);
nodes[i].r=Bsearch(nodes[i].r,m,mp);
Update(nodes[i].l,nodes[i].r,i,,m-,);
}
ans=;
Query(,m-,);
printf("%d\n",ans);
}
return ;
}
POJ2528 线段树离散化的更多相关文章
- poj2528(线段树+离散化)Mayor's posters
2016-08-15 题意:一面墙,往上面贴海报,后面贴的可以覆盖前面贴的.问最后能看见几种海报. 思路:可以理解成往墙上涂颜色,最后能看见几种颜色(下面就是以涂色来讲的).这面墙长度为1~1000 ...
- poj2528 线段树+离散化 (倒序)
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...
- poj2528 线段树+离散化
由于坐标可能很大,此时需要离散化,将值转化为对应的坐标. #include<stdio.h> #include<algorithm> using namespace std; ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- poj-2528线段树练习
title: poj-2528线段树练习 date: 2018-10-13 13:45:09 tags: acm 刷题 categories: ACM-线段树 概述 这道题坑了我好久啊啊啊啊,,,, ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- [UESTC1059]秋实大哥与小朋友(线段树, 离散化)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
随机推荐
- ionic ios样式偏移解决方案。
在css属性内增加: .item-ios [item-end] { //解决ios系统上尾部图标出现重影而增加的格式. margin: 0px -15.3px 0px 0px; margin-bott ...
- Hexo 博客 之 腾讯云部署过程
写在前面 Hexo 博客搭好了有差不多两周时间了,这期间走了很多弯路,跳了很多坑.一些坑自己 bing 到了答案,找到了解决方法,一些坑则是自己摸索出来的解决方法.现在准备写几篇关于搭建流程.搭建过程 ...
- [Data Structures and Algorithms - 1] Introduction & Mathematics
References: 1. Stanford University CS97SI by Jaehyun Park 2. Introduction to Algorithms 3. Kuangbin' ...
- JS验证验证服务器控件
JS验证验证服务器控件 <script language="javascript" type="text/javascript"> /******* ...
- JavaScript筑基篇(三)->JS原型和原型链的理解
删除理由:很久以前写的,当时理解不够深入,这样描述反而看起来更复杂了.因此就删掉,免得误人子弟! 可以看看另一篇文章:[如何继承Date对象?由一道题彻底弄懂JS继承.](http://www.cnb ...
- JDK中的泛型
Java中的泛型介绍: 起因: 1. JDK 1.4 以前类型不明确: ① 装入集合的对象被当作 Object 类型对待,从而失去了自己的原有类型: ② 从集合中取出时往往需要转型,效率低下,并且容易 ...
- LintCode-105.复制带随机指针的链表
复制带随机指针的链表 给出一个链表,每个节点包含一个额外增加的随机指针可以指向链表中的任何节点或空的节点. 返回一个深拷贝的链表. 挑战 可否使用O(1)的空间 标签 哈希表 链表 优步 code / ...
- 大型网站架构演化(六)——使用反向代理和CDN加速网站响应
随着网站业务不断发展,用户规模越来越大,由于中国复杂的网络环境,不同地区的用户访问网站时,速度差别也极大.有研究表明,网站访问延迟和用户流失率正相关,网站访问越慢,用户越容易失去耐心而离开.为了提供更 ...
- win10 64位系统中安装多个jdk版本的切换问题
前言: 近期要更换oracle jdk到zulu jdk,因此在本地先安装一版zulu的来进行代码的编译和比较. 注释: 本地电脑之前是oracle jdk 1.8,要更换为zulu jdk 1.8. ...
- SQL SERVER技术内幕之10 可编程对象
一.变量 变量用于临时保存数据值,以供在声明它们的同一批处理语句中引用.例如,以下代码先声明一个数据类型为INT的变量@i,再将它赋值为10; DECLARE @i as INT; SET @i = ...