# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
772757 | BlockOG | Fountain (eJOI20_fountain) | C++14 | 0 ms | 0 KiB |
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;
int diameters[100000];
int down[100000][20];
int sum[100000][20];
int st[100001][2];
int sp = 1;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
st[0][0] = INT_MAX;
st[0][1] = -1;
int n, q; cin >> n >> q;
for (int i = 0; i < n; i++) cin >> diameters[i] >> sum[i][0];
for (int i = n - 1; i >= 0; i--) {
while (st[sp - 1][0] <= diameters[i]) sp--;
down[i][0] = st[sp - 1][1];
st[sp][0] = diameters[i];
st[sp++][1] = i;
}
for (int j = 1; j < 20; j++) {
for (int i = 0; i < n; i++) {
if (down[i][j - 1] == -1) {
down[i][j] = -1;
sum[i][j] = sum[i][j - 1];
continue;
}
down[i][j] = down[down[i][j - 1]][j - 1];
sum[i][j] = sum[i][j - 1] + sum[down[i][j - 1]][j - 1];
}
}
for (int it = 0; it < q; it++) {
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 = down[r][i];
if (r == -1) break;
}
cout << r + 1 << endl;
}
}