计蒜客 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, ...
随机推荐
- P1985 [USACO07OPEN]翻转棋
题目链接: 翻转棋 题目分析: 先状压/\(dfs\)枚举第一排状态,然后在每个\(1\)下面翻,即确定了第一排就确定了后面的状态 最后验证一下最后一排是不是全0即可 代码: #include< ...
- laravel中如何使用Redis(Redis是什么)
laravel中如何使用Redis(Redis是什么) 一.总结 一句话总结: 基于内存亦可持久化键值数据库 Redis是完全开源免费的,遵守BSD协议,是一个高性能的键值数据库.是当前最热门的的的N ...
- Python2.7版本:定义类时为什么要继承object类?
********此答案摘自知乎,且经过自己实际运行后得出******** 继承 object 类的是新式类,不继承 object 类的是经典类 例子: 新式类: 经典类: B.C 是 A 的子类,D ...
- Spring SpringMVC SpringBoot SpringCloud 注解整理大全
Spring SpringMVC SpringBoot SpringCloud 注解整理 才开的博客所以放了一篇以前整理的文档,如果有需要添加修改的地方欢迎指正,我会修改的φ(๑˃∀˂๑)♪ Spri ...
- C#绘制渐变背景
//绘制渐变色背景 Graphics g = e.Graphics; LinearGradientBrush linearGradientBrush = new LinearGradientBrush ...
- 2019-5-24-WPF-源代码-从零开始写一个-UI-框架
title author date CreateTime categories WPF 源代码 从零开始写一个 UI 框架 lindexi 2019-05-24 15:54:36 +0800 2018 ...
- python使用SUDS调用webservice
最近做接口对接,遇到了.net开发的webservice接口,因为python第一次与webservice对接,连问带查,最后使用suds库来实现了 1.安装suds mac: sudo pip in ...
- 中国 SaaS 企业如何突围?这几点是关键!
进入2019年,关于SaaS的各种言论甚嚣尘上,有人看好:“SaaS市场得大客户者得天下”,也有人唱衰:“SaaS已死,下一个”.在众说纷纭中,中国SaaS企业的成功之路迷雾重重,前期走的较早的头部S ...
- python 连接mssql数据库
1.目标数据sql2008 R2 ComPrject=>TestModel 2.安装python 连接mssql 模块 运行 pip install pymssql-2.2.0.dev0-cp3 ...
- Joining Byte Blocks(哈希+带花树)
题目链接 Problem Statement As you are probably aware, the Internet protocols specify a canonical byte or ...