#include <bits/stdc++.h>
#define suiii ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define co cout<<
// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,sse3,sse4,avx")
using namespace std;
//stuff
const ll maxn=1000001;
ll n,q;
ll arr[maxn],val[maxn];
ll f[maxn][22][2];
ll bigger[maxn];
void make_spars(ll x){
if(x==0){
for(int i=1;i<=n;i++){
f[i][x][0]=val[i];
f[i][x][1]=bigger[i];
}
}
else{
for(int i=1;i<=n;i++){
f[i][x][0]=f[i][x-1][0]+f[f[i][x-1][1]][x-1][0];
f[i][x][1]=f[f[i][x-1][1]][x-1][1];
}
}
}
void solve(){
cin>>n>>q;
vector<ll>v;
for(int i=1;i<=n;i++){
cin>>arr[i]>>val[i];
}
for(int i=n;i>=1;i--){
while(v.size()&&arr[v.back()]<=arr[i]) v.pop_back();
if(v.size()) bigger[i]=v.back();
else bigger[i]=0;
v.push_back(i);
}
for(int i=0;i<=21;i++) make_spars(i);
while(q--){
ll a,b;
cin>>a>>b;
ll idx=21;
while(idx>=0){
if(a==0) break;
if(b-f[a][idx][0]>0&&f[a][idx][0]!=0){
b-=f[a][idx][0];
a=f[a][idx][1];
}
idx--;
}
co a<<'\n';
}
}
int main()
{
suiii
int t=1;
// cin>>t;
while(t--){
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
43080 KB |
Output is correct |
2 |
Correct |
80 ms |
41164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
78 ms |
43080 KB |
Output is correct |
9 |
Correct |
80 ms |
41164 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
36 ms |
29948 KB |
Output is correct |
12 |
Correct |
146 ms |
45840 KB |
Output is correct |
13 |
Correct |
102 ms |
45016 KB |
Output is correct |
14 |
Correct |
79 ms |
44884 KB |
Output is correct |
15 |
Correct |
65 ms |
44880 KB |
Output is correct |