Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) F. Souvenirs 线段树套set
F. Souvenirs
题目连接:
http://codeforces.com/contest/765/problem/F
Description
Artsem is on vacation and wants to buy souvenirs for his two teammates. There are n souvenir shops along the street. In i-th shop Artsem can buy one souvenir for ai dollars, and he cannot buy more than one souvenir in one shop. He doesn't want to introduce envy in his team, so he wants to buy two souvenirs with least possible difference in price.
Artsem has visited the shopping street m times. For some strange reason on the i-th day only shops with numbers from li to ri were operating (weird? yes it is, but have you ever tried to come up with a reasonable legend for a range query problem?). For each visit, Artsem wants to know the minimum possible difference in prices of two different souvenirs he can buy in the opened shops.
In other words, for each Artsem's visit you should find the minimum possible value of |as - at| where li ≤ s, t ≤ ri, s ≠ t.
Input
The first line contains an integer n (2 ≤ n ≤ 105).
The second line contains n space-separated integers a1, ..., an (0 ≤ ai ≤ 109).
The third line contains the number of queries m (1 ≤ m ≤ 3·105).
Next m lines describe the queries. i-th of these lines contains two space-separated integers li and ri denoting the range of shops working on i-th day (1 ≤ li < ri ≤ n).
Output
Print the answer to each query in a separate line.
Sample Input
8
3 1 4 1 5 9 2 6
4
1 8
1 3
4 8
5 7
Sample Output
0
1
1
3
Hint
题意
给你n个数,查询区间(l,r)中,某两个l<=i,j<=r,且i!=r的,min(abs(a[i]-a[j]))这个值。
题解:
只有询问,显然就离线。
最简单的做法是莫队+xxx,很容易做到nsqrt(n)logn的复杂度,当然这会TLE的。
于是我们离线去,用线段树套set去维护这个最小值。
每个线段树的节点,都维护一个set,存放的是这个区间的数。
然后每次离线更新的时候,更新这个节点的最小值就好了。因为插入一个数,只会考虑这个数左边和右边的数是什么就好了。
然后不断更新就行。
这样复杂度是nlognlogn的。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
struct node{
int l,r,id;
bool operator<(const node& a)const{
if(a.l==l&&a.r==r){
return id<a.id;
}
if(a.l==l)
return r<a.r;
return l<a.l;
}
}query[maxn];
struct Node{
set<int> T;
int mi;
}T[maxn];
int n,m,b[maxn],ans[maxn];
void build(int x,int l,int r){
T[x].mi=(1<<30);
for(int i=l;i<=r;i++)
T[x].T.insert(b[i]);
if(l==r)return;
int mid=(l+r)/2;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
}
int Query(int x,int l,int r,int L,int R){
if(l>R||L>r)return (1<<30);
if(L<=l&&r<=R)return T[x].mi;
int mid=(l+r)/2;
return min(Query(x<<1,l,mid,L,R),Query(x<<1|1,mid+1,r,L,R));
}
void upd(int x,int l,int r,int L,int R,int val,int &mi){
if(l>R||L>r)return;
if(l==r){
T[x].mi=min(T[x].mi,abs(val-b[l]));
mi=min(mi,T[x].mi);
return;
}
set<int> &t = T[x].T;
auto p = t.lower_bound(val);
if((p==t.end()||(*p-val)>=mi)&&(p==t.begin()||(val-*(--p))>=mi)){
mi=min(mi,Query(x,l,r,L,R));
return;
}
int mid=(l+r)/2;
upd(x<<1,l,mid,L,R,val,mi);
upd(x<<1|1,mid+1,r,L,R,val,mi);
T[x].mi=min(T[x<<1].mi,T[x<<1|1].mi);
}
void init(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&b[i]);
}
scanf("%d",&m);
for(int i=1;i<=m;i++)
scanf("%d%d",&query[i].l,&query[i].r),query[i].id=i;
}
int main(){
init();
sort(query+1,query+1+m);
build(1,1,n);
for(int i=m,l=n;i;i--){
for(;l>=query[i].l;l--){
int inf=(1<<30);
upd(1,1,n,l+1,n,b[l],inf);
}
ans[query[i].id]=Query(1,1,n,query[i].l,query[i].r);
}
for(int i=1;i<=m;i++)
cout<<ans[i]<<endl;
return 0;
}
Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) F. Souvenirs 线段树套set的更多相关文章
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding 拓扑排序
E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders 数学 构造
D. Artsem and Saunders 题目连接: http://codeforces.com/contest/765/problem/D Description Artsem has a fr ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) C. Table Tennis Game 2 水题
C. Table Tennis Game 2 题目连接: http://codeforces.com/contest/765/problem/C Description Misha and Vanya ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) B. Code obfuscation 水题
B. Code obfuscation 题目连接: http://codeforces.com/contest/765/problem/B Description Kostya likes Codef ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A. Neverending competitions 水题
A. Neverending competitions 题目连接: http://codeforces.com/contest/765/problem/A Description There are ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A B C D 水 模拟 构造
A. Neverending competitions time limit per test 2 seconds memory limit per test 512 megabytes input ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding
地址:http://codeforces.com/contest/765/problem/E 题目: E. Tree Folding time limit per test 2 seconds mem ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders
地址:http://codeforces.com/contest/765/problem/D 题目: D. Artsem and Saunders time limit per test 2 seco ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) C - Table Tennis Game 2
地址:http://codeforces.com/contest/765/problem/C 题目: C. Table Tennis Game 2 time limit per test 2 seco ...
随机推荐
- bzoj千题计划286:bzoj1226: [SDOI2009]学校食堂Dining
http://www.lydsy.com/JudgeOnline/problem.php?id=1226 关键点:一个人只能忍受 ‘紧跟’ 在他 后面的b个人比他先打到饭 dp[i][j][k] 前i ...
- hdu 5290 Bombing plan
http://acm.hdu.edu.cn/showproblem.php?pid=5290 题意: 一棵树,每个点有一个权值wi,选择点i即可破坏所有距离点i<=wi的点,问破坏所有点 最少需 ...
- 第11月第3天 直播 rtmp yuv
1. LiveVideoCoreSDK AudioUnitRender ==> MicSource::inputCallback ==> GenericAudioMixer::pushBu ...
- vsftp服务器部署
环境:CentOS 6.6 目标:个人虚机部署vsftp服务器,供测试使用. 说明:步骤已改写为脚本,直接添加用户与对应的密码列表,调用函数名即可 ########################## ...
- MongoDB 之 Array Object 的特殊操作 MongoDB - 6
相比关系型数据库, Array [1,2,3,4,5] 和 Object { 'name':'DragonFire' } 是MongoDB 比较特殊的类型了 特殊在哪里呢?在他们的操作上又有什么需要注 ...
- MongoDB 之 手把手教你增删改查 MongoDB - 2
我们在 MongoDB 之 你得知道MongoDB是个什么鬼 MongoDB - 1 中学习了如果安装部署一个 MongoDB 如果没看到我的金玉良言的话,就重新打开一次客户端和服务端吧 本章我们 ...
- python 获取二进制文件
import requests response = requests.get('https://www.baidu.com/aladdin/img/tools/ip.png')with open(' ...
- linux系统下如何批量更改文件夹里面所有相同字符【转】
如网上源码下载下来我们需要调试,调试过程中需要修改里面相同文字,下面提供三种方法供参考 通过sed命令替换 sed -i "s/oldyunwei/newyunwei/g" gre ...
- 洛谷P2613有理数取余
传送门 #include <iostream> #include <cstdio> #include <cstring> #include <algorith ...
- java innerclasses(内部类)
可以将一个类定义在另一个类的内部,这就是内部类 创建内部类的方式---把类的定义置于外部类的里面 典型的情况是,外部类将有一个方法,该方法返回一个指向内部类的引用,就像在to()和contents() ...