计蒜客 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, ...
随机推荐
- css - 常见知识点
1. 盒模型 页面渲染时,dom 元素所采用的 布局模型.可通过box-sizing进行设置.根据计算宽高的区域可分为: content-box (W3C 标准盒模型) border-box (IE ...
- Object.keys()应用
<script type="text/javascript"> var person={ firstName:"David", lastName:& ...
- spring boot 监听容器启动
/** * 在容器启动的时候 加载没问完成的消息重发 * @author zhangyukun * */ @Component @Slf4j public class LoadMessageListe ...
- window查看端口信息
netstat -nao |findstr "2129" 列出所有端口的情况 tasklist|findstr "pid" 查看对应进程信息
- Java超简明入门学习笔记(零)
Java编程思想第4版学习笔记(零) 前言 这个笔记本主要记录了我在学习Java编程思想(第4版,中文版)的过程中遇到的重难点及其分析.主要参考了C++11版本的C++语言,对比了它 ...
- 如何 在 jQuery 中的 $.each 循环中使用 break 和 continue
jQuery中each类似于javascript的for循环 但不同于for循环的是在each里面不能使用break结束循环,也不能使用continue来结束本次循环,想要实现类似的功能就只能用ret ...
- parameter– tRPRE and tRPST
DDR读数据有效之前,有一段时间DQS(DQS#)需为低(高),此段时间即为read preamble,tRPRE. 同理,读数据结束之前,某段时间为read postamble,tRPST.
- [转]iMPACT Spartan-6 FPGA - "WARNING:iMPACT:2217-Error shows in the status register, CRC Error bit is Not 0"
AR# 45304 iMPACT Spartan-6 FPGA - "WARNING:iMPACT:2217-Error shows in the status register, CRC ...
- Maven实战02_Maven的安装和配置
1:在Windows上安装Maven 本人系统环境:win10 + JDK1.8 + apache-maven-3.3.9 在安装Maven之前,首先要确定你的Java环境是否已经配置好了,你是否已 ...
- 记一次msf入侵win10,并拍照
好久没有玩kali了,刚才看到一位大佬msf渗透win10的思路,我感觉不错,我就来复现一下 kali :192.168.45.136 win10 : 192.168.45.137 1 首先,我们查 ...