Submission #895807

#TimeUsernameProblemLanguageResultExecution timeMemory
895807tvladm2009Fountain (eJOI20_fountain)C++17
100 / 100
308 ms40744 KiB
#include <bits/stdc++.h> #define int ll using namespace std; typedef long long ll; const int LOG = 20; signed main() { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; vector<int> d(n + 2), c(n + 2); for (int i = 1; i <= n; ++i) cin >> d[i] >> c[i]; vector<int> l(n + 1), stk; for (int i = n; i >= 1; --i) { while (!stk.empty() && d[i] >= d[stk.back()]) { stk.pop_back(); } if (stk.empty()) { l[i] = 0; } else { l[i] = stk.back(); } stk.push_back(i); } vector<vector<int>> anc(LOG, vector<int>(n + 1)); vector<vector<int>> sum(LOG, vector<int>(n + 1, 0LL)); for (int i = 1; i <= n; ++i) { anc[0][i] = l[i]; sum[0][i] = c[l[i]]; } for (int h = 1; h < LOG; ++h) { for (int i = 1; i <= n; ++i) { anc[h][i] = anc[h - 1][anc[h - 1][i]]; sum[h][i] = sum[h - 1][i] + sum[h - 1][anc[h - 1][i]]; } } while (q--) { int r, cap; cin >> r >> cap; if (c[r] >= cap) { cout << r << "\n"; continue; } cap -= c[r]; ll cur = 0; for (int k = LOG - 1; k >= 0; --k) { if (anc[k][r] != 0 && cur + sum[k][r] < cap) { cur += sum[k][r]; r = anc[k][r]; } } if (l[r] == n + 1) { cout << "0\n"; continue; } cout << l[r] << "\n"; } return 0; } /** 6 5 4 10 6 8 3 5 4 14 10 9 4 20 1 25 6 30 5 8 3 13 2 8 **/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...