#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define fi first
#define sc second
#define vi vector<int>
#define pii pair<int,int>
#define vpi vector<pair<int,int> >
#define mp make_pair
const ll MOD1 = 1e9+7;
const ll MOD2 = 998244353;
const ll MOD3 = 1e9+93;
const ll MOD4 = 1e9+97;
const ll p1 = 1e9+87;
const ll p2 = 1e9+9;
const int INF = 1e9+7;
const int BASE = 1<<20;
const int LOG = 20;
const int ALF = 27;
const int MAXN = 1e6+7;
const int MAXNN = 1e3+7;
ll d[MAXN],c[MAXN];
int ans[MAXN];
vector<pair<ll,int>> z[MAXN];
ll tree[2*BASE];
vector<pair<ll,int>>v;
void put(int x,int a){
x+=BASE, tree[x]=a, x/=2;
while(x>0) tree[x]=tree[2*x]+tree[2*x+1], x/=2;
}
int query(int x,ll w){
if(x>=BASE) return x-BASE;
else if(tree[2*x+1]>=w) return query(2*x+1,w);
else return query(2*x,w-tree[2*x+1]);
}
void solve(){
int n,q,x,y; ll w=0;
cin>>n>>q;
for(int i=1;i<=n;i++) cin>>d[i]>>c[i];
for(int i=1;i<=q;i++) cin>>x>>y, z[x].pb({y,i});
for(int i=n;i>=1;i--){
while(v.size() and v.back().fi<d[i]) put(v.size(),0), v.pop_back();
v.pb({d[i],i}), put(v.size(),c[i]);
for(auto xd:z[i]){
w=query(1,xd.fi);
if(w==0) ans[xd.sc]=0; else ans[xd.sc]=v[w-1].sc;
}
}
for(int i=1;i<=q;i++) cout<<ans[i]<<'\n';
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tt=1;
while(tt--) solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |