#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double db;
#define int ll
#define all(x) (x).begin(), (x).end()
constexpr int maxn = 500'007;
constexpr ll inf = 1e18 + 7;
constexpr ll M = 1e9 + 7;
int binpow(int a, int n) {
if (n == 0)
return 1;
if (n & 1)
return a * binpow(a, n - 1) % M;
int x = binpow(a, n >> 1);
return x * x % M;
}
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const db eps = 0.00000000001;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
int n, l, x[maxn], q, dp[2][2002][2002][2], pref[maxn][2], suf[maxn][2];
void solve() {
cin >> n >> l;
l++;
for (int i = 1; i <= n; i++)
cin >> x[i];
sort(x + 1, x + n + 1);
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
for (int t1 = 0; t1 < 2; t1++) {
for (int t2 = 0; t2 < 2; t2++) {
dp[t1][i][j][t2] = inf;
}
}
}
}
dp[0][1][n][0] = 0;
dp[1][1][n][1] = 0;
for (int k = n; k > 1; k--) {
for (int i = 1; i + k - 1 <= n; i++) {
int j = i + k - 1;
for (int t = 0; t < 2; t++) {
dp[t][i + 1][j][0] = min(dp[t][i + 1][j][0], dp[t][i][j][0] + (x[i + 1] - x[i]) * (n - (k - 1) + 1));
// dp[t][i + 1][j][1] = min(dp[t][i + 1][j][1], dp[t][i][j][0] + (x[j] - x[i]) * (n - (k - 1) + 1));
// dp[t][i][j - 1][0] = min(dp[t][i][j - 1][0], dp[t][i][j][1] + (x[j] - x[i]) * (n - (k - 1) + 1));
dp[t][i][j - 1][1] = min(dp[t][i][j - 1][1], dp[t][i][j][1] + (x[j] - x[j - 1]) * (n - (k - 1) + 1));
}
}
}
for (int i = 1; i <= n; i++) {
for (int t = 0; t < 2; t++) {
dp[t][i][i][0] = dp[t][i][i][1] = min(dp[t][i][i][0], dp[t][i][i][1]);
}
}
pref[0][0] = pref[0][1] = inf;
for (int i = 1; i <= n; i++) {
pref[i][0] = min(dp[0][i][i][0] - x[i] * (n + 1) + n, pref[i - 1][0]);
pref[i][1] = min(dp[1][i][i][0] - x[i] * (n + 1) + n, pref[i - 1][1]);
}
suf[n + 1][0] = suf[n + 1][1] = inf;
for (int i = n; i >= 1; i--) {
suf[i][0] = min(dp[0][i][i][0] + x[i] * (n + 1) + n, suf[i + 1][0]);
suf[i][1] = min(dp[1][i][i][0] + x[i] * (n + 1) + n, suf[i + 1][1]);
}
cin >> q;
while (q--) {
int s, g, t;
cin >> s >> g >> t;
int l = 0, r = n + 1;
while (l + 1 < r) {
int mid = (l + r) / 2;
if (x[mid] <= g)
l = mid;
else
r = mid;
}
int res = min({pref[l][0] + g * (n + 1) + abs(s - x[1]), pref[l][1] + g * (n + 1) + abs(x[n] - s),
suf[l + 1][0] - g * (n + 1) + abs(s - x[1]), suf[l + 1][1] - g * (n + 1) + abs(x[n] - s)});
// cout << res << ' ';
if (res <= t)
cout << "Yes\n";
else
cout << "No\n";
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int test = 1;
// cin >> test;
while (test--) {
solve();
// cout << '\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... |