This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,q,lg=17;
cin>>n>>q;
int nxt[n+1][lg];
stack<int> st;
int a[n],b[n],su[n+1][lg]={};
for (int i=0;i<n;i++)
cin>>a[i]>>b[i],su[i][0]=b[i];
nxt[n][0]=n;
for (int i=n-1;i>=0;i--)
{
while (!st.empty() && a[st.top()]<=a[i])
st.pop();
if (st.empty())
nxt[i][0]=n;
else
nxt[i][0]=st.top();
st.push(i);
}
for (int p=1;p<lg;p++)
for (int i=0;i<=n;i++)
nxt[i][p]=nxt[nxt[i][p-1]][p-1],su[i][p]=su[i][p-1]+su[nxt[i][p-1]][p-1];
while (q--)
{
int id,x;
cin>>id>>x;
id--;
for (int p=lg-1;p>=0 && id<n;p--)
if (su[id][p]<x)
x-=su[id][p],id=nxt[id][p];
cout<<(id+1)%(n+1)<<endl;
}
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... |