Submission #1096990

#TimeUsernameProblemLanguageResultExecution timeMemory
1096990raphaelpFountain (eJOI20_fountain)C++14
100 / 100
458 ms20424 KiB
#include <bits/stdc++.h> using namespace std; int main() { int N, Q; cin >> N >> Q; vector<int> D(N + 1), C(N + 1); for (int i = 1; i <= N; i++) { cin >> D[i] >> C[i]; } vector<vector<int>> lca(log2(N) + 1, vector<int>(N + 1)), lca2(log2(N) + 1, vector<int>(N + 1)); stack<pair<int, int>> S; S.push({0, 1000000002}); for (int i = N; i > 0; i--) { while (S.top().second <= D[i]) S.pop(); lca[0][i] = S.top().first; lca2[0][i] = C[i]; S.push({i, D[i]}); } lca[0][0] = 0; lca2[0][0] = 0; for (int i = 1; i <= log2(N); i++) { for (int j = 0; j <= N; j++) { lca[i][j] = lca[i - 1][lca[i - 1][j]]; lca2[i][j] = lca2[i - 1][j] + lca2[i - 1][lca[i - 1][j]]; } } for (int i = 0; i < Q; i++) { int r, v; cin >> r >> v; for (int i = log2(N); i >= 0; i--) { if (lca2[i][r] < v) { v -= lca2[i][r]; r = lca[i][r]; } } cout << r << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...