#line 2 "/home/defineprogram/Desktop/Library/template/template.cpp" #include using namespace std; #define ll long long #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i < n; i++) #define rev(i, n) for (int i = n - 1; i >= 0; i--) #define all(v) v.begin(), v.end() #define P pair #define len(s) (ll) s.size() template inline bool chmin(T &a, U b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T &a, U b) { if (a < b) { a = b; return true; } return false; } constexpr ll inf = 3e18; #line 3 "/home/defineprogram/Desktop/Library/structure/Mo.cpp" struct Mo { vector left, right, order; vector v; int width, l = 0, r = 0, cur = 0; Mo(int n) : width(sqrt(n)), v(n) {} void insert(int l, int r) { left.push_back(l); right.push_back(r); } void init() { order.resize(len(left)); iota(all(order), 0); sort(all(order), [&](int a, int b) { if (left[a] / width != left[b] / width) return left[a] < left[b]; return right[a] < right[b]; }); } int process() { int id = order[cur]; while (l > left[id]) dis(--l); while (r < right[id]) dis(r++); while (l < left[id]) dis(l++); while (r > right[id]) dis(--r); return order[cur++]; } inline void dis(int idx) { v[idx].flip(); if (v[idx]) add(idx); else del(idx); } void add(int); void del(int); }; #line 3 "/home/defineprogram/Desktop/Library/structure/BIT.cpp" template class BIT { ll N; vector bit; void add_(ll x, T y) { while (x <= N) { bit[x] += y; x += x & -x; } } T sum_(ll x) { T res = 0; while (x > 0) { res += bit[x]; x -= x & -x; } return res; } public: ll lower_bound(T w) { if (w <= 0) return -1; ll x = 0; ll k = 1; while (k * 2 <= N) k *= 2; for (; k > 0; k /= 2) { if (x + k <= N && bit[x + k] < w) { w -= bit[x + k]; x += k; } } return x; } void add(ll x, T y) { add_(x + 1, y); } T sum(ll l, ll r) { return sum_(r) - sum_(l); } BIT(ll x) : N(x), bit(x + 1) {} }; /* @brief Binary Indexed Tree @docs docs/BIT.md */ #line 4 "main.cpp" int N,X[1<<19]; BIT bit(1),bit2(1); multisetst; void Mo::add(int idx){ bit.add(X[idx],1); if(bit.sum(X[idx],X[idx]+1)==1){ bit2.add(X[idx],1); } } void Mo::del(int idx){ bit.add(X[idx],-1); if(bit.sum(X[idx],X[idx]+1)==0){ bit2.add(X[idx],-1); } } int Q,L[1<<17],R[1<<17],x[1<<17]; int ans[1<<17]; int main() { cin.tie(0); ios::sync_with_stdio(false); cin>>N; vectorxx; rep(i,N){ cin>>X[i]; xx.push_back(X[i]); } xx.push_back(-1e9);xx.push_back(2e9); sort(all(xx));xx.erase(unique(all(xx)),xx.end()); rep(i,N)X[i]=lower_bound(all(xx),X[i])-xx.begin(); bit=BIT(len(xx)); bit2=BIT(len(xx)); bit.add(0,1);bit2.add(0,1); bit.add(len(xx)-1,1);bit2.add(len(xx)-1,1); Mo mo(len(xx)); cin>>Q; rep(i,Q){ cin>>L[i]>>R[i]>>x[i]; L[i]--; mo.insert(L[i],R[i]); } mo.init(); rep(i,Q){ int id=mo.process(); int idx=lower_bound(all(xx),x[id])-xx.begin(); int memo=bit2.sum(0,idx); int l=bit2.lower_bound(memo),r=bit2.lower_bound(memo+1); ans[id]=min(x[id]-xx[l],xx[r]-x[id]); } rep(i,Q){ cout<