天天看点

Codeforces Round #397 F. Souvenirs(线段树,离线)

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.

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




题意:无修改,每次询问区间中的最小绝对值差(你可以任选两个元素).




分析:我一开始用莫队+权值线段树然后tle14过不去,正解也是离线,从左到右开始插入元素,然后每插入一个元素我们就去拿它更新答案区间,比如当前枚举到元素i,那么我们先找到离i最近的j满足ai > aj,然后拿ai - aj更新[1,j]的询问,然后这是可能存在一个k满足k < j但是ai - ak更优,这时ak必须满足

ak >= (ai+aj)/2,(ak-aj在枚举j时已经更新过了),这样我们要继续向右找到第一个大于等于(ai+aj)/2的数更新答案,然后一直迭代下去,最多更新log(a[i])次,开两个线段树分别来维护必要信息就可以了。





       
#include<bits/stdc++.h>
#define MAXN 0x3f
#define N 100005
#define INF 2147483647
using namespace std;
typedef pair<int,int> pii;
int n,m,a[N],s[N],Ans[3*N];
vector<pii> G[N];
struct Thing
{
	int l,r,Max,Min;
}tr1[4*N],tr2[4*N];
void Build(int node,int l,int r)
{
	tr1[node].l = l,tr1[node].r = r;
	tr2[node].l = l,tr2[node].r = r;
	tr2[node].Min = INF;
	tr1[node].Max = 0;
	if(l == r) return;
	int mid = (l+r)>>1;
	Build(2*node,l,mid);
	Build(2*node+1,mid+1,r);
}
void Push_up(int node,int x,int val)                              //??tr1 ?±?? 
{
	if(tr1[node].l == tr1[node].r)
	{
		tr1[node].Max = val;
		return;
	}
	int mid = (tr1[node].l + tr1[node].r)>>1;
	if(x <= mid) Push_up(2*node,x,val);
	else Push_up(2*node+1,x,val);
	tr1[node].Max = max(tr1[2*node].Max,tr1[2*node+1].Max);
}
int Find(int node,int l,int r)                                   
{
	if(tr1[node].l == l && tr1[node].r == r) return tr1[node].Max;
	int mid = (tr1[node].l+tr1[node].r)>>1;
	if(r <= mid) return Find(2*node,l,r);
	else 
	 if(l <= mid) return max(Find(2*node,l,mid),Find(2*node+1,mid+1,r));
	 else return Find(2*node+1,l,r);
}
void Updata(int node,int l,int r,int val)                       
{
	if(tr2[node].l == l && tr2[node].r == r)
	{
		tr2[node].Min = min(tr2[node].Min,val);
		return;
	}
	int mid = (tr2[node].l+tr2[node].r)>>1;
	if(r <= mid) Updata(2*node,l,r,val);
	else 
	 if(l <= mid) 
	 {
	 	Updata(2*node,l,mid,val);
	 	Updata(2*node+1,mid+1,r,val);
	 }
	 else Updata(2*node+1,l,r,val);
}
int Query(int node,int x)
{
	if(tr2[node].l == tr2[node].r) return tr2[node].Min;
	if(tr2[node].Min == 0) return 0;
	int mid = (tr2[node].l + tr2[node].r)>>1;
	if(x <= mid) return min(tr2[node].Min,Query(2*node,x));
	else return min(tr2[node].Min,Query(2*node+1,x));
}
int main()
{
	scanf("%d",&n);
	for(int i = 1;i <= n;i++)
	{
		scanf("%d",&a[i]);
		s[i] = a[i];
	}
	sort(s+1,s+1+n);
	for(int i = 1;i <= n;i++) a[i] = lower_bound(s+1,s+1+n,a[i]) - s;
	scanf("%d",&m);
	for(int i = 1;i <= m;i++)
	{
		int l,r;
		scanf("%d%d",&l,&r); 
		G[r].push_back(make_pair(l,i));         
	}
	Build(1,1,n);                              
	for(int i = 1;i <= n;i++)
	{
		int t,pos = Find(1,1,a[i]);        
		if(pos)
		{
			Updata(1,1,pos,abs(s[a[i]] - s[a[pos]]));
			while(a[pos] != a[i])
			{
				t = lower_bound(s+1,s+1+n,(s[a[i]] + s[a[pos]])*0.5) - s;
				pos = Find(1,t,a[i]);
				if(!pos) break;
				Updata(1,1,pos,abs(s[a[i]] - s[a[pos]]));
			}
		}
		pos = Find(1,a[i],n);         
		if(pos)
		{
			Updata(1,1,pos,abs(s[a[i]] - s[a[pos]]));
			while(a[pos] != a[i])
			{
				t = lower_bound(s+1,s+1+n,(s[a[i]] + s[a[pos]])*0.5) - s;
				t--;
				pos = Find(1,a[i],t);
				if(!pos) break;
				Updata(1,1,pos,abs(s[a[i]] - s[a[pos]]));
			}
		}
		Push_up(1,a[i],i);
		for(int j = 0;j < G[i].size();j++)
		{
			int pos = G[i][j].first,lab = G[i][j].second;
			Ans[lab] = Query(1,pos);
		}
	}
	for(int i = 1;i <= m;i++) cout<<Ans[i]<<endl;
}