#include <iostream>
#include <vector>
#include <utility>
#include <climits>
using namespace std;
int diameters[100000];
int down[100000][20];
int sum[100000][20];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, q; cin >> n >> q;
for (int i = 0; i < n; i++) cin >> diameters[i] >> sum[i][0];
vector<pair<int, int>> st = { { INT_MAX, -1 } };
for (int i = n - 1; i >= 0; i--) {
while (st.back().first <= diameters[i]) st.pop_back();
down[i][0] = st.back().second;
st.push_back({ diameters[i], 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;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
244 ms |
17568 KB |
Output is correct |
2 |
Correct |
267 ms |
16420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
464 KB |
Output is correct |
8 |
Correct |
244 ms |
17568 KB |
Output is correct |
9 |
Correct |
267 ms |
16420 KB |
Output is correct |
10 |
Correct |
3 ms |
472 KB |
Output is correct |
11 |
Correct |
134 ms |
11556 KB |
Output is correct |
12 |
Correct |
378 ms |
20164 KB |
Output is correct |
13 |
Correct |
405 ms |
19624 KB |
Output is correct |
14 |
Correct |
283 ms |
19260 KB |
Output is correct |
15 |
Correct |
298 ms |
20576 KB |
Output is correct |