time limit per test 3 seconds
memory limit per test 512 megabytes
input standard input
output standard output

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 ≤ ris ≠ 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.

Example
input
8
3 1 4 1 5 9 2 6
4
1 8
1 3
4 8
5 7
output
0
1
1
3

树套树 线段树+set 脑洞题

将询问按右端点从小到大排序。从左向右扫描,每次用当前点x更新从1到x位置的答案,然后回答所有右端点在x的询问。

我们可以用线段树更新每个位置答案的时候,这样复杂度显然比暴力更……劣了(可能需要遍历每个位置)。

不过我们可以给线段树加一些剪枝。当用a[x]更新区间[L,R]的时候,设当前最优答案是nowans 如果[L,R]区间内没有在a[x]-nowans ~ a[x]+nowans范围内的数,就不往深层更新了。

如何查找线段树区间内有哪些数?结点上挂个平(s)衡(e)树(t)即可。

↑为了让这个剪枝正确,更新的时候必须先更新线段树右区间再更新左区间。

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<set>
using namespace std;
const int INF=1e9;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct node{
int f;
set<int>st;
}t[mxn<<];
int a[mxn];
set<int>::iterator it,it2;
struct query{
int L,R;
int id;
bool operator < (const query &b)const{
return (R==b.R && L<b.L) || R<b.R;
}
}q[mxn];
int ans[mxn];
int n,Q;
#define ls rt<<1
#define rs rt<<1|1
void Build(int l,int r,int rt){
t[rt].f=INF;
for(int i=l;i<=r;i++)t[rt].st.insert(a[i]);
if(l==r)return;
int mid=(l+r)>>;
Build(l,mid,ls);Build(mid+,r,rs);
it=t[rt].st.begin();
it2=it;it2++;
for(;it2!=t[rt].st.end();it++,it2++){
t[rt].f=min(t[rt].f,abs(*it2-*(it)));
}
return;
}
int query(int L,int R,int l,int r,int rt){
// printf("L:%d R:%d l:%d r:%d rt:%d f:%d\n",L,R,l,r,rt,t[rt].f);
if(L<=l && r<=R)return t[rt].f;
int mid=(l+r)>>;int res=INF;
if(L<=mid)res=min(res,query(L,R,l,mid,ls));
if(R>mid)res=min(res,query(L,R,mid+,r,rs));
return res;
}
int nowans;
void update(int pos,int v,int l,int r,int rt){
if(l==r){
nowans=min(nowans,abs(a[l]-v));
t[rt].f=min(t[rt].f,nowans);
}
it=t[rt].st.lower_bound(v);
if(it!=t[rt].st.begin()){it2=it;it2--;}
if((it==t[rt].st.begin() || *(it2)<=v-nowans)&&(it==t[rt].st.end() || *(it)>=v+nowans)){
nowans=min(nowans,query(l,r,,n,));
return;
}
int mid=(l+r)>>;
if(pos>mid)
update(pos,v,mid+,r,rs);
update(pos,v,l,mid,ls);
t[rt].f=min(t[ls].f,t[rs].f);
return;
}
void solve(){
int i,j;
int hd=;
for(i=;i<=Q;i++){
while(hd<q[i].R){
nowans=INF; update(hd,a[hd+],,n,); hd++;
}
ans[q[i].id]=query(q[i].L,q[i].R,,n,);
}
for(i=;i<=Q;i++)printf("%d\n",ans[i]);
return;
}
int main(){
int i,j;
n=read();
for(i=;i<=n;i++)a[i]=read();
Q=read();
Build(,n,);
for(i=;i<=Q;i++){
q[i].L=read();q[i].R=read();q[i].id=i;
}
sort(q+,q+Q+);
solve();
return ;
}

Codeforces 765F Souvenirs的更多相关文章

  1. Codeforces 765F Souvenirs - 莫队算法 - 链表 - 线段树

    题目传送门 神速的列车 光速的列车 声速的列车 题目大意 给定一个长度为$n$的序列,$m$次询问区间$[l, r]$内相差最小的两个数的差的绝对值. Solution 1 Mo's Algorith ...

  2. Codeforces 765F Souvenirs 线段树 + 主席树 (看题解)

    Souvenirs 我们将询问离线, 我们从左往右加元素, 如果当前的位置为 i ,用一棵线段树保存区间[x, i]的答案, 每次更新完, 遍历R位于 i 的询问更新答案. 我们先考虑最暴力的做法, ...

  3. Codeforces.765F.Souvenirs(主席树)

    题目链接 看题解觉得非常眼熟,总感觉做过非常非常类似的题啊,就是想不起来=v=. 似乎是这道...也好像不是. \(Description\) 给定长为\(n\)的序列\(A_i\).\(m\)次询问 ...

  4. 【codeforces 765F】 Souvenirs

    http://codeforces.com/problemset/problem/765/F (题目链接) 题意 给出$n$个数的序列,$m$次询问,每次查询区间$[l,r]$之间相差最小的两个数的差 ...

  5. 【codeforces 765F】Souvenirs

    Description Artsem is on vacation and wants to buy souvenirs for his two teammates. There are n souv ...

  6. Souvenirs CodeForces - 765F (好题)

    大意: 给定序列$a$, $m$个询问$[l,r]$, 回答$[l,r]$内最接近的两个数的差. 考虑离线, 枚举右端点, 每个点维护左端点的贡献, 对于新添一个点$a_r$, 只考虑左侧点比$a_r ...

  7. codeforces选做

    收录了最近本人完成的一部分codeforces习题,不定期更新 codeforces 1132E Knapsack 注意到如果只使用某一种物品,那么这八种物品可以达到的最小相同重量为\(840\) 故 ...

  8. 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 ...

  9. CodeForces 765 F Souvenirs 线段树

    Souvenirs 题意:给你n个数, m次询问, 对于每次一次询问, 求出询问区间内绝对值差值的最小值. 题解:先按查询的右端点从小到大sort一下,然后对于塞入一个数的时候, 就处理出所有左端点到 ...

随机推荐

  1. swift - tabBar图片设置的一些注意点

    图片大小尺寸 刚刚开始接触的话,从美工那边拿来的图标大小一般都是偏大的,就像这样: 在此建议,tabBar的图标大小可以是32*32,个人感觉效果不错 图片的颜色问题 如上图所示,该图标的期望颜色(也 ...

  2. lintcode-473-单词的添加与查找

    473-单词的添加与查找 设计一个包含下面两个操作的数据结构:addWord(word), search(word) addWord(word)会在数据结构中添加一个单词.而search(word)则 ...

  3. 倒计时60s 代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 【php】set_include_path和get_include_path用法详解

    目的:在框架中方便加载文件 参考:http://blog.sina.com.cn/s/blog_4ce89f200100twbl.html 如果我们没有设置这个值,可能我们需要写一些完全的路径:    ...

  5. gulp 定义依赖关系

    var gulp = require('gulp'); // 返回一个 callback,因此系统可以知道它什么时候完成 gulp.task('one', function(cb) { // 做一些事 ...

  6. Debugger DataSet 调试时查看DataSet

    delphi  跟踪调试的时候查看DataSet数据记录 Ctrl+F7调试 增强工具DataSethttp://edn.embarcadero.com/article/40268 http://do ...

  7. 第197天:js---caller、callee、constructor和prototype用法

    一.caller---返回函数调用者 //返回函数调用者 //caller的应用场景 主要用于察看函数本身被哪个函数调用 function fn() { //判断某函数是否被调用 if (fn.cal ...

  8. CentOS 6.5 下安装 Redis

    wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-stable make ...

  9. get 与 next()

  10. BZOJ 1190 梦幻岛宝珠(分组01背包)

    跑了7000ms... 这是个体积和价值都超大的背包.但是体积保证为a*2^b的(a<=10,b<=30)形式.且n<=100. 于是可以想到按b来分组.这样的话每组最多为a*n*2 ...