#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; int n; ll X[300000]; int q; ll ans[100000]; vector> query[400]; // {r, l, x, idx} void input(){ cin >> n; for(int i = 0; i < n; i++) cin >> X[i]; int sq = sqrt(n)+10; cin >> q; for(int i = 0; i < q; i++){ int l, r, x; cin >> l >> r >> x; l--; r--; query[l/sq].push_back({r, l, x, i}); } for(int i = 0; i <= sq; i++){ sort(query[i].begin(), query[i].end()); } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; input(); multiset st; int cur_l = 0, cur_r = 0; st.insert(X[0]); for(int s = 0; s*s <= n; s++){ for(auto v : query[s]){ int r = v[0], l = v[1], idx = v[3]; ll x = v[2]; while(cur_r < r){ cur_r++; st.insert(X[cur_r]); } while(cur_r > r){ st.erase(st.find(X[cur_r])); cur_r--; } while(cur_l < l){ st.erase(st.find(X[cur_l])); cur_l++; } while(cur_l > l){ cur_l--; st.insert(X[cur_l]); } auto p = st.lower_bound(x); ll a = 1e15; if(p != st.end()) chmin(a, abs(*p-(x))); if(p != st.begin()){ p--; chmin(a, abs(*p-(x))); } ans[idx] = a; // cout << l << ' ' << r << ' ' << st.size() << endl; } } for(int i = 0; i < q; i++) cout << ans[i] << endl; }