#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define nL '\n'
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
struct node {
ll p, idx, c;
ll d;
};
vector<node> G;
vvl up;
vvl sum;
void readInput(ll n, ll i) {
ll d;
ll c;
cin >> d >> c;
if (i < n)
readInput(n, i+1);
G[i] = {i, i, c, d};
ll j = i+1;
while (j > 0 && j <= n && G[i].d > G[j].d) {
j = G[j].p;
}
if (j > n)
j = 0;
G[i].p = j;
}
int main() {
#ifdef LOCAL
freopen("io/input.txt", "r", stdin);
freopen("io/output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
ll n, q;
cin >> n >> q;
G = vector<node>(n+1);
readInput(n, 1);
ll LOG = log2(n);
up = vvl(n+1, vl(LOG));
sum = vvl(n+1, vl(LOG));
for (ll v = n; v > 0; v--) {
up[v][0] = G[v].p;
sum[v][0] = G[v].c;
for (ll j = 1; j < LOG; j++) {
up[v][j] = up[up[v][j-1]][j-1];
sum[v][j] = sum[v][j-1] + sum[up[v][j-1]][j-1];
}
}
// for (int i = 1; i <= n; i++) {
// for (int j = 0; j < LOG; j++)
// cout << "(" << up[i][j] << ", " << sum[i][j] << ") ";
// cout << nL;
// }
while (q--) {
ll r;
ll v;
cin >> r >> v;
// while (v > 0 && r > 0) {
// if (v - G[r].c <= 0) {
// //cout << r << nL;
// break;
// }
// ll lo = 0, hi = LOG-1, res = 0, mid;
// while (lo <= hi) {
// mid = (lo+hi)/2;
// if (up[r][mid].second >= v) {
// res = mid;
// hi = mid - 1;
// } else
// lo = mid + 1;
// }
// //cout << res << nL;
// v -= up[r][res].second;
// r = up[r][res].first;
// }
for (ll i = LOG-1; i >= 0; i--) {
if (sum[r][i] < v) {
v -= sum[r][i];
r = up[r][i];
}
}
cout << (r <= n ? r : 0) << nL;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
335 ms |
41260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |