#include #include using namespace std; using namespace atcoder; #define int long long struct Query { int value, left, right, index; bool operator<(const Query& other) const { if (value != other.value) return value < other.value; return left < other.left; } }; int op(int a, int b) { return max(a, b); } int inf=1ll<<60; int e() { return -inf; // effectively negative infinity } signed main() { ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; vector X(N); for (int i = 0; i < N; ++i) { cin >> X[i]; } int Q; cin >> Q; vector query0, query1; vector ans(Q, inf); for (int i = 0; i < N; ++i) { query0.push_back({X[i], -1, -1, i}); query1.push_back({-X[i], -1, -1, i}); } for (int i = 0; i < Q; ++i) { int l, r, x; cin >> l >> r >> x; --l; query0.push_back({x, l, r, i}); query1.push_back({-x, l, r, i}); } sort(query0.begin(), query0.end()); sort(query1.begin(), query1.end()); for (auto& query : {query0, query1}) { segtree seg(N); for (auto& q : query) { if (q.left == -1) { seg.set(q.index, q.value); } else { ans[q.index] = min(ans[q.index], q.value - seg.prod(q.left, q.right)); } } } for (int i = 0; i < Q; ++i) { cout << ans[i] << "\n"; } return 0; }