#include <bits/stdc++.h>
using i64 = long long;
struct SegmentTree {
int n;
std::vector<int> t;
SegmentTree(int n, int Fill) : n(n), t(2 * n, Fill) {}
void modify(int p, int val) {
for (t[p += n] = val; p > 1; p >>= 1) {
t[p >> 1] = std::min(t[p], t[p ^ 1]);
}
}
int rangeMin(int l, int r) {
int res = n;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l % 2 == 1) {
res = std::min(res, t[l++]);
}
if (r % 2 == 1) {
res = std::min(res, t[--r]);
}
}
return res;
}
};
constexpr int Log = 20;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, q;
std::cin >> n >> q;
std::vector<int> d(n), c(n);
for (int i = 0; i < n; i++) {
std::cin >> d[i] >> c[i];
}
std::vector<int> pr(n + 2);
for (int i = 0; i < n; i++) {
pr[i + 1] = pr[i] + c[i];
}
pr[n + 1] = pr[n];
auto X = d;
std::sort(X.begin(), X.end());
X.erase(std::unique(X.begin(), X.end()), X.end());
for (auto &x : d) {
x = std::lower_bound(X.begin(), X.end(), x) - X.begin();
}
std::vector go(Log, std::vector<int>(n + 1, n));
std::vector need(Log, std::vector<i64>(n + 1, 1e9));
SegmentTree seg(n + 1, n);
for (int i = n - 1; i >= 0; i--) {
go[0][i] = seg.rangeMin(d[i] + 1, n + 1);
need[0][i] = c[i];
for (int j = 1; j < Log; j++) {
go[j][i] = go[j - 1][go[j - 1][i]];
need[j][i] = need[j - 1][i] + need[j - 1][go[j - 1][i]];
}
seg.modify(d[i], i);
}
while (q--) {
int u, v;
std::cin >> u >> v;
u--;
for (int i = Log - 1; i >= 0; i--) {
if (need[i][u] < v) {
v -= need[i][u];
u = go[i][u];
// std::cout << i << " " << u << " " << v << "\n";
}
}
if (u == n) {
std::cout << 0 << "\n";
} else {
std::cout << u + 1 << "\n";
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
294 ms |
25544 KB |
Output is correct |
2 |
Correct |
325 ms |
23892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
472 KB |
Output is correct |
8 |
Correct |
294 ms |
25544 KB |
Output is correct |
9 |
Correct |
325 ms |
23892 KB |
Output is correct |
10 |
Correct |
2 ms |
472 KB |
Output is correct |
11 |
Correct |
152 ms |
15284 KB |
Output is correct |
12 |
Correct |
500 ms |
27428 KB |
Output is correct |
13 |
Correct |
359 ms |
27380 KB |
Output is correct |
14 |
Correct |
308 ms |
27408 KB |
Output is correct |
15 |
Correct |
285 ms |
27384 KB |
Output is correct |