#include<bits/stdc++.h>
#define int long long
#define MOD 1000000007
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
const int inf=1e15;
int par[100005][20],arr[100005][20];
int32_t main(){
int n,q;
cin>>n>>q;
int a[n+5],b[n+5];
for(int i=1;i<=n;i++){
cin>>a[i]>>b[i];
}
a[n+1]=inf;
b[n+1]=inf;
for(int i=1;i<=n+1;i++)arr[i][0]=b[i];
stack<int>s;
for(int i=1;i<=n+1;i++){
while(!s.empty() && a[s.top()]<a[i]){
par[s.top()][0]=i;
s.pop();
}
s.push(i);
}
for(int j=1;j<=19;j++){
for(int i=1;i<=n+1;i++){
par[i][j]=par[par[i][j-1]][j-1];
arr[i][j]=arr[i][j-1]+arr[par[i][j-1]][j-1];
}
}
while(q--){
int x,y;
cin>>x>>y;
for(int i=19;i>=0;i--){
if(y>arr[x][i]){
y-=arr[x][i];
x=par[x][i];
}
}
cout<<(x==n+1?0:x)<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
448 KB |
Output is correct |
3 |
Correct |
2 ms |
452 KB |
Output is correct |
4 |
Correct |
4 ms |
596 KB |
Output is correct |
5 |
Correct |
5 ms |
596 KB |
Output is correct |
6 |
Correct |
4 ms |
596 KB |
Output is correct |
7 |
Correct |
5 ms |
576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
510 ms |
35200 KB |
Output is correct |
2 |
Correct |
555 ms |
32956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
448 KB |
Output is correct |
3 |
Correct |
2 ms |
452 KB |
Output is correct |
4 |
Correct |
4 ms |
596 KB |
Output is correct |
5 |
Correct |
5 ms |
596 KB |
Output is correct |
6 |
Correct |
4 ms |
596 KB |
Output is correct |
7 |
Correct |
5 ms |
576 KB |
Output is correct |
8 |
Correct |
510 ms |
35200 KB |
Output is correct |
9 |
Correct |
555 ms |
32956 KB |
Output is correct |
10 |
Correct |
5 ms |
596 KB |
Output is correct |
11 |
Correct |
293 ms |
20768 KB |
Output is correct |
12 |
Correct |
777 ms |
38336 KB |
Output is correct |
13 |
Correct |
708 ms |
38028 KB |
Output is correct |
14 |
Correct |
556 ms |
37320 KB |
Output is correct |
15 |
Correct |
512 ms |
38312 KB |
Output is correct |