#include <bits/stdc++.h>
using namespace std;
#define ii pair<int,int>
vector<vector<int> >adj;
const int N = 1e5 + 5;
const int K = 18; /// K = log2N
int d[N],c[N];
int lift[N][K];
int sum[N][K];
void dfs(int idx,int p){
lift[idx][0] = p;
sum[idx][0] = c[idx];
for(int j = 1;j < K;j++){
lift[idx][j] = lift[lift[idx][j-1]][j-1];
sum[idx][j] = sum[idx][j-1] + sum[lift[idx][j-1]][j-1];
}
for(auto x:adj[idx]){
if(x != p){
dfs(x,idx);
}
}
}
int main(void){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,q;
cin>>n>>q;
adj.assign(n+5,vector<int>());
for(int i = 1;i <= n;i++){
cin>>d[i]>>c[i];
}
stack<ii>s;
for(int i = n;i >= 1;i--){
while(!s.empty() && s.top().first < d[i]){
s.pop();
}
if(s.empty()){
adj[i].push_back(0);
adj[0].push_back(i);
}
else{
adj[i].push_back(s.top().second);
adj[s.top().second].push_back(i);
}
s.push(ii(d[i],i));
}
dfs(0,0);
while(q--){
int r,v;
cin>>r>>v;
for(int j = K-1;j >= 0;j--){
if(sum[r][j] < v){
v -= sum[r][j];
r = lift[r][j];
}
}
cout<<r<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
305 ms |
27152 KB |
Output is correct |
2 |
Correct |
277 ms |
28820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |