#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef pair <int, int> pii;
const int MAXN = 1e5 + 5;
int n, q;
pii info[MAXN], child_pow2[MAXN][20];
vector <pii> available;
int main()
{
cin >> n >> q;
for(int i = 1; i <= n; i++) cin >> info[i].f >> info[i].s;
for(int i = n; i >= 1; i--){
while(!available.empty() && available.back().f <= info[i].f) available.pop_back();
if(available.empty()) child_pow2[i][0] = {0, info[i].s};
else child_pow2[i][0] = {available.back().s, info[i].s};
available.push_back({info[i].f, i});
}
for(int j = 1; j < 20; j++){
for(int i = 1; i <= n; i++){
pii child = child_pow2[child_pow2[i][j - 1].f][j - 1];
child_pow2[i][j] = {child.f, child_pow2[i][j - 1].s + child.s};
}
}
while(q--){
int ind, amount;
cin >> ind >> amount;
for(int i = 19; i >= 0; i--){
if(amount > child_pow2[ind][i].s){
amount -= child_pow2[ind][i].s;
ind = child_pow2[ind][i].f;
}
}
cout << ind << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
4 ms |
460 KB |
Output is correct |
5 |
Correct |
6 ms |
460 KB |
Output is correct |
6 |
Correct |
6 ms |
460 KB |
Output is correct |
7 |
Correct |
5 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
506 ms |
17472 KB |
Output is correct |
2 |
Correct |
559 ms |
16228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
4 ms |
460 KB |
Output is correct |
5 |
Correct |
6 ms |
460 KB |
Output is correct |
6 |
Correct |
6 ms |
460 KB |
Output is correct |
7 |
Correct |
5 ms |
460 KB |
Output is correct |
8 |
Correct |
506 ms |
17472 KB |
Output is correct |
9 |
Correct |
559 ms |
16228 KB |
Output is correct |
10 |
Correct |
8 ms |
460 KB |
Output is correct |
11 |
Correct |
292 ms |
10172 KB |
Output is correct |
12 |
Correct |
786 ms |
18864 KB |
Output is correct |
13 |
Correct |
713 ms |
18176 KB |
Output is correct |
14 |
Correct |
580 ms |
17820 KB |
Output is correct |
15 |
Correct |
589 ms |
17796 KB |
Output is correct |