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;
int main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n,q;
cin >> n >> q;
vector<pair<int , int>> v;
stack<int> s;
int p[n];
for (int i=0; n>i; i++)
{
p[i] = 0;
}
for (int i=0; n>i; i++)
{
int a,b;
cin >> a >> b;
v.push_back({a , b});
}
for (int i=n-1; i>=0; i--)
{
while (!s.empty() && v[i].first > v[s.top()].first)
{
s.pop();
}
if (s.empty())
{
p[i] = -1;
}
else
{
p[i] = s.top();
}
s.push(i);
}
while (q--)
{
int a,b;
cin >> a >> b;
a--;
int i=a;
while(b > 0)
{
b -= v[i].second;
if (b <= 0)
{
cout << i + 1 << endl;
i = -1;
break;
}
i = p[i];
if (i == n - 1)
{
cout << 0 << endl;
break;
}
}
}
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... |