#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<pair<int, int>> containers(n + 1);
containers[0] = {1e9, 1e9};
for (int i = 1; i <= n; i++)
{
cin >> containers[i].first;
cin >> containers[i].second;
}
vector<pair<int, int>> questions(q);
for (int i = 0; i < q; i++)
{
cin >> questions[i].first;
cin >> questions[i].second;
}
vector<pair<int, int>> containers_overflow;
int max_pow = 30;
vector<vector<pair<int, long long>>> jump(n + 1, vector<pair<int, long long>>(max_pow + 1));
containers_overflow.push_back({0, 1e9});
int current_container;
for (int i = n; i >= 1; i--)
{
while (containers[i].first >= containers_overflow.back().second)
{
containers_overflow.pop_back();
}
current_container = containers_overflow.back().first;
jump[i][0] = {current_container, containers[current_container].second};
containers_overflow.push_back({i, containers[i].first});
}
int where, capacity;
for (int l = 1; l <= max_pow; l++)
{
for (int v = n; v >= 1; v--)
{
where = jump[jump[v][l - 1].first][l - 1].first;
if (where == 0)
{
capacity = 1e9;
}
else
{
capacity = jump[v][l - 1].second + jump[jump[v][l - 1].first][l - 1].second;
}
jump[v][l] = {where, capacity};
}
}
int wasser;
int start;
for (int i = 0; i < q; i++)
{
start = questions[i].first;
wasser = questions[i].second;
wasser = max(wasser - containers[start].second, 0);
for (int p = max_pow; p >= 0; p--)
{
if (jump[start][p].second <= wasser)
{
wasser -= jump[start][p].second;
start = jump[start][p].first;
}
}
if (wasser > 0)
{
start = jump[start][0].first;
}
cout << start << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |