#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for(int i = (l); i < (r); ++i)
#define ROF(i, r, l) for(int i = (r) - 1; i >= (l); --i)
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define pb push_back
#define eb emplace_back
#define sz(v) (int)v.size()
#define sum_of(v) accumulate(all(v), 0ll)
#define dbg(x) "[" #x " = " << (x) << "]"
template<typename T>
      bool minimize(T& a, const T& b){
            if(a > b) return a = b, true;
            return false;
      }
template<typename T>
      bool maximize(T& a, const T& b){
            if(a < b) return a = b, true;
            return false;
      }
using ll = long long;
using db = double;
using ld = long double;
using ull = unsigned long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vc = vector<char>;
using vl = vector<ll>;
using vd = vector<db>;
using vpi = vector<pi>;
using vpl = vector<pl>;
void setIO(){
      ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
      freopen("task.inp", "r", stdin);
      freopen("task.out", "w", stdout);
#endif // LOCAL
}
const int MAX = 5e5 + 5;
int N, L, M, X[MAX], v[MAX], c[MAX];
ll pref[MAX], suff[MAX];
int main(){
      setIO();
      cin >> N >> L;
      FOR(i, 0, N) cin >> X[i];
      sort(X, X + N);
      FOR(i, 0, N){
            if(!M || v[M] != X[i]){
                  ++M;
                  v[M] = X[i];
                  c[M] = 1;
            } else ++c[M];
      }
      FOR(i, 1, M+1) pref[i] = pref[i-1] + c[i];
      ROF(i, M+1, 1) suff[i] = suff[i+1] + c[i];
      int Q;
      cin >> Q;
      while(Q--){
            int S, G, T;
            cin >> S >> G >> T;
            ll best = 1e18;
            //move from i to 1 then to M
            ll A = abs(S - v[1]) + abs(G - v[M]) * 1LL * (N+1);
            FOR(j, 1, M) A += 1LL * (v[j+1] - v[j]) * (pref[j]+1);
            minimize(best, A);
            //move from i to M then to i
            ll B = abs(S - v[M]) + abs(G - v[1]) * 1LL * (N+1);
            ROF(j, M, 1) B += 1LL * (v[j+1] - v[j]) * (suff[j+1]+1);
            minimize(best, B);
            FOR(i, 1, M+1){
                  //i -> 1 -> M -> i
                  ll C = abs(S - v[1]) + abs(G - v[i]) * 1LL * (N+1) + 1LL * (v[M] - v[i]) * (pref[i-1] + 1);
                  FOR(j, 1, i) C += 1LL * (v[j+1] - v[j]) * (pref[j]+1);
                  FOR(j, i, M) C += 1LL * (v[j+1] - v[j]) * (suff[j+1] + 1 + pref[i-1]);
                  minimize(best, C);
                  //i -> M -> 1 -> i
                  ll D = abs(S - v[M]) + abs(G - v[i]) * 1LL * (N+1) + 1LL * (v[i] - v[1]) * (suff[i+1] + 1);
                  ROF(j, M, i) D += 1LL * (v[j+1] - v[j]) * (suff[j+1] + 1);
                  FOR(j, 1, i) D += 1ll * (v[j+1] - v[j]) * (pref[i] + 1 + suff[i+1]);
                  minimize(best, D);
//                  cout << dbg(C) << dbg(D) << '\n';
            }
            best += N;
//            cout << dbg(best) << '\n';
            cout << (best <= T ? "Yes\n" : "No\n");
      }
      return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |