#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10, K = 20;
#define pb push_back
int n, qq, c[N], d[N], dp[N][K+1], go[N][K+1], a, b;
int main(){
cin.tie(0); ios::sync_with_stdio(0);
cin >> n >> qq;
for(int i = 1; i <= n; i++){
cin >> d[i] >> c[i];
}
deque<pair<int, int>> q;
q.pb({1e9+7, n + 1});
go[n + 1][0] = n + 1;
c[n + 1] = 0;
dp[n + 1][0] = 0;
for(int i = n; i >= 1; i--){
while(!q.empty() && q.back().first <= d[i]) q.pop_back();
go[i][0] = q.back().second;
dp[i][0] = c[i];
q.pb({d[i], i});
}
for(int k = 1; k <= K; k++){
for(int v = 1; v <= n+1; v++){
go[v][k] = go[go[v][k - 1]][k - 1];
dp[v][k] = dp[v][k - 1] + dp[go[v][k-1]][k-1];
// cout << go[v][k] << ' ' << v << ' ' << k << endl;
}
// cout << endl;
}
for(int i = 0; i < qq; i++){
cin >> a >> b;
int v = a;
if(c[v] >= b){
cout << v << '\n';
continue;
}
for(int i = K; i > 0; i--){
if(dp[v][i] < b) b -= dp[v][i], v = go[v][i];
// cout << v<<endl;
}
b -= dp[v][0];
if(dp[go[v][0]][0] >= b && b>0) v = go[v][0];
cout << (v==n+1?0:v) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
588 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
216 ms |
18204 KB |
Output is correct |
2 |
Correct |
230 ms |
16936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
588 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
216 ms |
18204 KB |
Output is correct |
9 |
Correct |
230 ms |
16936 KB |
Output is correct |
10 |
Correct |
2 ms |
464 KB |
Output is correct |
11 |
Correct |
122 ms |
12144 KB |
Output is correct |
12 |
Correct |
318 ms |
23536 KB |
Output is correct |
13 |
Correct |
246 ms |
22504 KB |
Output is correct |
14 |
Correct |
184 ms |
21552 KB |
Output is correct |
15 |
Correct |
148 ms |
21936 KB |
Output is correct |