poj 2528 Mayor's posters(线段树+离散化)
/*
poj 2528 Mayor's posters
线段树 + 离散化 离散化的理解:
给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显
会导致内存的耗尽。所以我们做一个映射关系,将范围很大的数据映射到范围很小的数据上
1---->1 4----->2 100----->3 1000000000----->4
这样就会减少内存一些不必要的消耗
建立好映射关系了,接着就是利用线段树求解
*/
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#define N 10000010
#define M 10005
using namespace std;
class EDGE{
public:
int ld, rd;
};
int tree[M*];//一共有M*2个端点,一个线段映射到四个点,左右端点, 左端点-1, 右端点+1, 数组的大小是线段树最底层数据个数的4倍
EDGE edge[M];
int p[M*];
int hash[N];
int n; int insert(int p, int lr, int rr, int ld, int rd){ if(tree[p] && lr<=ld && rd<=rr)//如果当前的区间[ld, rd]被包含在[lr, rr]中,而且[lr, rr]的区间已经被覆盖
return ;
else if(lr==ld && rr==rd){
tree[p]=;
return ;
}
else{
int mid=(lr+rr)>>;
int f1, f2, f3, f4;
if(mid>=rd)
f1=insert(p<<, lr, mid, ld, rd);
else if(mid<ld)
f2=insert(p<<|, mid+, rr, ld, rd);
else{
f3=insert(p<<, lr, mid, ld, mid);
f4=insert(p<<|, mid+, rr, mid+, rd);
}
tree[p]=tree[p<<] && tree[p<<|];//两个子树都被覆盖的时候父类才会被覆盖
if(mid>=rd)
return f1;
else if(mid<ld)
return f2;
else return f3 && f4;
}
}
/*
3
1 10
1 3
6 10
如果将一个线段离散化成两个点,输出地结果是2
如果是四个节点,输出的结果就是3
而正确的结果就是3
*/ int main(){
int t, i, nm;
scanf("%d", &t);
while(t--){
int maxR=;
scanf("%d", &n);
for(i=; i<n; ++i){
scanf("%d%d", &edge[i].ld, &edge[i].rd);
p[maxR++]=edge[i].ld-;
p[maxR++]=edge[i].ld;
p[maxR++]=edge[i].rd;
p[maxR++]=edge[i].rd+;
}
sort(p, p+maxR);
maxR=unique(p, p+maxR)-p;//元素去重
for(i=, nm=; i<maxR; ++i){
hash[p[i]]=++nm;
}
memset(tree, , sizeof(tree));//初始值是所有的点都没有被覆盖
int ans=;
for(i=n-; i>=; --i){//由外向里看真是个不错的主意
if(!insert(, , nm, hash[edge[i].ld], hash[edge[i].rd]))
++ans;
}
printf("%d\n", ans);
}
return ;
}
poj 2528 Mayor's posters(线段树+离散化)的更多相关文章
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化
Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...
- POJ 2528 Mayor's posters (线段树+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions:75394 Accepted: 21747 ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- POJ 2528 Mayor’s posters (线段树段替换 && 离散化)
题意 : 在墙上贴海报, n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000).求出最后还能看见多少张海报. 分析 ...
- poj 2528 Mayor's posters 线段树区间更新
Mayor's posters Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...
- poj 2528 Mayor's posters(线段树)
题目:http://poj.org/problem?id=2528 题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度.现在往墙上贴N张海报,每张海报的宽度是任意的, 但是必定是单位宽度的整数 ...
- POJ 2528 Mayor's posters (线段树)
题目链接:http://poj.org/problem?id=2528 题目大意:有一个很上的面板, 往上面贴海报, 问最后最多有多少个海报没有被完全覆盖 解题思路:将贴海报倒着想, 对于每一张海报只 ...
随机推荐
- 【转载】Recommendations with Thompson Sampling (Part II)
[原文链接:http://engineering.richrelevance.com/recommendations-thompson-sampling/.] [本文链接:http://www.cnb ...
- delphi 读写文本
将字符串写入txt文档,读取txt文档中的内容. //一次写字符串到文本文件,每次都会将原来的内容替换掉. procedure FilePutContents(f,s:String); // f为文件 ...
- CPU与内存的关系
至今才对cpu和内存有一定了解了,下面有几个比喻的理解,很形象呦~ 1# 例如你要吃东西时,硬盘是锅,内存是碗,CPU是你,浅显点就是这样子了~ 2# 例如电脑是企业,内存是车间,cpu是生产线,硬盘 ...
- Python小爬虫练习
# coding: utf-8 __author__ = 'zhangcx' from urllib3 import PoolManager import codecs import json cla ...
- 【TextBox】重写右键菜单
参考资料 http://bbs.csdn.net/topics/390324356 http://www.cnblogs.com/ycxy/archive/2012/10/09/2716852.htm ...
- 负载均衡算法(四)IP Hash负载均衡算法
/// <summary> /// IP Hash负载均衡算法 /// </summary> public static class IpHash { static Dicti ...
- RCP:给GEF编辑器添加拖拽辅助线
当图形边缘碰触时,会产生一条帮助拖拽的辅助线 这里需要三个类: 1.SnapToGeomotry 2.SnapToGuide(非必须) 3.SnapFeedbackPolicy
- Java多线程3:Thread中的静态方法
Thread类中的静态方法 Thread类中的静态方法表示操作的线程是"正在执行静态方法所在的代码块的线程".为什么Thread类中要有静态方法,这样就能对CPU当前正在运行的线程 ...
- git 合并分支
当前git有PreRelease和Release两个分支,现在需要将前者合并到后者. Clone版本 在本地clone一个目标工程. 然后右键 Tortoise-->Switch/Checkou ...
- Portal.MVC —— nopcommerce的简化版
Portal.MVC 简介 项目是基于MVC4+EF,带有角色,权限,用户中心及账户相关(登录,注册,修改密码,找回密码等)等基本功能.参考的开源项目 nopcommerce,这是一个电商架构的MVC ...