POJ-2528 Mayor's posters (离散化, 线段树)
题目传送门: POJ-2528
题意就是在一个高度固定的墙面上贴高度相同宽度不同的海报,问贴到最后还能看到几张?本质上是线段树区间更新问题,但是要注意的是题中所给数据范围庞大,直接搞肯定会搞出问题,所以要离散化,而离散化的过程中要注意一个问题,比方说1-10,1-5,6-10,本来是可以三张海报都可以看见的,但是按照题意来看是看不到的,因为他是一个点代表一个单位长度(诡异>_<),解决的办法则是对于距离大于1的两相邻点,中间再插入一个点......
代码如下,我用了vector,和用数组的相差大改50ms左右,如果时间不是特别紧的话,完全可以用vector,还可以借助STL来节省时间(懒???>_<)......
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio> using namespace std;
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1 | 1 const int N = + ;
int col[N<<], li[N], ri[N],Hash[N], cnt;
vector<int> x; void PushDown(int rt){
if(col[rt] != -){
col[rt<<] = col[rt<<|] = col[rt];
col[rt] = -;
}
} void Updata(int L, int R, int c, int l, int r, int rt){
if(L <= l && r <= R){
col[rt] = c;
return ;
}
PushDown(rt);
int m = (l + r) >> ;
if(L <= m) Updata(L, R, c, lson);
if(R > m) Updata(L, R, c, rson);
} void Query(int l,int r,int rt) {
if (col[rt] != -) {
if (!Hash[col[rt]]) cnt ++;
Hash[col[rt]] = true;
return ;
}
if (l == r) return ;
int m = (l + r) >> ;
Query(lson);
Query(rson);
} int Tran(int pos){
return distance(x.begin(), lower_bound(x.begin(), x.end(), pos)); //这个地方要注意, x.begin()要放前面, 不然出来的都是负值
} int Disc(vector<int> &x){ //vector离散化
sort(x.begin(), x.end());
x.erase(unique(x.begin(), x.end()), x.end());
for(int i = x.size() - ; i > ; i--)
if(x[i] != x[i-] + ) x.push_back(x[i]+);
sort(x.begin(), x.end());
return x.size();
} /**
int Disc(int cur, int X[]){ //数组离散化
sort(X, X + cur);
int m = 1;
for(int i = 1; i < cur; i++){
if(X[i]!=X[i-1]) X[m++] = X[i];
}
for(int i = m-1; i > 0; i--){
if(X[i] != X[i-1] + 1) X[m++] = X[i] + 1;
}
sort(X, X + m);
return m;
}
*/ void Init_work(){
memset(col, -, sizeof(col));
memset(Hash, false, sizeof(Hash));
cnt = ;
} int main(){
int T,n;
scanf("%d", &T);
while(T--){
Init_work();
scanf("%d", &n);
for(int i = ; i < n; i++){
scanf("%d %d", &li[i], &ri[i]);
x.push_back(li[i]);
x.push_back(ri[i]);
}
int m = Disc( x ); for(int i = ; i < n; i++){
Updata(Tran(li[i]), Tran(ri[i]), i, , m, );
} Query(, m, );
printf("%d\n",cnt);
}
}
POJ-2528 Mayor's posters (离散化, 线段树)的更多相关文章
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- 【POJ】2528 Mayor's posters ——离散化+线段树
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Description The citizens of Bytetown, A ...
- POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 59239 Accepted: 17157 ...
- POJ 2528 Mayor's posters(线段树/区间更新 离散化)
题目链接: 传送门 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Description The citizens of By ...
- POJ 2528——Mayor's posters——————【线段树区间替换、找存在的不同区间】
Mayor's posters Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ 2528 ——Mayor's posters(线段树+区间操作)
Time limit 1000 ms Memory limit 65536 kB Description The citizens of Bytetown, AB, could not stand t ...
- (中等) POJ 2528 Mayor's posters , 离散+线段树。
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- POJ 2528 Mayor's posters(线段树染色问题+离散化)
http://poj.org/problem?id=2528 题意: 给出一面无限长的墙,现在往墙上依次贴海报,问最后还能看见多少张海报. 题意:这道题目就相当于对x轴染色,然后计算出最后还能看见多少 ...
- POJ 2528 Mayor's posters(线段树)
点我看题目 题意 :建一堵墙粘贴海报,每个候选人只能贴一张海报,海报的高度与墙一样高,一张海报的宽度是整数个单位,墙被划分为若干个部分,每个部分的宽度为一个单位,每张海报完全的覆盖一段连续的墙体,墙体 ...
- POJ 2528 - Mayor's posters - [离散化+区间修改线段树]
题目链接:http://poj.org/problem?id=2528 Time Limit: 1000MS Memory Limit: 65536K Description The citizens ...
随机推荐
- layui数据表格分页加载动画,自己定义加载动画,"加载中..."
记录思路,仅供参考 在表格渲染完成后,在done回调函数中给分页动态加点击事件, 关闭"加载中..."动画也是在 done回调函数中关闭 这是我实现的思路,记录给大家参考. , d ...
- 内存泄露问题改进(转自vczh)
参考:http://www.cppblog.com/vczh/archive/2010/06/22/118493.html 参考:https://www.cnblogs.com/skynet/arch ...
- cryto-js 常用加密库 md5加密
安装 npm i crypto-js 使用 import CryptoJs from 'crypto-js' CryptoJs.MD5(password).toString() password 会被 ...
- 面试题常考&必考之--js闭包特性和优缺点 (外加小例子)
当内部函数被返回到外部并保存时,一定会产生闭包.闭包会产生原来的作用域链,不释放. 闭包,可以理解为,写一个函数,然后产生闭包的这种现象. 概念: 基础: 主要是:add reduce 被返回 ...
- CSS3画五角星和六角星
最终想要实现的效果 一.五角星 在画五角星之前首先分析这个五角星是如何实现,由哪几个部分构成的,示意图如下: 三个顶角向上的三角形,通过设置旋转和定位相互重叠和拼接实现最终的五角星效果. 为了语义化和 ...
- 20181019-JSP 教程/简介
JSP 教程 这是第一篇JSP JSP与PHP.ASP.ASP.NET等语言类似,运行在服务端的语言. JSP(全称Java Server Pages)是由Sun Microsystems公司倡导和许 ...
- Spring Cloud Commons教程(三)忽略网络接口
有时,忽略某些命名网络接口是有用的,因此可以将其从服务发现注册中排除(例如,在Docker容器中运行).可以设置正则表达式的列表,这将导致所需的网络接口被忽略.以下配置将忽略“docker0”接口和以 ...
- WebServices 实现跨应用程序进行通信和跨平台进行通信
SOA ,即Service Oriented Architecture ,中文一般理解为面向服务的架构, 既然说是一种架构的话,所以一般认为 SOA 是包含了运行环境,编程模型, 架构风格和相关方法论 ...
- Linux网卡驱动分析
以太网(Ethernet)是一种计算机局域网组网技术,基于IEEE 802.3标准,它规定了包括物理层的连线.电信号和介质访问层协议. Ethernet接口的实质是MAC通过MII总线控制PHY的过程 ...
- Linux下查看分区内目录及文件占用空间容量
转载linux下使用 du查看某个文件或目录占用磁盘空间的大小 du -ah --max-depth=1 这个是我想要的结果 a显示目录占用的磁盘空间大小,还要显示其下目录和文件占用磁盘 ...