#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include using namespace std; using ll = long long; template using vec = vector; template using vvec = vector>; template bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;} template bool chmax(T& a,T b){if(a=0;i--) #define all(x) (x).begin(),(x).end() #define debug(x) cerr << #x << " = " << (x) << endl; constexpr int si = 600; constexpr int inf = 1e9; class SqrtDecomposition{ private: class bucket{ vec val; vec s; int N,l,r; bool invalid(int a,int b){ return max(l,a)>min(b,r); } int query_all(int x){ int res = inf; int id = lower_bound(all(s),x)-s.begin(); if(id!=N) chmin(res,abs(x-s[id])); if(id!=0) chmin(res,abs(x-s[id-1])); return res; } int query_partial(int a,int b,int x){ int res = inf; for(int i=a;i& A):N(r-l),l(l),r(r),val(A),s(A){ sort(all(s)); } int query(int a,int b,int x){ if(invalid(a,b)) return inf; if(a<=l && r<=b) return query_all(x); else return query_partial(max(l,a)-l,min(r,b)-l,x); } }; int N; vec B; public: SqrtDecomposition(int N,vec A):N(N){ for(int l=0;l val; for(int i=l;i> N; vec X(N); rep(i,N) cin >> X[i]; SqrtDecomposition D(N,X); int Q; cin >> Q; rep(_,Q){ int l,r,x; cin >> l >> r >> x; l--,r--; cout << D.query(l,r+1,x) << "\n"; } }