이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 1e5 + 10;
pair <int, int> seg[20][N * 4];
pair <int, int> laz[N * 4];
int n, k;
pair <int, int> merge(pair <int, int> A, pair <int, int> B) {
return {min(A.f, B.f), max(A.s, B.s)};
}
void push(int id, int l, int r, int u, int v, int k) {
if (l > v || r < u)
return;
if (l >= u && r <= v) {
laz[id] = merge(laz[id], {k, k});
return;
}
int m = (l + r) / 2;
push(id * 2, l, m, u, v, k);
push(id * 2 + 1, m + 1, r, u, v, k);
}
void run(int id, int l, int r, pair <int, int> st) {
st = merge(st, laz[id]);
if (l == r) {
seg[0][id] = merge({l, l}, st);
return;
}
int m = (l + r) / 2;
run(id * 2, l, m, st);
run(id * 2 + 1, m + 1, r, st);
seg[0][id] = merge(seg[0][id * 2], seg[0][id * 2 + 1]);
}
pair <int, int> range(int dep, int id, int l, int r, int u, int v) {
if (l > v || r < u)
return {n + 1, 0};
if (l >= u && r <= v) {
return seg[dep][id];
}
int m = (l + r) / 2;
return merge(range(dep, id * 2, l, m, u, v), range(dep, id * 2 + 1, m + 1, r, u, v));
}
void build(int dep, int id, int l, int r) {
if (l == r) {
pair <int, int> st = seg[dep - 1][id];
st = range(dep - 1, 1, 1, n, st.f, st.s);
seg[dep][id] = st;
return;
}
int m = (l + r) / 2;
build(dep, id * 2, l, m);
build(dep, id * 2 + 1, m + 1, r);
seg[dep][id] = merge(seg[dep][id * 2], seg[dep][id * 2 + 1]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n * 4; i++) {
laz[i].f = n + 1;
}
int m;
cin >> m;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
if (x <= y) {
push(1, 1, n, x, min(x + k - 1, y - 1), y);
} else {
push(1, 1, n, max(y + 1, x - k + 1), x, y);
}
}
run(1, 1, n, {n + 1, 0});
for (int i = 1; i < 20; i++)
build(i, 1, 1, n);
int q;
cin >> q;
while (q--) {
int s, t;
cin >> s >> t;
int ans = 0;
pair <int, int> inv = {s, s};
for (int j = 19; j >= 0; j--) {
pair <int, int> sel = range(j, 1, 1, n, inv.f, inv.s);
if (sel.f <= t && sel.s >= t)
continue;
ans |= (1 << j);
inv = sel;
}
if (ans > m) {
ans = -2;
}
cout << ans + 1 << '\n';
}
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |