#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define s second
#define f first
const ll N = 1e5 + 10, K = 17, MAXX = 1e18;
int n;
pair<int, ll> jump[N][K + 10];
vector <pair<ll, ll>> V;
stack <ll> stos;
ll jump_(int v, ll x) {
int k = K, suma = 0;
while(suma < x && k >= 0) {
if(jump[v][k].second + suma < x) {suma += jump[v][k].second; v = jump[v][k].first;}
k--;
}
if(suma < x) {suma += jump[v][0].second; v = jump[v][0].first;}
if(v == n) return -1;
return v;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int m; cin >> n >> m;
for(int i = 0; i < n; i++) {
int d, c; cin >> d >> c;
V.push_back({d, c});
}
for(int i = V.size() - 1; i >= 0; i--) {
ll a = V[i].f;
while(!stos.empty() && V[stos.top()].f < a) stos.pop();
if(!stos.empty()) jump[i][0] = {stos.top(), V[stos.top()].s};
else jump[i][0] = {n, MAXX};
stos.push(i);
}
for(int i = 0; i <= K; i++) jump[n][i] = {n, 0};
for(int j = 1; j <= K; j++) {
for(int i = 0; i < n; i++) {
jump[i][j] = {jump[jump[i][j - 1].f][j - 1].f, jump[i][j - 1].s + jump[jump[i][j - 1].f][j - 1].s};
jump[i][j].s = min(MAXX, jump[i][j].s);
}
}
while(m--) {
ll r, v; cin >> r >> v;
r--; v -= V[r].s;
cout << jump_(r, v) + 1 << '\n';
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |