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;
using ll = long long;
using ld = long double;
const int N = 1e6 + 1;
const int mod = 1e9 + 7;
template <typename T>
bool umx(T& a, T b) {
return (a < b ? a = b, 1 : 0);
}
template <typename T>
bool umn(T& a, T b) {
return (a > b ? a = b, 1 : 0);
}
int to[20][N];
ll s[20][N];
void solve () {
int n, q;
cin >> n >> q;
vector<int> d (n + 1), c (n + 1);
for (int i = 1; i <= n; ++i) {
cin >> d[i] >> c[i];
}
vector<int> bigr(n + 1);
vector<int> ve;
vector<ll> pref (n + 1);
for (int i = 1; i <= n; ++i) {
while ((int)ve.size() && d[ve.back()] < d[i]) {
bigr[ve.back()] = i;
ve.pop_back();
}
ve.push_back(i);
}
for (int i = n; i >= 1; --i) {
to[0][i] = bigr[i];
s[0][i] = c[bigr[i]];
for (int l = 1; l < 20; ++l) {
to[l][i] = to[l - 1][to[l - 1][i]];
s[l][i] = s[l - 1][i] + s[l - 1][to[l - 1][i]];
}
}
while (q--) {
int r, v;
cin >> r >> v;
if (v <= c[r]) {
cout << r << "\n";
continue;
}
v -= c[r];
for (int l = 19; l >= 0; --l) {
if (s[l][r] < v) {
v -= s[l][r];
r = to[l][r];
}
}
cout << to[0][r] << "\n";
}
}
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
// freopen ("input.txt", "r", stdin);
// freopen ("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
while (T--) 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... |