#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _ <<' '<<
#define print(x) cout<<#x<<": "<<(x)<<'\n'
constexpr int inf=INT_MAX>>2;
int n,q,k;
stack<array<ll, 2>> mq;
vector<vector<ll>> st, par;
vector<ll> c, d;
int main(){
cin.tie(0)->sync_with_stdio(0);
cout.tie(0);
cin>>n>>q;
k=32-__builtin_clz(n);
c.resize(n);
d.resize(n);
par.assign(k, vector<ll>(n+1, n));
st.assign(k, vector<ll>(n+1, 0));
for (int i=0; i<n; i++) {
cin>>d[i]>>c[i];
}
for (int i=n-1; i>=0; --i) {
while (!mq.empty() && mq.top()[0]<=d[i]) mq.pop();
if (mq.empty()) {
par[0][i]=n;
} else {
const auto [dm, j]=mq.top();
par[0][i]=j;
}
st[0][i]=c[i];
mq.push({d[i], i});
}
//print(st[0][0]);
//for (int i=n-1; i>=0; i--) print(dp[i]);
for (int i=1; i<k; i++) for (int j=n-1; j>=0; j--) {
par[i][j]=par[i-1][par[i-1][j]];
st[i][j]=st[i-1][j]+st[i-1][par[i-1][j]];
}
for (int i=0; i<q; i++) {
ll a,b;
cin>>a>>b;
--a;
for (int i=k-1; i>=0; --i) {
//print(i);
//print(a);
//print(b);
//print(st[i][a]);
if (b-st[i][a]>0) {
b-=st[i][a];
a=par[i][a];
}
}
//a=par[0][a];
if (a==n) {
cout<<"0\n";
} else {
cout<<a+1<<'\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... |