#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/**
* Problem: Marathon Race 2 (JOI 2023/2024 Final Round)
* Complexity: O((N + Q) log N) due to sorting and binary search queries.
*/
typedef long long ll;
int main() {
// Optimize I/O performance
ios::sync_with_stdio(false);
cin.tie(NULL);
int N;
ll L;
if (!(cin >> N >> L)) return 0;
// Read and sort ball positions
vector<ll> X(N);
for (int i = 0; i < N; i++) cin >> X[i];
sort(X.begin(), X.end());
// Precompute prefix sums for O(1) range sum queries
vector<ll> pref(N + 1, 0);
for (int i = 0; i < N; i++) {
pref[i + 1] = pref[i] + X[i];
}
int Q;
cin >> Q;
while (Q--) {
ll S, G, T;
cin >> S >> G >> T;
ll Xmin = X[0];
ll Xmax = X[N - 1];
// Path 1 Cost: S -> Xmin -> Xmax -> G
// Path distance to G for a ball at X[i]:
// If X[i] >= G, Rie can collect it on the final leg (Xmax -> G) with distance |X[i] - G|
// If X[i] < G, Rie collects it on the middle leg (Xmin -> Xmax) with distance (Xmax - X[i]) + |Xmax - G|
ll L1 = abs(S - Xmin) + (Xmax - Xmin) + abs(Xmax - G);
int k1 = lower_bound(X.begin(), X.end(), G) - X.begin(); // First index i where X[i] >= G
ll D1 = 0;
// Elements i < k1: X[i] < G
D1 += (ll)k1 * Xmax - pref[k1] + (ll)k1 * abs(Xmax - G);
// Elements i >= k1: X[i] >= G
D1 += (pref[N] - pref[k1]) - (ll)(N - k1) * G;
ll T1 = N + L1 + D1;
// Path 2 Cost: S -> Xmax -> Xmin -> G
// If X[i] <= G, distance |G - X[i]| (collected on final leg Xmin -> G)
// If X[i] > G, distance (X[i] - Xmin) + |Xmin - G| (collected on middle leg Xmax -> Xmin)
ll L2 = abs(S - Xmax) + (Xmax - Xmin) + abs(Xmin - G);
int k2 = upper_bound(X.begin(), X.end(), G) - X.begin() - 1; // Last index i where X[i] <= G
ll D2 = 0;
// Elements i <= k2: X[i] <= G
D2 += (ll)(k2 + 1) * G - pref[k2 + 1];
// Elements i > k2: X[i] > G
D2 += (pref[N] - pref[k2 + 1]) - (ll)(N - 1 - k2) * Xmin + (ll)(N - 1 - k2) * abs(Xmin - G);
ll T2 = N + L2 + D2;
// Determine if either path satisfies the time limit
if (min(T1, T2) <= T) {
cout << "Yes\n";
} else {
cout << "No\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... |