//@formatter:off #include #define rep(i,n) for (int i = 0; i < int(n); ++i) #define rrep(i,n) for (int i = int(n)-1; i >= 0; i--) #define rep2(i,s,n) for (int i = int(s); i < int(n); ++i) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define pb push_back #define eb emplace_back #define vi vector #define vvi vector> #define vl vector #define vvl vector> #define vd vector #define vvd vector> #define vs vector #define vc vector #define vvc vector> #define vb vector #define vvb vector> #define vp vector

#define vvp vector> using namespace std; using ll = long long; using P = pair; using LP = pair; template istream& operator>>(istream &is,pair &p) { return is >> p.first >> p.second; } template ostream& operator<<(ostream &os,const pair &p) { return os<<'{'< istream& operator>>(istream &is,vector &v) { for(T &t:v){is>>t;} return is; } template ostream& operator<<(ostream &os,const vector &v) { os<<'[';rep(i,v.size())os< bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;} template bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;} const int inf = 1001001001; const ll linf = 1001001001001001001; //@formatter:on template class segtree { using T = typename Monoid::value_type; int n; vector val; public: constexpr segtree(int _n) { n = 1; while (n < _n) n *= 2; val = vector(2 * n, Monoid::identity); } constexpr segtree(vector init) { int _n = init.size(); n = 1; while (n < _n) n *= 2; val = vector(2 * n, Monoid::identity); rep(i, _n) val[i + n] = init[i]; rrep(i, n) val[i] = Monoid::operate(val[i * 2], val[i * 2 + 1]); } // segment [l,r) T fold(int l, int r) { l += n, r += n; T fold_l = Monoid::identity; T fold_r = Monoid::identity; while (l < r) { if (l & 1) fold_l = Monoid::operate(fold_l, val[l++]); if (r & 1) fold_r = Monoid::operate(val[--r], fold_r); l >>= 1, r >>= 1; } return Monoid::operate(fold_l, fold_r); } template void update(int i, const F &f) { i += n; val[i] = f(val[i]); while (i > 1) { i >>= 1; val[i] = Monoid::operate(val[i * 2], val[i * 2 + 1]); } } }; class Node { public: using value_type = int; value_type value; Node(value_type value) : value(value) {} static constexpr value_type identity = -1; static constexpr value_type operate(const value_type &l, const value_type &r) { return max(l,r); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin >> n; vi X(n); cin >> X; vi pr = X; int q; cin >> q; vi l(q), r(q), x(q); rep(i, q) { cin >> l[i] >> r[i] >> x[i]; l[i]--; r[i]--; pr.pb(x[i]); } sort(all(pr)); pr.erase(unique(all(pr)),pr.end()); int sz = pr.size(); vi ord(q); iota(all(ord), 0); sort(all(ord), [&](int i,int j){ return r[i] < r[j]; }); segtree st(sz); vi ans(q,inf); int now = 0; rep(i,n) { X[i] = lower_bound(all(pr), X[i]) - pr.begin(); st.update(X[i], [&](auto) { return i; }); while(now < q and r[ord[now]] == i) { int nl = l[ord[now]]; int nr = r[ord[now]]; int nx = x[ord[now]]; nx = lower_bound(all(pr),nx)-pr.begin(); if(st.fold(nx,sz) >= nl) { int ok = sz, ng = nx; auto f = [&](int t) -> bool { return st.fold(nx,t) >= nl; }; while(abs(ok-ng) > 1) { int mid = (ng+ok)/2; if(f(mid)) ok = mid; else ng = mid; } chmin(ans[ord[now]],pr[ok-1]-pr[nx]); } if(st.fold(0,nx+1) >= nl) { int ok = 0, ng = nx+1; auto f = [&](int t) -> bool { return st.fold(t,nx+1) >= nl; }; while(abs(ok-ng) > 1) { int mid = (ng+ok)/2; if(f(mid)) ok = mid; else ng = mid; } chmin(ans[ord[now]],pr[nx]-pr[ok]); } now++; } } rep(i,q) cout << ans[i] << (i < q-1 ? "\n" : ""); }