#include <iostream>
#include <algorithm>
#include <stack>
#include <cstdint>
#define int long long
using namespace std;
int n, q;
struct reservoir
{
int d, c;
} v[100005];
int dr[100005], stiva[100005], cnt;
pair <int, int> table[100005][20];
void solve ()
{
int ptr, val;
cin >> ptr >> val;
for (int bit=17;bit>=0;bit--)
{
if (table[ptr][bit].second<val)
{
val-=table[ptr][bit].second;
ptr=table[ptr][bit].first;
}
}
cout << (ptr==n+1?0:ptr) << "\n";
}
int32_t main ()
{
ios :: sync_with_stdio (0);
cin.tie (nullptr);
cin >> n >> q;
for (int i=1;i<=n;i++)
{
cin >> v[i].d >> v[i].c;
}
v[n+1].d=1e9+1;
v[n+1].c=0;
stiva[++cnt]=n+1;
for (int i=n;i>=1;i--)
{
while (cnt && v[stiva[cnt]].d<=v[i].d)
{
cnt--;
}
dr[i]=stiva[cnt];
stiva[++cnt]=i;
}
dr[n+1]=n+1;
for (int i=n+1;i>=1;i--)
{
table[i][0].first=dr[i];
table[i][0].second=v[i].c;
for (int p=1;p<=17;p++)
{
table[i][p].first=table[table[i][p-1].first][p-1].first;
table[i][p].second=table[i][p-1].second+table[table[i][p-1].first][p-1].second;
}
}
while (q--)
{
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... |