#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> d(n), c(n);
for (int i = 0; i < n; ++i) {
cin >> d[i] >> c[i];
}
vector<int> res(q);
vector<array<int, 3>> Queries;
for (int i = 0; i < q; ++i) {
int u, v; cin >> u >> v; --u;
Queries.push_back({u, v, i});
}
sort(Queries.rbegin(), Queries.rend());
vector<array<int, 2>> st;
auto solve = [&](int x) {
int l = 1, r = st.size() - 1, p = 0;
while (l <= r) {
int md = (l + r) / 2;
if (st.back()[1] - st[md - 1][1] >= x) {
p = st[md][0] + 1;
l = md + 1;
} else {
r = md - 1;
}
}
return p;
};
st.push_back({n, 0});
for (int i = n - 1, j = 0; i >= 0; --i) {
while (st.size() > 1 && d[st.back()[0]] <= d[i]) {
st.pop_back();
}
st.push_back({i, st.back()[1] + c[i]});
while (j < q && Queries[j][0] == i) {
res[Queries[j][2]] = solve(Queries[j][1]);
++j;
}
}
for (int i = 0; i < q; ++i) {
cout << res[i] << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
8496 KB |
Output is correct |
2 |
Correct |
70 ms |
8900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
64 ms |
8496 KB |
Output is correct |
9 |
Correct |
70 ms |
8900 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
41 ms |
4660 KB |
Output is correct |
12 |
Correct |
96 ms |
11348 KB |
Output is correct |
13 |
Correct |
82 ms |
9412 KB |
Output is correct |
14 |
Correct |
70 ms |
8384 KB |
Output is correct |
15 |
Correct |
66 ms |
8748 KB |
Output is correct |