#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;
vector<ll> cnt1;
vector<ll> cnt2;
ll get(ll x, ll y){
ll res=1;
if(x > 0){
res += cnt1[x-1];
}
if(y+1 < cnt2.size()){
res += cnt2[y+1];
}
return res;
}
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])*get(x, y));
}
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])*get(x, y));
}
}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])*get(x, y));
}
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])*get(x, y));
}
}
return dp[x][y][type];
}
void solve(){
ll n, q, l, i, x, y, time, res, pos;
cin>>n>>l;
vector<ll> a;
map<ll, ll> m;
for(i=0;i<n;++i){
cin>>x;
++m[x];
}
if(m.size() > 3000){
cin>>q;
for(i=0;i<q;++i){
cin>>x>>y>>time;
cout<<"No"<<endl;
}
return;
}
for(auto el: m){
a.pb(el.first);
cnt1.pb(el.second);
cnt2.pb(el.second);
}
n = a.size();
for(i=1;i<n;++i){
cnt1[i] += cnt1[i-1];
}
for(i=n-2;i>=0;--i){
cnt2[i] += cnt2[i+1];
}
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;
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]*(cnt1.back()+1);
d2[0] = min(calc(0, 0, 0, dp2, a), calc(0, 0, 1, dp2, a)) + -a[0]*(cnt1.back()+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]*(cnt1.back()+1));
d2[i] = min(d2[i-1], min(calc(i, i, 0, dp2, a), calc(i, i, 1, dp2, a)) + -a[i]*(cnt1.back()+1));
}
c1[n-1] = min(calc(n-1, n-1, 0, dp, a), calc(n-1, n-1, 1, dp, a)) + a[n-1]*(cnt1.back()+1);
c2[n-1] = min(calc(n-1, n-1, 0, dp2, a), calc(n-1, n-1, 1, dp2, a)) + a[n-1]*(cnt1.back()+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]*(cnt1.back()+1));
c2[i] = min(c2[i+1], min(calc(i, i, 0, dp2, a), calc(i, i, 1, dp2, a)) + a[i]*(cnt1.back()+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*(cnt1.back()+1) + abs(x - a[0]));
res = min(res, c2[pos] - y*(cnt1.back()+1) + abs(x - a.back()));
}
--pos;
if(pos >= 0){
res = min(res, d1[pos] + y*(cnt1.back()+1) + abs(x - a[0]));
res = min(res, d2[pos] + y*(cnt1.back()+1) + abs(x - a.back()));
}
res += cnt1.back();
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... |