이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |