This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |