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;
#ifdef MIKU
string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif
// #define int long long
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
typedef pair<int, int> pii;
const int MXN = 100005, LG = 19;
int n, k, m, q;
pii rng[LG][MXN];
pii operator+(pii a, pii b) {
return mp(min(a.fs, b.fs), max(a.sc, b.sc));
}
struct SMG {
pii val[MXN * 2];
void build(pii *rng) {
FOR(i, 0, n) val[i + n] = rng[i];
for (int i = n - 1; i > 0; i--) val[i] = val[i << 1] + val[(i << 1) | 1];
}
pii query(int l, int r) {
pii ans = mp(MXN, -1);
for (l += n, r += n; l != r; l >>= 1, r >>= 1) {
if (l & 1) ans = ans + val[l++];
if (r & 1) ans = ans + val[--r];
}
return ans;
}
} smg[LG];
namespace PPRE {
vector<int> l[MXN], r[MXN];
multiset<int> MS;
void DO() {
int x, y;
FOR(i, 0, m) {
cin >> x >> y;
x--, y--;
if (x < y) {
r[x].push_back(y);
r[min(y + 1, x + k)].push_back(-y - 1);
} else {
l[max(y, x - k + 1)].push_back(y);
l[x + 1].push_back(-y - 1);
}
}
FOR(i, 0, n) {
for (auto &j : l[i]) {
if (j >= 0) MS.insert(j);
else MS.erase(MS.find(-j - 1));
}
rng[0][i].fs = (MS.empty() ? i : *MS.begin());
}
MS.clear();
FOR(i, 0, n) {
for (auto &j : r[i]) {
if (j >= 0) MS.insert(j);
else MS.erase(MS.find(-j - 1));
}
rng[0][i].sc = (MS.empty() ? i : *MS.rbegin());
}
// FOR(i, 0, n) debug(i, rng[0][i].fs, rng[0][i].sc);
}
}
void PRE() {
FOR(w, 1, LG) {
smg[w - 1].build(rng[w - 1]);
FOR(i, 0, n) rng[w][i] = smg[w - 1].query(rng[w - 1][i].fs, rng[w - 1][i].sc + 1);
}
// FOR(i, 0, n) debug(i, rng[17][i].fs, rng[17][i].sc);
}
int calc(int x, int y) {
// debug(x, y);
// debug(rng[LG - 1][x].fs, rng[LG - 1][x].sc);
if (!(rng[LG - 1][x].fs <= y && y <= rng[LG - 1][x].sc)) return -1;
pii now = mp(x, x);
int ans = 0;
for (int w = LG - 2; w >= 0; w--) {
// debug(now.fs, now.sc);
pii nxt = smg[w].query(now.fs, now.sc + 1);
// debug(w, nxt.fs, nxt.sc);
if (!(nxt.fs <= y && y <= nxt.sc)) {
now = nxt;
ans ^= (1 << w);
}
}
return ans + 1;
}
void miku() {
int x, y;
cin >> n >> k >> m;
PPRE::DO();
PRE();
cin >> q;
while (q--) {
cin >> x >> y;
cout << calc(--x, --y) << '\n';
}
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
cin.exceptions(cin.failbit);
miku();
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... |