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>
#define ll long long
using namespace std;
const int MAX = 1e5 + 5, LOG = 20, INF = 2e9;
int up[MAX][LOG], sum[MAX][LOG], D[MAX];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, q;
cin>>n>>q;
for (int i = 1; i <= n; i++) cin>>D[i]>>sum[i][0];
D[n + 1] = INF;
sum[n + 1][0] = INF;
up[n + 1][0] = n + 1;
stack<int> s;
s.push(n + 1);
for (int i = n; i >= 1; i--)
{
while(D[s.top()] <= D[i]) s.pop();
up[i][0] = s.top();
s.push(i);
}
for (int i = 1; i < LOG; i++)
{
for (int j = 1; j <= n; j++)
{
up[j][i] = up[up[j][i - 1]][i - 1];
sum[j][i] = sum[up[j][i - 1]][i - 1] + sum[j][i - 1];
}
}
for (int i = 0; i < q; i++)
{
int r, v;
cin>>r>>v;
for (int j = LOG - 1; j >= 0; j--)
{
if(v > sum[r][j]){
v -= sum[r][j];
r = up[r][j];
}
}
if(r == n + 1) cout<<0<<'\n';
else cout<<r<<'\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... |