#include <bits/stdc++.h>
#define int long long
#define ll long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<char,char> pcc;
typedef pair<int,pii> ipii;
typedef pair<pii,pii> ipiii;
const int MAXN = 2e5+20;
const int SQRT = 610;
const int MAXA = 50;
const int MOD = 1e9+7;
const int INF = 1e9+10;
const int LOG = 21;
const ld EPS = 1e-12;
int n,q,a[MAXN],b[MAXN],ans[MAXN];
vector <pii> vec[MAXN];
vector <pii> non;
struct seg {
    int st[MAXN];
    int que(int x){
        int te = 0;
        for(; x>=1; x-=(x&(-x))) te += st[x];
        return te;
    }
    void upd(int x,int y){
        for(; x<=MAXN-10; x+=(x&(-x))) st[x] += y;
    }
} A;
signed main(){
    // ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n>>q;
    for(int i=1; i<=n; i++) cin>>a[i]>>b[i];
    for(int i=1; i<=q; i++){
        int x, val; cin>>x>>val;
        vec[x].pb({val, i});
    }
    int tot = 0;
    for(int i=n; i>=1; i--){
        while(!non.empty() && non.back().fi <= a[i]){
            tot -= b[non.back().se];
            A.upd(non.size(), -b[non.back().se]);
            non.pop_back();
        }
        non.pb({a[i], i});
        A.upd(non.size(), b[i]);
        tot += b[i];
        for(auto [val, id] : vec[i]){
            int l=1, r=(int)non.size(), mid=0, cnt=-1;
            while(l<=r){
                mid = md;
                if(tot-A.que(mid-1) >= val) l = mid+1, cnt=mid;
                else r = mid-1; 
            }
            if(cnt==-1) ans[id] = 0;
            else ans[id] = non[cnt-1].se;
        }
    }
    for(int i=1; i<=q; i++) cout << ans[i] << '\n';
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |