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;
const int Z = 1e5;
const int B = 17;
int N, K, M;
struct SegmentTree {
int a[2 * Z] = {};
int minQuery = 0;
void update(int i, int value) {
i += N;
if (minQuery)
a[i] = max(a[i], Z - value);
else
a[i] = max(a[i], value);
for (i /= 2; i >= 1; i /= 2) {
a[i] = max(a[2 * i], a[2 * i + 1]);
}
}
int query(int l, int r) {
int result = 0;
l += N;
r += N + 1;
while (l < r) {
if (l & 1) result = max(result, a[l++]);
if (r & 1) result = max(result, a[--r]);
l /= 2;
r /= 2;
}
return minQuery ? Z - result : result;
}
int operator[](int i) {
return minQuery ? Z - a[i + N] : a[i + N];
}
} segmentTree[2][B];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> K >> M;
priority_queue<array<int, 2>> pq[2];
vector<array<int, 2>> updates[2][N];
while (M--) {
int l, r;
cin >> l >> r;
--l, --r;
if (l < r)
updates[1][l].push_back({r, min(l + K - 1, r)});
else
updates[0][max(l - K + 1, r)].push_back({-r, l});
}
for (int i = 0; i < B; ++i) {
segmentTree[0][i].minQuery = 1;
}
for (int i = 0; i < N; ++i) {
for (int j : {0, 1}) {
for (auto k : updates[j][i])
pq[j].push(k);
while (!pq[j].empty() && pq[j].top()[1] < i)
pq[j].pop();
int topVal = pq[j].empty() ? i : (j ? 1 : -1) * pq[j].top()[0];
segmentTree[j][0].update(i, topVal);
}
}
for (int k = 0; k + 1 < B; ++k) {
for (int j : {0, 1}) {
for (int i = 0; i < N; ++i) {
int left = segmentTree[0][k][i];
int right = segmentTree[1][k][i];
segmentTree[j][k + 1].update(i, segmentTree[j][k].query(left, right));
}
}
}
cin >> M;
while (M--) {
int l, t;
cin >> l >> t;
--l;
--t;
int r = l;
int steps = 0;
for (int i = B - 1; i >= 0; --i) {
int left = segmentTree[0][i].query(l, r);
int right = segmentTree[1][i].query(l, r);
if (left > t || t > right) {
l = left;
r = right;
steps |= 1 << i;
}
}
cout << (steps + 1 > N ? -1 : steps + 1) << '\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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |