Submission #874253

#TimeUsernameProblemLanguageResultExecution timeMemory
874253prvocisloBitaro's travel (JOI23_travel)C++17
100 / 100
96 ms5004 KiB
#include <algorithm> #include <iostream> #include <set> #include <string> #include <vector> typedef long long ll; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> d(n), li(n, -1), ri(n, -1); for (int i = 0; i < n; i++) cin >> d[i]; set<pair<int, int> > id; for (int i = n - 2; i >= 0; i--) { auto it = id.upper_bound({ d[i], n }); ri[i + 1] = (it == id.begin() ? n - 1 : prev(it)->second); pair<int, int> nw = { 2 * d[i] - d[i + 1], i }; while (!id.empty() && id.rbegin()->first >= nw.first) id.erase(*id.rbegin()); id.insert(nw); } ri[0] = n - 1; id.clear(); for (int i = 0; i + 1 < n; i++) { auto it = id.upper_bound({ d[i + 1], n }); li[i] = (it == id.end() ? 0 : it->second); pair<int, int> nw = { 2 * d[i + 1] - d[i], i + 1 }; while (!id.empty() && id.begin()->first <= nw.first) id.erase(*id.begin()); id.insert(nw); } li[n - 1] = 0; int q; cin >> q; while (q--) { int x; cin >> x; int i = lower_bound(d.begin(), d.end(), x) - d.begin(); if (i == n || (i && x - d[i - 1] <= d[i] - x)) i--; ll ans = abs(d[i] - x); while (i && i != n - 1) { if (i != ri[i]) ans += (ll)(d[ri[i]] - d[i]), i = ri[i]; else ans += (ll)(d[i] - d[li[i]]), i = li[i]; } ans += d[n - 1] - d[0]; cout << ans << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...