#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0;i<int(n);i++)
#define forsn(i,s,n) for(int i=int(s);i<int(n);i++)
#define dforn(i,n) for(int i=int(n)-1;i>=0;i--)
#define dforsn(i,s,n) for(int i=int(n)-1;i>=int(s);i--)
#define fst first
#define snd second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef pair<int,int> ii;
const int K = 17;
const int SZ = 1 << K;
const int INF = 1e9;
int maxi[2 * SZ], mini[2 * SZ];
template<typename T>
void chmin(T &x, T v) {
if (x > v) x = v;
}
template<typename T>
void chmax(T &x, T v) {
if (x < v) x = v;
}
void update(int l, int r, int v) {
for (l += SZ, r += SZ; l < r; l /= 2, r /= 2) {
if (l & 1) {
chmax(maxi[l], v);
chmin(mini[l], v);
l++;
}
if (r & 1) {
--r;
chmax(maxi[r], v);
chmin(mini[r], v);
}
}
}
int queryMax(int i) {
int res = maxi[i += SZ];
while (i /= 2) chmax(res, maxi[i]);
return res;
}
int queryMin(int i) {
int res = mini[i += SZ];
while (i /= 2) chmin(res, mini[i]);
return res;
}
int stMin[K][2 * SZ], stMax[K][2 * SZ];
int askMin(int k, int l, int r) {
int res = INF;
for (l += SZ, r += SZ; l < r; l /= 2, r /= 2) {
if (l & 1) chmin(res, stMin[k][l++]);
if (r & 1) chmin(res, stMin[k][--r]);
}
return res;
}
int askMax(int k, int l, int r) {
int res = -INF;
for (l += SZ, r += SZ; l < r; l /= 2, r /= 2) {
if (l & 1) chmax(res, stMax[k][l++]);
if (r & 1) chmax(res, stMax[k][--r]);
}
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
{
int m, k;
cin >> n >> k >> m;
forn(i, 2 * SZ) mini[i] = INF, maxi[i] = -INF;
forn(i, n) update(i, i + 1, i);
forn(i, m) {
int a, b;
cin >> a >> b;
--a, --b;
if (a < b) {
update(a, min(a + k, b), b);
} else {
update(max(a - k + 1, b + 1), a + 1, b);
}
}
}
forn(k, K) forn(i, 2 * SZ) stMin[k][i] = INF, stMax[k][i] = -INF;
forn(i, n) stMin[0][i + SZ] = queryMin(i), stMax[0][i + SZ] = queryMax(i);
forn(k, K) {
dforsn(i, 1, SZ) {
stMin[k][i] = min(stMin[k][2 * i], stMin[k][2 * i + 1]);
stMax[k][i] = max(stMax[k][2 * i], stMax[k][2 * i + 1]);
}
if (k < K - 1) forn(i, n) {
int l = stMin[k][i + SZ], r = stMax[k][i + SZ];
stMin[k + 1][i + SZ] = askMin(k, l, r + 1);
stMax[k + 1][i + SZ] = askMax(k, l, r + 1);
}
}
int q;
cin >> q;
forn(_, q) {
int s, t;
cin >> s >> t;
--s, --t;
int l = s, r = s;
int dist = 1;
dforn(k, K) {
int nextL = askMin(k, l, r + 1);
int nextR = askMax(k, l, r + 1);
if (nextL > t || nextR < t) {
l = nextL, r = nextR;
dist += 1 << k;
}
}
int nextL = askMin(0, l, r + 1);
int nextR = askMax(0, l, r + 1);
l = nextL, r = nextR;
if (l <= t && t <= r) cout << dist << '\n';
else cout << "-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... |