计蒜客 Zoning Houses(线段树区间最大次大)
Given a registry of all houses in your state or province, you would like to know the minimum size of an axis-aligned square zone such that every house in a range of addresses lies in the zone or on its border. The zoning is a bit lenient and you can ignore any one house from the range to make the zone smaller.
The addresses are given as integers from 1..n. Zoning requests are given as a consecutive range of houses. A valid zone is the smallest axis-aligned square that contains all of the points in the range, ignoring at most one.
Given the (x, y) locations of houses in your state or province, and a list of zoning requests, you must figure out for each request: What is the length of a side of the smallest axis-aligned square zone that contains all of the houses in the zoning request, possibly ignoring one house?
Input Format
Each input will consist of a single test case.
Note that your program may be run multiple times on different inputs.
Each test case will begin with a line containing two integers nn and q(1≤n,q≤10^5), where n is the number of houses, and q is the number of zoning requests.
The next n lines will each contain two integers, x and y(−10^9≤x,y≤10^9), which are the (x,y) coordinates of a house in your state or province. The address of this house corresponds with the order in the input. The first house has address 1, the second house has address 2, and so on. No two houses will be at the same location.
The next q lines will contain two integers a and b(1≤a<b≤n), which represents a zoning request for houses with addresses in the range [a..b] inclusive.
Output Format
Output q lines.
On each line print the answer to one of the zoning requests, in order: the side length of the smallest axis-aligned square that contains all of the points of houses with those addresses, if at most one house can be ignored.
样例输入1
3 2
1 0
0 1
1000 1
1 3
2 3
样例输出1
1
0
样例输入2
4 2
0 0
1000 1000
300 300
1 1
1 3
2 4
样例输出2
300
299
题意
n个点q次询问,每次询问编号[a,b]的点中,最多忽略一个点覆盖剩下点的最小正方形。
题解
如果不忽略点,那么就是max(最大的X-最小的X,最大的Y-最小的Y)。
如果忽略点,那么就有4种情况,最大的X,最大的Y,最小的X,最小的Y,那么就是次大,次小。
还有4种情况,忽略的点同时是最大的X最大的Y,最大的X最小的Y,最小的X最小的Y,最小的X最大的Y。
线段树维护比较复杂,要自己仔细推一下。
代码
#include<bits/stdc++.h>
using namespace std; const int N=1e5+;
int x[N],y[N];
map<pair<int,int>,int>ma;
int x_maxx[N<<],x_maxx1[N<<],x_minn[N<<],x_minn1[N<<];
int y_maxx[N<<],y_maxx1[N<<],y_minn[N<<],y_minn1[N<<]; void up(int rt)
{
int ls=rt<<,rs=rt<<|;
x_maxx1[rt]=max(min(x_maxx[ls],x_maxx[rs]),max(x_maxx1[ls],x_maxx1[rs]));
x_maxx[rt]=max(x_maxx[ls],x_maxx[rs]); x_minn1[rt]=min(max(x_minn[ls],x_minn[rs]),min(x_minn1[ls],x_minn1[rs]));
x_minn[rt]=min(x_minn[ls],x_minn[rs]); y_maxx1[rt]=max(min(y_maxx[ls],y_maxx[rs]),max(y_maxx1[ls],y_maxx1[rs]));
y_maxx[rt]=max(y_maxx[ls],y_maxx[rs]); y_minn1[rt]=min(max(y_minn[ls],y_minn[rs]),min(y_minn1[ls],y_minn1[rs]));
y_minn[rt]=min(y_minn[ls],y_minn[rs]);
}
int X_MAX,X_MAX1,X_MIN,X_MIN1;
int Y_MAX,Y_MAX1,Y_MIN,Y_MIN1;
void query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
//printf("FUCK=%d\n%d %d\n",rt,X_MIN,X_MIN1);
X_MAX1=max(min(X_MAX,x_maxx[rt]),max(X_MAX1,x_maxx1[rt]));
X_MAX=max(X_MAX,x_maxx[rt]); X_MIN1=min(max(X_MIN,x_minn[rt]),min(X_MIN1,x_minn1[rt]));
X_MIN=min(X_MIN,x_minn[rt]); Y_MAX1=max(min(Y_MAX,y_maxx[rt]),max(Y_MAX1,y_maxx1[rt]));
Y_MAX=max(Y_MAX,y_maxx[rt]); Y_MIN1=min(max(Y_MIN,y_minn[rt]),min(Y_MIN1,y_minn1[rt]));
Y_MIN=min(Y_MIN,y_minn[rt]);
//printf("rt=%d\n%d %d\n",rt,X_MIN,X_MIN1);
return;
}
int mid=(l+r)>>;
if(L<=mid)query(L,R,l,mid,rt<<);
if(R>mid)query(L,R,mid+,r,rt<<|);
}
void build(int l,int r,int rt)
{
if(l==r)
{
x_maxx[rt]=x_minn[rt]=x[l];
x_maxx1[rt]=-2e9;
x_minn1[rt]=2e9; y_maxx[rt]=y_minn[rt]=y[l];
y_maxx1[rt]=-2e9;
y_minn1[rt]=2e9;
return;
}
int mid=(l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
up(rt);
}
int main()
{
int n,q;
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
{
scanf("%d%d",&x[i],&y[i]);
ma[{x[i],y[i]}]=i;
}
build(,n,);
/*
for(int i=1;i<=7;i++)
printf("i=%d\n%d %d %d %d\n%d %d %d %d\n",i,x_maxx[i],x_maxx1[i],x_minn[i],x_minn1[i],
y_maxx[i],y_maxx1[i],y_minn[i],y_minn1[i]);
*/
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
X_MAX=X_MAX1=Y_MAX=Y_MAX1=-2e9;
X_MIN=X_MIN1=Y_MIN=Y_MIN1=2e9;
query(l,r,,n,);
//printf("%d %d %d %d\n",X_MAX,X_MAX1,X_MIN,X_MIN1);
//printf("%d %d %d %d\n",Y_MAX,Y_MAX1,Y_MIN,Y_MIN1);
int ans=max(X_MAX-X_MIN,Y_MAX-Y_MIN);
ans=min(ans,max(X_MAX1-X_MIN,Y_MAX-Y_MIN));
ans=min(ans,max(X_MAX-X_MIN1,Y_MAX-Y_MIN));
ans=min(ans,max(X_MAX-X_MIN,Y_MAX1-Y_MIN));
ans=min(ans,max(X_MAX-X_MIN,Y_MAX-Y_MIN1));
if(l<=ma[{X_MAX,Y_MAX}]&&ma[{X_MAX,Y_MAX}]<=r)ans=min(ans,max(X_MAX1-X_MIN,Y_MAX1-Y_MIN));
if(l<=ma[{X_MAX,Y_MIN}]&&ma[{X_MAX,Y_MIN}]<=r)ans=min(ans,max(X_MAX1-X_MIN,Y_MAX-Y_MIN1));
if(l<=ma[{X_MIN,Y_MAX}]&&ma[{X_MIN,Y_MAX}]<=r)ans=min(ans,max(X_MAX-X_MIN1,Y_MAX1-Y_MIN));
if(l<=ma[{X_MIN,Y_MIN}]&&ma[{X_MIN,Y_MIN}]<=r)ans=min(ans,max(X_MAX-X_MIN1,Y_MAX-Y_MIN1));
printf("%d\n",ans);
}
return ;
}
/*
5 5
-1000000000 -1000000000
-1000000000 1000000000
1000000000 1000000000
1000000000 -1000000000
0 0
1 3
*/
计蒜客 Zoning Houses(线段树区间最大次大)的更多相关文章
- 计蒜客 31459 - Trace - [线段树][2018ICPC徐州网络预赛G题]
题目链接:https://nanti.jisuanke.com/t/31459 样例输入 3 1 4 4 1 3 3 样例输出 10 题意: 二维平面上给出 $n$ 个点,每个点坐标 $\left( ...
- [计蒜客T2237]魔法_树
魔法 题目大意: 数据范围: 题解: 这个题挺好玩的 可以用反证法,发现所有叶子必须都得选而且所有叶子都选了合法. 故此我们就是要使得,一次操作之后使得叶子的个数最少. 这怎么弄呢? 我们发现,如果一 ...
- 计蒜客 25985.Goldbach-米勒拉宾素数判定(大素数) (2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 B)
若干年之前的一道题,当时能写出来还是超级开心的,虽然是个板子题.一直忘记写博客,备忘一下. 米勒拉判大素数,关于米勒拉宾是个什么东西,传送门了解一下:biubiubiu~ B. Goldbach 题目 ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- 【BZOJ3995】[SDOI2015]道路修建 线段树区间合并
[BZOJ3995][SDOI2015]道路修建 Description 某国有2N个城市,这2N个城市构成了一个2行N列的方格网.现在该国政府有一个旅游发展计划,这个计划需要选定L.R两列(L&l ...
- 计蒜客 28449.算个欧拉函数给大家助助兴-大数的因子个数 (HDU5649.DZY Loves Sorting) ( ACM训练联盟周赛 G)
ACM训练联盟周赛 这一场有几个数据结构的题,但是自己太菜,不会树套树,带插入的区间第K小-替罪羊套函数式线段树, 先立个flag,BZOJ3065: 带插入区间K小值 计蒜客 Zeratul与Xor ...
- 计蒜客 A1607 UVALive 8512 [ACM-ICPC 2017 Asia Xi'an]XOR
ICPC官网题面假的,要下载PDF,点了提交还找不到结果在哪看(我没找到),用VJ交还直接return 0;也能AC 计蒜客题面 这个好 Time limit 3000 ms OS Linux 题目来 ...
- [计蒜客] 矿石采集【记搜、Tarjan缩点+期望Dp】
Online Judge:计蒜客信息学3月提高组模拟赛 Label:记搜,TarJan缩点,树状数组,期望Dp 题解 整个题目由毫无关联的两个问题组合成: part1 问题:对于每个询问的起点终点,求 ...
- 计蒜客 NOIP 提高组模拟竞赛第一试 补记
计蒜客 NOIP 提高组模拟竞赛第一试 补记 A. 广场车神 题目大意: 一个\(n\times m(n,m\le2000)\)的网格,初始时位于左下角的\((1,1)\)处,终点在右上角的\((n, ...
随机推荐
- webpack新手名词解释……妈妈再也不担心我看不懂webpack官方文档了
__dirname : 在任何模块文件内部,可以使用__dirname变量获取当前模块文件所在目录的完整绝对路径. path.resolve(): 方法将一系列路径或路径段解析为绝对路径. 语法: p ...
- 上海第三产业增加值 占比GDP首破七成
上海第三产业增加值 占比GDP首破七成 2016年08月16日08:10 来源:新闻晨报 分享到: 不久前结束的ChinaJoy上,一家名为HYPEREAL的VR公司展台前,体验者的热情程度 ...
- 局域网下共享 MySQL 数据库连接
转载自:https://blog.csdn.net/larger5/article/details/96974554 一.前言 在使用公司的一些开发框架,需要特定的 MySQL 版本,还要做一些配置操 ...
- 设置和修改Linux的swap分区大小
在Linux编译gcc时,遇到编译错误,究其根源是因为内存不足,这时通过修改swap大小解决了问题 相关操作如下: 1. 查看当前分区情况free -m 2. 增加 swap 大小, 2G 左右dd ...
- ThinkCMF框架任意内容包含漏洞复现
1. 漏洞概述 ThinkCMF是一款基于PHP+MYSQL开发的中文内容管理框架,底层采用ThinkPHP3.2.3构建. 利用此漏洞无需任何权限情况下,构造恶意的url,可以向服务器写入任意内容的 ...
- 解析Request和Response
简介: Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象. request和response对象即然代表请求和响应 ...
- html2canvas JS截图插件
github/download:https://github.com/niklasvh/html2canvas/releases 参考文章:基于html2canvas实现网页保存为图片及图片清晰度优化 ...
- Android SDK上手指南:下一步学习方向
Android SDK上手指南:下一步学习方向 2014-02-28 11:01 核子可乐 译 51CTO 字号:T | T 到目前为止,我们已经介绍过的知识足以帮助大家从非常理想的起点开始进行And ...
- 转载 ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(一) 整理基础数据
ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(一) 整理基础数据 最近碰巧发现一款比较好的Web即时通讯前端组件,layim,百度关键字即可,我下面要做的就是基于这个前 ...
- win7安装mysql8提示one more product requirements have not been satisified
点击否 然后查看一下到底缺啥,系统版本不一样,缺少的东西也不一定一样 去微软下就是了https://www.microsoft.com/en-us/download/details.aspx?id=4 ...