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 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
随机推荐
- Faster RCNN论文解析
Faster R-CNN由一个推荐区域的全卷积网络和Fast R-CNN组成, Fast R-CNN使用推荐区域.整个网络的结构如下: 1.1 区域推荐网络 输入是一张图片(任意大小), 输出是目标推 ...
- metamask注记词
leaf orbit poet zebra toy day put dinosaur review cool pluck throw(m) 一个钱包地址 里面有多个账号 菲苾代表了不同网络
- 4. hadoop启动脚本分析
4. hadoop启动脚本分析 1. hadoop的端口 ``` 50070 //namenode http port 50075 //datanode http port 50090 //2name ...
- Thunder团队第三周 - Scrum会议2
Scrum会议2 小组名称:Thunder 项目名称:i阅app Scrum Master:李传康 工作照片: 胡佑蓉在拍照,所以不在照片中. 参会成员: 王航:http://www.cnblogs. ...
- 项目--uml
[团队信息] 团队项目: 小葵日记--主打记录与分享模式的日记app 队名:日不落战队 队员信息及贡献分比例: 短学号 名 本次作业博客链接 此次作业任务 贡献分配 备注 501 安琪 http:// ...
- 某一线互联网公司前端面试题总结css部分
1,css3选择器 :not(selector) 选择页面内所有type!=text的类型: input:not([type=text]){ color: red; font-weight: bold ...
- my.conf 修改编码
mysql汉字乱码的原因 mysql默认的编码是Latin1是I-8859-1的别名,但Latin1是不支持汉字的,所以要将其改为UTF-8或GBK 1.关闭mysql服务器,这个很重要. 2.通过m ...
- Java中的日志——Java.util.logging、log4j、commons-logging
Java中给项目程序添加log主要有三种方式,一使用JDK中的java.util.logging包,一种是log4j,一种是commons-logging.其中log4j和commons-loggin ...
- BZOJ3167/BZOJ4824 HEOI2013SAO/CQOI2017老C的键盘(树形dp)
前者是后者各方面的强化版. 容易想到设f[i][j]表示i子树中第j小的是i的方案数(即只考虑相对关系).比较麻烦的在于转移.考虑逐个合并子树.容易想到枚举根原来的排名和子树根原来的排名,算一发组合数 ...
- Javascript基础之-强制类型转换(二)
思考下面这个问题: console.log(+"123"); // 123 console.log(-"123"); // -123 console.log(+ ...