find the minimum absolute difference between all pairs in that interval.
Input
First line contains an integer T (T ≤ 10). T sets follow. Each set begins with an integer N (N ≤
200000). In the next line there are N integers ai (1 ≤ ai ≤ 104
), the number in the i-th cell of the
array. Next line will contain Q (Q ≤ 104
). Q lines follow, each containing two integers li
, ri (1 ≤ li
,
ri ≤ N, li < ri) describing the beginning and ending of of i-th range. Total number of queries will be
less than 15000.
Output
For the i-th query of each test output the minimum |ajak| for li ≤ j, k ≤ ri (j ̸= k) a single line.
Sample Input
1
10
1 2 4 7 11 10 8 5 1 10000
4
1 10
1 2
3 5
8 10
Sample Output
0
1
3
4
题意:给定一个整数数组,对每组的询问L,R,输出区间差最小是多少
做的时候TLE超时了,大概是用的方法太过于暴力了,后来改了改总算是过了
#include<bits/stdc++.h>
#define maxn 200010
using namespace std;
long long a[maxn];
bool vis[10010];
int main(){
long long t,i,j,n,m,l,r;
bool flag;
scanf("%lld",&t);
while (t--){
scanf("%lld",&n);
for (i=1;i<=n;i++)
scanf("%lld",&a[i]);
scanf("%lld",&m);
while (m--){
scanf("%lld%lld",&l,&r);
if (r-l+1>10000){
puts("0");
continue;
}
memset (vis,0,sizeof(vis));
flag=0;
for (i=l;i<=r;i++){
if (!vis[a[i]]){
vis[a[i]]=1;
}
else {
flag=1;
}
}
if (flag){
puts("0");
}
else {
long long temp=-1;
long long minn=10000;
for (i=1;i<=10000;i++){
if (vis[i]&&temp==-1){
temp=i;
}
else if (vis[i]){
long long fun=i-temp;
temp=i;
if (fun<minn)minn=fun;
}
}
printf("%lld\n",minn);
}
}
}
return 0;
}