#include<bits/stdc++.h>
using namespace std;
#define Study ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
#define ull unsigned long long
#define pb push_back
#define ff first
#define ss second
#define ins insert
#define all(x) x.begin(),x.end()
#define fori(x,y,z) for(ll x=y;x<=z;x++)
const ll INF=1e9;
const ll sz=2e5+10;
const ll mod=1e9+7;
void work(){
ll n,q;
cin>>n>>q;
vector<pair<ll,ll>>v(n+1);
vector<ll>pref(n+10,0);
v[0].ss=-1;v[0].ff=-1;
bool yes=0;
fori(i,1,n){
ll d,x;
cin>>d>>x;
v[i].ff=d;
v[i].ss=x;
pref[i]=pref[i-1]+x;
if(v[i].ff<=v[i-1].ff){
yes=1;
}
}
if(yes){
while(q--){
ll d,l;
cin>>d>>l;
ll sum=v[d].ss;
ll maxr=v[d].ff;
d++;
while(d<=n and sum<l){
if(v[d].ff>maxr){
sum+=v[d].ss;
maxr=v[d].ff;
}
d++;
}
if(sum>=l)
d--;
//cout<<d<<' '<<sum<<endl;
if(sum<l and d>=n)
cout<<0<<endl;
else
cout<<d<<endl;
}
}
else{
while(q--){
ll d,x;
cin>>d>>x;
ll l=d;
ll r=n;
ll ans=0;
while(l<=r){
ll mid=(l+r)/2;
if(pref[mid]-pref[d-1]>=x){
r=mid-1;
ans=mid;
}
else
l=mid+1;
}
if(pref[l]-pref[d-1]>x){
cout<<0<<endl;
}
else{
cout<<ans<<endl;
}
}
}
}
int main(){
Study;
ll t=1;
//cin>>t;
while(t--){
work();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |