#include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template struct segtree{ int n; T UNIT; vector dat; segtree(int n_, T unit){ UNIT = unit; n = 1; while(n < n_) n *= 2; dat = vector(2*n); for(int i = 0; i < 2*n; i++) dat[i] = UNIT; } T calc(T a, T b){ return max(a, b); } void insert(int k, T a){ dat[k+n-1] = a; } void update_all(){ for(int i = n-2; i >= 0; i--){ dat[i] = max(dat[i*2+1], dat[i*2+2]); } } //k番目の値(0-indexed)をaに変更 void update(int k, T a){ k += n-1; dat[k] = a; while(k > 0){ k = (k-1)/2; dat[k] = calc(dat[k*2+1], dat[k*2+2]); } } //[a, b) //区間[a, b]へのクエリに対してはquery(a, b+1)と呼ぶ T query(int a, int b, int k=0, int l=0, int r=-1){ if(r < 0) r = n; if(r <= a || b <= l) return UNIT; if(a <= l && r <= b) return dat[k]; else{ T vl = query(a, b, k*2+1, l, (l+r)/2); T vr = query(a, b, k*2+2, (l+r)/2, r); return calc(vl, vr); } } }; int n; int X[300000]; int q; int l[100000], r[100000], x[100000], q_idx[100000]; map compress; vector recover; void input(){ cin >> n; for(int i = 0; i < n; i++) cin >> X[i]; cin >> q; vector> v; for(int i = 0; i < q; i++){ int l, r, x; cin >> l >> r >> x; l--; r--; v.push_back({r, l, x, i}); } sort(v.begin(), v.end()); for(int i = 0; i < q; i++){ l[i] = v[i][1], r[i] = v[i][0], x[i] = v[i][2], q_idx[i] = v[i][3]; } { set st; for(int i = 0; i < n; i++) st.insert(X[i]); for(int i = 0; i < q; i++) st.insert(x[i]); int i = 0; for(int y : st){ compress[y] = i; recover.push_back(y); i++; } } } const int INF = 2e9; void solve(){ int m = compress.size(); segtree sgt(m, -INF); int cur_r = -1; vector ret(q); for(int i = 0; i < q; i++){ // cout << l[i] << ',' << r[i] << endl; int ans = INF; while(cur_r < r[i]){ cur_r++; sgt.update(compress[X[cur_r]], cur_r); } int idx = compress[x[i]]; // idxより小さい最大の値 if(sgt.query(0, idx+1) >= l[i]){ if(sgt.query(idx, idx+1) >= l[i]){ ans = 0; }else{ int lb = 0, rb = idx; while(rb-lb > 1){ int c = (lb+rb)/2; if(sgt.query(c, idx+1) >= l[i]) lb = c; else rb = c; } chmin(ans, abs(x[i]-recover[lb])); } } // idxより大きい最小の値 if(sgt.query(idx, m) >= l[i]){ if(sgt.query(idx, idx+1) >= l[i]){ ans = 0; }else{ int lb = idx, rb = m; while(rb-lb > 1){ int c = (lb+rb)/2; if(sgt.query(idx, c+1) >= l[i]) rb = c; else lb = c; } chmin(ans, abs(x[i]-recover[rb])); } } ret[q_idx[i]] = ans; } for(int i = 0; i < q; i++) cout << ret[i] << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; input(); solve(); }