天天看點

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;
}