1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 300006
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedef long long ll; int n , m; int A[MAXN];
struct poi { int l , r , idx; } Q[MAXN] ; bool cmp( const poi& a , const poi& b ) { return a.r < b.r; }
int T[MAXN << 2]; vi S[MAXN << 2]; inline void pu( int rt ) { T[rt] = min( T[rt << 1] , T[rt << 1 | 1] ); } void build( int rt , int l , int r ) { rep( i , l , r ) S[rt].pb( A[i] ); sort( all( S[rt] ) ); T[rt] = 0x3f3f3f3f; if( l == r ) return; int m = l + r >> 1; build( rt << 1 , l , m ) , build( rt << 1 | 1 , m + 1 , r ); pu( rt ); } int cur; void mdfy( int rt , int l , int r , int p , int x ) { if( r <= p ) { auto it = lower_bound( all( S[rt] ) , x ); if( it != S[rt].end() ) T[rt] = min( T[rt] , ( *it ) - x ); if( it != S[rt].begin() ) T[rt] = min( T[rt] , x - *( it - 1 ) ); if( T[rt] >= cur ) return; } if( l == r ) { cur = min( cur , T[rt] ); return; } int m = l + r >> 1; if( p > m ) mdfy( rt << 1 | 1 , m + 1 , r , p , x ); mdfy( rt << 1 , l , m , p , x ); pu( rt ); cur = min( cur , T[rt] ); } int que( int rt , int l , int r , int L ) { if( L <= l ) return T[rt]; int m = l + r >> 1; if( L <= m ) return min( T[rt << 1 | 1] , que( rt << 1 , l , m , L ) ); else return que( rt << 1 | 1 , m + 1 , r , L ); }
int ans[MAXN];
void solve() { cin >> n; rep( i , 1 , n ) scanf("%d",A + i); cin >> m; rep( i , 1 , m ) scanf("%d%d",&Q[i].l,&Q[i].r) , Q[i].idx = i; sort( Q + 1 , Q + 1 + m , cmp ); build( 1 , 1 , n ); int cc = 1; while( Q[cc].r <= 1 ) ++ cc; rep( i , 2 , n ) { cur = 0x3f3f3f3f; mdfy( 1 , 1 , n , i - 1 , A[i] ); while( Q[cc].r == i ) ans[Q[cc].idx] = que( 1 , 1 , n , Q[cc].l ) , ++ cc; } rep( i , 1 , m ) printf("%d\n",ans[i]); }
signed main() {
solve(); }
|