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 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
随机推荐
- day21 TFRecord格式转换MNIST并显示
首先简要介绍了下TFRecord格式以及内部实现protobuf协议,然后基于TFRecord格式,对MNIST数据集转换成TFRecord格式,写入本地磁盘文件,再从磁盘文件读取,通过pyplot模 ...
- 关于LNMP常见问题和性能方面的个人理解
简单整理,自己做备忘的,不为其他作任何参考- PHP程序 1.开启慢日志,过滤超时时间为1s的方法,针对性优化,可以通过添加缓存方式解决. 2.过滤access日志,统计哪些请求较多较为频繁,是否合理 ...
- 《剑指Offer》题一~题十
一.赋值运算符函数 题目:如下为类型CMyString的声明,请为该类型添加赋值运算符函数. class CMyString { public: CMyString(char *pData = nul ...
- ACM 第十九天
积性函数 积性函数线性筛,筛素数,u(n),欧拉函数: vis[]=vis[]=,mu[]=,phi[]=; ;i<=N;++i){ ,phi[i]=i-,prime[++cnt]=i; ,k= ...
- redis切换数据库的方法【jedis】
package com.test; import redis.clients.jedis.Jedis; public class readredis { public static void main ...
- wpf拖拽
简单拖拽的实现是,实现源控件的MouseDown事件,和目标控件Drop事件.调用DragDrop.DoDragDrop()以启动拖放操作,DragDrop.DoDragDrop()函数接受三个参数: ...
- Codeforces 1025D(区间dp)
容易想到设f[i][j][k]为i~j区间以k为根是否能构成bst.这样是O(n4)的.考虑将状态改为f[i][j][0/1]表示i~j区间以i-1/j+1为根能否构成bst.显然如果是i-1作为根的 ...
- CF546E Soldier and Traveling
题目描述 In the country there are n n n cities and m m m bidirectional roads between them. Each city has ...
- 使用thymeleaf实现div中加载html
目标:固定顶部或者左侧导航,点击导航动态更新中间content区域的页面,也就是在放一个div在页面上,把html加载到div里,以前类似的实现都是通过Iframe或者js实现,在使用springbo ...
- POJ3469:Dual Core CPU——题解
http://poj.org/problem?id=3469 题目大意: 两个CPU,处理每个任务有不同的代价,有些对任务如果不在同一个CPU就会增加代价,求最小代价. ——————————————— ...