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, ..., a(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.

题意:给定 $n$ 个数, $m$ 次询问,每次询问区间中 $|a_{i}-a_{j}|$ 的最小值。

分析:

对于每一个 $i$ ,考虑所有满足 $j>i$ 且 $a_{j}\leq a_{i}$ 的可能可以成为答案的 $j$($a_{j}\geq a_{i}$ 的情况可以用同样的方式处理)。假设当前已经找到了一对 $(i,j)$,则下一个合法的位置 $k$ 需要满足 $a_{k}<a_{j}$ 且 $a_{k}-a_{i}<a_{j}-a_{k}$,即每次需要查询区间 [j+1,n] 内的第一个满足 $a_{i}\leq a_{k}<\frac{a_{i}+a_{j}}{2}$ 的 $k$,可以将数字从大到小加入线段树后直接查询。由于 $a_{j}-a_{i}$ 每次至少减少一半,所以最多有 $O(nloga)$对$(i,j)$ 。

得到所有合法的 $(i,j)$ 后,可以按 $j$ 排序后插入树状数组,每次查询左端点。时间复杂度 $O((nloga+m)logn)$ 。

 #include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
#define lc(x) x<<1
#define rc(x) x<<1|1
using namespace std;
const int N=1e5+;
const int inf=0x3f3f3f3f;
int n,m,tot,sum,a[N],b[N];
int mn[N*],ans[N*];
struct data
{
int l,r,w,id;
bool operator < (const data &t) const{return r<t.r||(r==t.r&&id<t.id);}
}c[N*];
int read()
{
int x=,f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
bool cmp(int x,int y){return a[x]>a[y]||(a[x]==a[y]&&x>y);}
void modify(int x,int l,int r,int p,int w)
{
mn[x]=min(mn[x],w);
if(l==r)return;
int mid=(l+r)>>;
if(p<=mid)modify(lc(x),l,mid,p,w);
else modify(rc(x),mid+,r,p,w);
}
int find(int x,int l,int r,int p,int w)
{
if(l==r)return mn[x]<=w?l:;
int mid=(l+r)>>;
if(p<=mid&&mn[lc(x)]<=w)
{
int t=find(lc(x),l,mid,p,w);
if(t)return t;
}
return find(rc(x),mid+,r,p,w);
}
void solve()
{
memset(mn,0x3f,sizeof(mn));
sort(b+,b+n+,cmp);
for(int i=;i<=n;i++)
{
int j=find(,,n,b[i],inf-);
while(j)
{
c[++tot]=(data){b[i],j,a[j]-a[b[i]],};
j=find(,,n,b[i],((a[b[i]]+a[j]-)/));
}
modify(,,n,b[i],a[b[i]]);
}
}
int lowbit(int x){return x&(-x);}
void change(int x,int w)
{
x=n-x+;
while(x<=n)mn[x]=min(mn[x],w),x+=lowbit(x);
}
int query(int x)
{
x=n-x+;sum=inf;
while(x)sum=min(sum,mn[x]),x-=lowbit(x);
return sum;
}
int main()
{
n=read();
for(int i=;i<=n;i++)a[i]=read()+,b[i]=i;
solve();
for(int i=;i<=n;i++)a[i]=1e9+-a[i];
solve();
m=read();
for(int i=;i<=m;i++)
c[++tot]=(data){read(),read(),,i};
sort(c+,c+tot+);
memset(mn,0x3f,sizeof(mn));
for(int i=;i<=tot;i++)
if(c[i].id)ans[c[i].id]=query(c[i].l);
else change(c[i].l,c[i].w);
for(int i=;i<=m;i++)printf("%d\n",ans[i]);
return ;
}

【codeforces 765F】Souvenirs的更多相关文章

  1. 【codeforces 765F】 Souvenirs

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

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  4. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  5. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  6. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  7. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  8. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  9. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

随机推荐

  1. linux ssh免密登陆远程服务器

    10.170.1.18服务器免密登录到10.170.1.16服务器 首先登入一台linux服务器(10.170.1.18),此台做为母机(即登入其他linux系统用这台做为入口):执行一行命令生成ke ...

  2. DeveloperGuide Hive UDTF

    Writing UDTF's Writing UDTF's GenericUDTF Interface GenericUDTF Interface A custom UDTF can be creat ...

  3. 修改json对象的每一个值

    function fun1(obj){ var names={}; /*for in 可以用于数组或者对象*/ for(var name in obj){ names[name] = obj[name ...

  4. Zookeeper集群为什么要是单数

    (原) 在zookeeper集群中,会有三种角色,leader. follower. observer分别对应着总统.议员.观察者. 半数以上投票通过:可以这样理解.客户端的增删改操作无论访问到了哪台 ...

  5. 第五章:Realm

    一,UserRealm 1,UserRealm父类AuthorizingRealm将获取Subject相关信息分成两步: 1.1,获取身份验证信息(doGetAuthenticationInfo): ...

  6. Linux基础(一)

    01-服务器 1.1 服务器型号 1.2 电源---双电源 1.3 CPU---计算,(2个CPU=2路) 1.4 内存 面试题:bufffer和cache的区别? buffer:写入数据到内存里,这 ...

  7. 占位符的使用和PreparedStatement接口使用:

    一.PreparedStatement 接口的使用   首先占位符我们可以使用 Statement 接口来操作数据, 但是这个接口存在两个问题: 1.使用 Statement  接口对象发送的 sql ...

  8. Git—分支管理

    Git—分支管理 分支学习:branch称为分支,默认仅有一个名为master的分支.一般开发新功能流程为:开发新功能时会在分支dev上进行,开发完毕后再合并到master分支. branch相关常用 ...

  9. git中如何撤销部分修改?

    以提问中修改了两个文件a.b为例,假设需要撤销文件a的修改,则修改后的两个文件: 1.如果没有被git add到索引区 git checkout a 便可撤销对文件a的修改 2.如果被git add到 ...

  10. react学习目录

    前面的话 React是如今热门的两大前端框架之一,它设计思路独特,性能卓越,逻辑简单,受到了大量开发者的喜爱.Vue的基本思路是基于HTML模板的扩展,而React的基本思路是基于JS语言的扩展.由于 ...