#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define all(aa) aa.begin(), aa.end()
int main(){
int n, q;
cin>>n>>q;
vector<pair<int, int>> a(n);
for(auto &[d, c]:a) cin>>d>>c;
vector<vector<pair<int, int>>> par(n+1, vector<pair<int, int>>(20));
stack<int> s;
for(int i = n; i > 0; i--){
while(s.size() && a[s.top()-1].first<=a[i-1].first) s.pop();
par[i][0]={(s.empty() ? 0:s.top()), a[i-1].second};
s.push(i);
}
for(int i = 1; i < 20; i++) for(int v = 1; v <= n; v++)
par[v][i]={par[par[v][i-1].first][i-1].first, par[par[v][i-1].first][i-1].second+par[v][i-1].second};
while(q--){
int v, d;
cin>>v>>d;
int l=0, r=n+1;
while(l<r){
int mid=(l+r)/2, cur=0, dist=0, nd=v;
for(int i = 19; i >= 0; i--) if(cur+(1<<i)<=mid){
cur+=(1<<i);
dist+=par[nd][i].second;
nd=par[nd][i].first;
}
if(dist<d) l=mid+1;
else r=mid;
}
l--;
for(int i = 19; i >= 0; i--) if(l-(1<<i)>=0)
l-=(1<<i), v=par[v][i].first;
cout<<v<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
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 |
5 ms |
460 KB |
Output is correct |
5 |
Correct |
10 ms |
532 KB |
Output is correct |
6 |
Correct |
7 ms |
460 KB |
Output is correct |
7 |
Correct |
7 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1579 ms |
23612 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
5 ms |
460 KB |
Output is correct |
5 |
Correct |
10 ms |
532 KB |
Output is correct |
6 |
Correct |
7 ms |
460 KB |
Output is correct |
7 |
Correct |
7 ms |
460 KB |
Output is correct |
8 |
Execution timed out |
1579 ms |
23612 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |