#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL;
const ll M = 1000LL * 1000LL * 1000LL + 7LL;
const int N = 2 * 1000 + 7;
ll dp[2][N][N], tab[N], spr[N];
int wgh[N];
inline ll D(int i, int j)
{
if(i > j) swap(i, j);
return tab[j] - tab[i];
}
ll Ans(int p, int k, int n)
{
if(n > 2000) return I;
int i = (upper_bound(tab + 1, tab + 1 + n, k) - tab) - 1;
ll ans = I;
if(i > 0)
{
ans = min(ans, (ll)(wgh[n] + 1) * (ll)(k - tab[i]) + dp[0][i][i + 1] + (ll)abs(tab[1] - p));
ans = min(ans, (ll)(wgh[n] + 1) * (ll)(k - tab[i]) + dp[1][i][i + 1] + (ll)abs(tab[n] - p));
}
if(i < n)
{
ans = min(ans, (ll)(wgh[n] + 1) * (ll)(tab[i + 1] - k) + dp[0][i + 1][i] + (ll)abs(tab[1] - p));
ans = min(ans, (ll)(wgh[n] + 1) * (ll)(tab[i + 1] - k) + dp[1][i + 1][i] + (ll)abs(tab[n] - p));
}
return ans;
}
void DP(int n)
{
dp[1][1][n + 1] = I;
dp[1][n + 1][1] = I;
dp[0][1][n + 1] = wgh[1];
dp[0][n][0] = I;
dp[0][0][n] = I;
dp[1][n][0] = wgh[n] - wgh[n - 1];
for(int d = n - 1; d >= 1; --d)
{
for(int i = 0; i <= n - d + 1; ++i)
{
int j = i + d; ll wi = wgh[i] - wgh[i - 1], wj = wgh[j] - wgh[j - 1];
ll il = wgh[i] + (wgh[n] - wgh[j - 1]) + 1LL;
dp[0][i][j] = I;
dp[1][i][j] = I;
dp[0][j][i] = I;
dp[1][j][i] = I;
if(i > 0)
{
il -= wi;
for(int r = 0; r < 2; ++r)
{
if(i - 1 > 0)
dp[r][i][j] = min(dp[r][i][j], dp[r][i - 1][j] + D(i, i - 1) * il + wi);
if(j <= n)
dp[r][i][j] = min(dp[r][i][j], dp[r][j][i - 1] + D(i, j) * il + wi);
}
il += wi;
}
if(j <= n)
{
il -= wj;
for(int r = 0; r < 2; ++r)
{
if(j < n)
dp[r][j][i] = min(dp[r][j][i], dp[r][j + 1][i] + D(j, j + 1) * il + wj);
if(i > 0)
dp[r][j][i] = min(dp[r][j][i], dp[r][i][j + 1] + D(j, i) * il + wj);
}
il += wj;
}
//cout << i << " " << j << " " << il << " " << D(j, j + 1) << "\n";
//cout << dp[0][i][j] << " " << dp[1][i][j] << " " << dp[0][j][i] << " " << dp[1][j][i] << "\n";
}
}
}
void Solve()
{
int n, q, xd; ll l;
cin >> n >> l;
for(int i = 1; i <= n; ++i)
cin >> tab[i];
sort(tab + 1, tab + 1 + n);
xd = n;
n = 0;
tab[0] = -3;
for(int i = 1; i <= xd; ++i)
{
if(tab[i] != tab[n])
{
++n;
tab[n] = tab[i];
}
++wgh[n];
}
for(int i = 1; i <= n; ++i)
wgh[i] += wgh[i - 1];
if(n <= 2000)
DP(n);
cin >> q;
for(int i = 1; i <= q; ++i)
{
int p, k, t;
cin >> p >> k >> t;
ll ans = Ans(p, k, n);
//cout << ans << "\n";
if(ans > (ll)t)
cout << "No\n";
else
cout << "Yes\n";
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//int t; cin >> t;
//while(t--)
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... |