#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <unordered_map>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <iterator>
#include <random>
#define ll long long
#define ld long double
#define inf (ll)(2*1e18)
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define endl "\n"
using namespace std;
ll calc(ll x, ll y, ll type, vector<vector<vector<ll>>>& dp, vector<ll>& a){
if(dp[x][y][type] != -1) return dp[x][y][type];
dp[x][y][type] = inf;
if(type){
if(x > 0){
dp[x][y][type] = min(dp[x][y][type], calc(x-1, y, 0, dp, a) + abs(a[x-1] - a[y])*((ll)a.size() - y + x));
}
if(y < a.size() - 1){
dp[x][y][type] = min(dp[x][y][type], calc(x, y+1, 1, dp, a) + abs(a[y] - a[y+1])*((ll)a.size() - y + x));
}
}else{
if(x > 0){
dp[x][y][type] = min(dp[x][y][type], calc(x-1, y, 0, dp, a) + abs(a[x-1] - a[x])*((ll)a.size() - y + x));
}
if(y < a.size() - 1){
dp[x][y][type] = min(dp[x][y][type], calc(x, y+1, 1, dp, a) + abs(a[x] - a[y+1])*((ll)a.size() - y + x));
}
}
return dp[x][y][type];
}
void solve(){
ll n, q, l, cnt=0, i, x, y, time, res, x1, x2, dis, pos;
cin>>n>>l;
vector<ll> a(n);
for(i=0;i<n;++i){
cin>>a[i];
}
sort(a);
vector<vector<vector<ll>>> dp(n, vector<vector<ll>>(n, vector<ll>(2, -1)));
dp[0][n-1][1] = 0;
dp[0][n-1][0] = inf;
vector<vector<vector<ll>>> dp2(n, vector<vector<ll>>(n, vector<ll>(2, -1)));
swap(dp, dp2);
dp[0][n-1][0] = 0;
dp[0][n-1][1] = inf;
calc(2, 2, 0, dp, a);
vector<ll> c1(n);
vector<ll> c2(n);
vector<ll> d1(n);
vector<ll> d2(n);
d1[0] = min(calc(0, 0, 0, dp, a), calc(0, 0, 1, dp, a)) + -a[0]*(n+1);
d2[0] = min(calc(0, 0, 0, dp2, a), calc(0, 0, 1, dp2, a)) + -a[0]*(n+1);
for(i=1;i<n;++i){
d1[i] = min(d1[i-1], min(calc(i, i, 0, dp, a), calc(i, i, 1, dp, a)) + -a[i]*(n+1));
d2[i] = min(d2[i-1], min(calc(i, i, 0, dp2, a), calc(i, i, 1, dp2, a)) + -a[i]*(n+1));
}
c1[n-1] = min(calc(n-1, n-1, 0, dp, a), calc(n-1, n-1, 1, dp, a)) + a[n-1]*(n+1);
c2[n-1] = min(calc(n-1, n-1, 0, dp2, a), calc(n-1, n-1, 1, dp2, a)) + a[n-1]*(n+1);
for(i=n-2;i>=0;--i){
c1[i] = min(c1[i+1], min(calc(i, i, 0, dp, a), calc(i, i, 1, dp, a)) + a[i]*(n+1));
c2[i] = min(c2[i+1], min(calc(i, i, 0, dp2, a), calc(i, i, 1, dp2, a)) + a[i]*(n+1));
}
cin>>q;
for(i=0;i<q;++i){
cin>>x>>y>>time;
res = inf;
pos = lower_bound(a.begin(), a.end(), y) - a.begin();
if(pos < n){
res = min(res, c1[pos] - y*(n+1) + abs(x - a[0]));
res = min(res, c2[pos] - y*(n+1) + abs(x - a.back()));
}
--pos;
if(pos >= 0){
res = min(res, d1[pos] + y*(n+1) + abs(x - a[0]));
res = min(res, d2[pos] + y*(n+1) + abs(x - a.back()));
}
res += n;
if(res <= time){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
srand(time(nullptr));
ll t=1;
// cin>>t;
for(;t>0;--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... |