#include<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long
#define pq priority_queue
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pp pop_back
#define F first
#define S second
using namespace std;
vector<int> a;
vector<int> pre;
vector<int> suf;
vector<pair<int, int>> dp;
void solve () {
int n, l;
cin >> n >> l;
a.resize(n+1);
dp.resize(n+1);
pre.resize(n+2);
suf.resize(n+2);
for (int i = 1; i <= n; i++) cin >> a[i];
sort(all(a));
a[0] = -1;
a.pb(2e6);
pre[0] = 0;
pre[1] = 1;
for (int i = 2; i <= n; i++) {
pre[i] = pre[i-1];
pre[i] += (a[i] - a[i-1]) * i + 1;
}
suf[n] = 1;
suf[n+1] = 0;
for (int i = n-1; i >= 1; i--) {
suf[i] = suf[i+1];
suf[i] += (a[i+1] - a[i]) * (n - i + 1) + 1;
}
for (int i = 1; i <= n; i++) {
int x = pre[i-1] + ((i) * (a[n] - a[i-1])) + ((i-1) * (a[n] - a[i])) + suf[i];
int y = suf[i+1] + ((n-i+1) * (a[i+1]-a[1])) + ((n-i) * (a[i]-a[1])) + pre[i];
dp[i] = {x, y};
}
int q; cin >> q;
while (q--) {
int s, e, t;
cin >> s >> e >> t;
int mn = 2e18;
int r = lb(all(a), e) - a.begin();
int l = r - 1;
if (l >= 1) {
mn = min(mn, abs(s - a[1]) + dp[l].F + ((n+1) * abs(a[l] - e)));
mn = min(mn, abs(s - a[n]) + dp[l].S + ((n+1) * abs(a[l] - e)));
}
if (r <= n) {
mn = min(mn, abs(s - a[1]) + dp[r].F + ((n+1) * abs(a[r] - e)));
mn = min(mn, abs(s - a[n]) + dp[r].S + ((n+1) * abs(a[r] - e)));
}
if (mn <= t) cout << "Yes\n";
else cout << "No\n";
}
}
signed main() {IOS solve(); 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... |