#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; using ull = unsigned long long; 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; } //template T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); }return a; } //mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); constexpr long long INF = 1LL << 60; constexpr int inf = 1000000007; constexpr long long mod = 1000000007LL; //constexpr long long mod = 998244353; template struct SegmentTree { int sz; vector seg; const F f; const Monoid IE; SegmentTree(int n, const F f, const Monoid& IE) :f(f), IE(IE) { sz = 1; while (sz < n) sz *= 2; seg.assign(2 * sz, IE); } void set(int k, const Monoid& x) { seg[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k * 2], seg[k * 2 + 1]); } } void update(int k, const Monoid& x) { k += sz; seg[k] = x; while (k /= 2) { seg[k] = f(seg[k * 2], seg[k * 2 + 1]); } } Monoid query(int a, int b) { if (a >= b) return IE; Monoid L = IE, R = IE; for (a += sz, b += sz; a < b; a /= 2, b /= 2) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } template int find_subtree(int idx, const C& check, Monoid& M, bool type) { while (idx < sz) { Monoid nxt = type ? f(seg[2 * idx + 1], M) : f(M, seg[2 * idx]); if (check(nxt)) idx = idx * 2 + type; else M = nxt, idx = idx * 2 + 1 - type; } return idx - sz; } template int min_left(int a, const C& check) { Monoid L = IE; if (a <= 0) { if (check(f(L, seg[1]))) return find_subtree(1, check, L, false); return -1; } int b = sz; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) { Monoid nxt = f(L, seg[a]); if (check(nxt)) return find_subtree(a, check, L, false); L = nxt; a += 1; } } return -1; } template int max_right(int b, const C& check) { Monoid R = IE; if (b >= sz) { if (check(f(seg[1], R))) return find_subtree(1, check, R, true); return -1; } int a = 0; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (b & 1) { b -= 1; Monoid nxt = f(seg[b], R); if (check(nxt)) return find_subtree(b, check, R, true); R = nxt; } } return -1; } }; int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int n; cin >> n; vector X(n); for (auto& e : X) cin >> e; int Q; cin >> Q; vector> vt(Q); for (int i = 0; i < Q; i++) { int l, r, x; cin >> l >> r >> x; l--; vt[i] = make_tuple(x, l, r, i); } sort(vt.begin(), vt.end()); auto seg_mn = SegmentTree(n, [](ll a, ll b) {return min(a, b); }, INF); auto seg_mx = SegmentTree(n, [](ll a, ll b) {return max(a, b); }, -INF); using P = pair; priority_queue, greater<>> pq; for (int i = 0; i < n; i++) pq.emplace(X[i], i); for (int i = 0; i < n; i++) seg_mn.set(i, X[i]); seg_mn.build(); seg_mx.build(); vector ans(Q, INF); for (auto [x, l, r, idx] : vt) { while (!pq.empty() and pq.top().first <= x) { auto [t, i] = pq.top(); pq.pop(); seg_mn.update(i, INF); seg_mx.update(i, t); } ll res = INF; chmin(res, x - seg_mx.query(l, r)); chmin(res, seg_mn.query(l, r) - x); ans[idx] = res; } for (auto res : ans) cout << res << "\n"; }