#include <iostream>
#include <vector>
#include <utility>
#include <climits>
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 next[n][20];
int sum[n][20];
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();
next[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 (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 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 = next[r][i];
if (r == -1) break;
}
cout << r + 1 << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
468 KB |
Output is correct |
6 |
Correct |
4 ms |
468 KB |
Output is correct |
7 |
Correct |
4 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
338 ms |
17128 KB |
Output is correct |
2 |
Correct |
348 ms |
15936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
468 KB |
Output is correct |
6 |
Correct |
4 ms |
468 KB |
Output is correct |
7 |
Correct |
4 ms |
340 KB |
Output is correct |
8 |
Correct |
338 ms |
17128 KB |
Output is correct |
9 |
Correct |
348 ms |
15936 KB |
Output is correct |
10 |
Correct |
4 ms |
340 KB |
Output is correct |
11 |
Correct |
226 ms |
9844 KB |
Output is correct |
12 |
Correct |
451 ms |
18420 KB |
Output is correct |
13 |
Correct |
423 ms |
17688 KB |
Output is correct |
14 |
Correct |
370 ms |
17404 KB |
Output is correct |
15 |
Correct |
374 ms |
19240 KB |
Output is correct |