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 <iostream>
using namespace std;
/*
6 5
4 10
6 8
3 5
4 14
10 9
4 20
1 25
6 30
5 8
3 13
2 8
*/
int main() {
int n, q; cin >> n >> q;
int diameters[n];
int capacities[n];
for (int i = 0; i < n; i++) cin >> diameters[i] >> capacities[i];
int next[n][20];
int sum[n][20];
for (int i = 0; i < n; i++) {
next[i][0] = -1;
sum[i][0] = capacities[i];
for (int j = i + 1; j < n; j++) {
if (diameters[j] > diameters[i]) {
next[i][0] = j;
break;
}
}
}
for (int j = 1; j < 20; j++) {
for (int i = 0; i < n; i++) {
if (next[i][j - 1] == -1) {
next[i][j] = -1;
sum[i][j] = sum[i][j - 1];
continue;
}
next[i][j] = next[next[i][j - 1]][j - 1];
sum[i][j] = sum[i][j - 1] + sum[next[i][j - 1]][j - 1];
}
}
for (int i = 0; i < q; i++) {
int r, v; cin >> r >> v; r--;
for (int i = 19; i >= 0; i--) {
if (sum[r][i] >= v) continue;
v -= sum[r][i];
r = next[r][i];
if (r == -1) break;
}
cout << r + 1 << 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... |