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. Git命令行管理代码、安装及使用

    出处:https://www.cnblogs.com/ximiaomiao/p/7140456.html Git安装和使用     目的:通过Git管理github托管项目代码 一.下载安装Git 1 ...

  2. HTML5 canvas clearRect() 方法

    浏览器支持 Internet Explorer 9.Firefox.Opera.Chrome 以及 Safari 支持 clearRect() 方法. 注释:Internet Explorer 8 或 ...

  3. PHP7--PHP的一次重大变革

    PHP7--PHP的一次重大变革 一.写在开头 PHP7是PHP编程语言全新的一个版本,主要在性能方面获得了极大的提升.官方的文档显示,PHP7可以达到PHP5.x版本两倍的性能.同时还对PHP的语法 ...

  4. K8S集群技术

    1.快速部署K8S环境 k8s-m :10.0.0.11   k8s-n1 :10.0.0.12   k8s-n2 :10.0.0.13 2.所有节点安装docker环境及依赖 2.1 上传docke ...

  5. UI Automator 常用 API 整理

    主要类: import android.support.test.uiautomator.UiDevice; 作用:设备封装类,测试过程中获取设备信息和设备交互. import android.sup ...

  6. TestNG安装及使用

    安装:https://www.cnblogs.com/xusweeter/p/6559196.html使用:https://www.cnblogs.com/liwu/p/5113936.html 作用 ...

  7. Redis详解(三)------ redis的五大数据类型详细用法

    我们说 Redis 相对于 Memcache 等其他的缓存产品,有一个比较明显的优势就是 Redis 不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据 ...

  8. flask(二)之Jinja2模板与Flask-WTF

    01-文档 官方文档:http://docs.jinkan.org/docs/jinja2/ 02-基本语义 Jinja2做构成的模板文件中,文本内容大致可以分成几个种类.比如特殊文本(不进行转义,比 ...

  9. 网站添加icon

    设置网站的icon<link rel="shortcut icon" href="./static/img/favicon.ico" >

  10. First ServiceStack Service

    博客1:ServiceStack Web Service 创建与调用简单示列 博客2:Hbuilder+vs2017 web api开发app 官方文档:servicestack docs 1.手动下 ...