#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; struct Mo{ int sq; vector left,right,order; Mo(int N,int Q) { order.resize(Q); sq = max(1,1.0*N/ max(1.0,sqrt(2.0*Q/3.0))); iota(order.begin(),order.end(),0); } void insert(int l,int r) { left.push_back(l); right.push_back(r); } template void run(const AL &addleft,const AR &addright,const DL &delleft,const DR &delright,const REM &rem) { assert(left.size() == order.size()); sort(order.begin(),order.end(),[&](int u,int v) { int ublock = left[u]/sq,vblock = left[v]/sq; if(ublock != vblock) return ublock < vblock; if(ublock & 1) return right[u] < right[v]; return right[u] > right[v]; }); int nl = 0,nr = 0; for(int i:order) { while(nl > left[i]) addleft(--nl); while(nr < right[i]) addright(nr++); while(nl < left[i]) delleft(nl++); while(nr > right[i]) delright(--nr); rem(i); } } }; void Main() { int N; cin >> N; vector X(N); for(int i = 0;i < N;i++) { cin >> X[i]; } int Q; cin >> Q; Mo mo(N,Q); vector x(Q); for(int i = 0;i < Q;i++) { int l,r; cin >> l >> r >> x[i]; mo.insert(--l,r); } set S; vector ans(Q,(int)2e9); auto add = [&](int i) -> void { S.insert(X[i]); }; auto del = [&](int i) -> void { auto it = S.find(X[i]); assert(it != S.end()); S.erase(it); }; auto rem = [&](int i) -> void { auto it = S.lower_bound(x[i]); if(it != S.end()) { ans[i] = min(ans[i],*it - x[i]); } if(it != S.begin()) { it--; ans[i] = min(ans[i],x[i] - *it); } }; mo.run(add,add,del,del,rem); for(int i = 0;i < Q;i++) { cout << ans[i] << "\n"; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int tt = 1; /* cin >> tt; */ while(tt--) Main(); }