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(线段树区间最大次大)的更多相关文章

  1. 计蒜客 31459 - Trace - [线段树][2018ICPC徐州网络预赛G题]

    题目链接:https://nanti.jisuanke.com/t/31459 样例输入 3 1 4 4 1 3 3 样例输出 10 题意: 二维平面上给出 $n$ 个点,每个点坐标 $\left( ...

  2. [计蒜客T2237]魔法_树

    魔法 题目大意: 数据范围: 题解: 这个题挺好玩的 可以用反证法,发现所有叶子必须都得选而且所有叶子都选了合法. 故此我们就是要使得,一次操作之后使得叶子的个数最少. 这怎么弄呢? 我们发现,如果一 ...

  3. 计蒜客 25985.Goldbach-米勒拉宾素数判定(大素数) (2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 B)

    若干年之前的一道题,当时能写出来还是超级开心的,虽然是个板子题.一直忘记写博客,备忘一下. 米勒拉判大素数,关于米勒拉宾是个什么东西,传送门了解一下:biubiubiu~ B. Goldbach 题目 ...

  4. 计蒜客 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 ...

  5. 【BZOJ3995】[SDOI2015]道路修建 线段树区间合并

    [BZOJ3995][SDOI2015]道路修建 Description  某国有2N个城市,这2N个城市构成了一个2行N列的方格网.现在该国政府有一个旅游发展计划,这个计划需要选定L.R两列(L&l ...

  6. 计蒜客 28449.算个欧拉函数给大家助助兴-大数的因子个数 (HDU5649.DZY Loves Sorting) ( ACM训练联盟周赛 G)

    ACM训练联盟周赛 这一场有几个数据结构的题,但是自己太菜,不会树套树,带插入的区间第K小-替罪羊套函数式线段树, 先立个flag,BZOJ3065: 带插入区间K小值 计蒜客 Zeratul与Xor ...

  7. 计蒜客 A1607 UVALive 8512 [ACM-ICPC 2017 Asia Xi'an]XOR

    ICPC官网题面假的,要下载PDF,点了提交还找不到结果在哪看(我没找到),用VJ交还直接return 0;也能AC 计蒜客题面 这个好 Time limit 3000 ms OS Linux 题目来 ...

  8. [计蒜客] 矿石采集【记搜、Tarjan缩点+期望Dp】

    Online Judge:计蒜客信息学3月提高组模拟赛 Label:记搜,TarJan缩点,树状数组,期望Dp 题解 整个题目由毫无关联的两个问题组合成: part1 问题:对于每个询问的起点终点,求 ...

  9. 计蒜客 NOIP 提高组模拟竞赛第一试 补记

    计蒜客 NOIP 提高组模拟竞赛第一试 补记 A. 广场车神 题目大意: 一个\(n\times m(n,m\le2000)\)的网格,初始时位于左下角的\((1,1)\)处,终点在右上角的\((n, ...

随机推荐

  1. PAT甲级——A1095 Cars on Campus

    Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out time ...

  2. linux最基础最常用的命令快速手记 — 让手指跟上思考的速度(三)

    这一篇作为姐妹篇的第三篇,废话不多说,我觉得这个比mysql的还要重要,为什么,一旦你摊上linux 敲键盘输入命令简直是要飞的速度,不断的卡壳查命令,效率太低了,而且非常严重的影响思绪,思绪! 某些 ...

  3. Python学习day26-面向对象之小结

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  4. 删除文件夹时提示“You need permission to perform this action。。。”,如何解决?

    Win10系统,有时,要删除某个文件夹,却提示“You need permission to perform this action...” 以下是我Google之后找到的解决方案 1.创建一个文本文 ...

  5. python-web-习题

    1.简单描述 webbrowser.requests.BeautifulSoup 和 selenium 模块之间的不同 webbrowser模块有一个 open() 方法,它启动 web 浏览器,打开 ...

  6. Windows API 第17篇 GetLogicalDriveStrings 获取本机所有逻辑驱动器,以根目录的形式表示

    函数原型:DWORD GetLogicalDriveStrings(  DWORD nBufferLength,  // size of buffer                          ...

  7. 再不懂时序就 OUT 啦!,DBengine 排名第一时序数据库,阿里云数据库 InfluxDB 正式商业化!

    云数据库 InfluxDB® 版介绍 阿里云数据库 InfluxDB® 版已于近日正式启动商业化 . 云数据库 InfluxDB® 是基于当前最流行的开源数据库 InfluxDB 提供的在线数据库服务 ...

  8. windows下 将tomcat做成服务,并于oracle后启动

    一.将tomcat做成服务 1.下载解压版的tomcat 6.*, 设置java.tomcat的环境(这个就不说了). 2.运行->cmd->到tomcat安装目录的bin目录: 3.运行 ...

  9. 工控安全入门(六)——逆向角度看Vxworks

    上一篇文章中我们对于固件进行了简单的分析,这一篇我们将会补充一些Vxworks的知识,同时继续升入研究固件内容. 由于涉及到操作系统的内容,建议大家在阅读本篇前有一定操作系统知识的基础,或者是阅读我的 ...

  10. thinkphp浏览历史功能实现方法

    这篇文章主要介绍了thinkphp浏览历史功能实现方法,可实现浏览器的浏览历史功能,是非常实用的技巧,需要的朋友可以参考下 本文实例讲述了thinkphp浏览历史功能实现方法,分享给大家供大家参考.具 ...