#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; T (*calc)(T, T); segtree(int n_, T unit, T (*_calc)(T, T)){ UNIT = unit; calc = _calc; n = 1; while(n < n_) n *= 2; dat = vector(2*n); for(int i = 0; i < 2*n; i++) dat[i] = UNIT; } void insert(int k, T a){ dat[k+n-1] = a; } void update_all(){ for(int i = n-2; i >= 0; i--){ dat[i] = calc(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[300000], r[300000], x[300000], q_idx[300000]; map compress; int recover[500000]; 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[i] = y; i++; } } } int calc_max(int a, int b){ return max(a, b); } const int INF = 2e9; void solve(){ int m = compress.size(); segtree sgt(m, -INF, calc_max); int cur_r = -1; vector ret(n); 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(); }