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>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 1e5 + 7;
const int Mod = 1e9 + 7;
const int INF = 1e9 + 7;
const ll BASE = 137;
struct Data {
int x, y, u, v, typ;
};
int n, K, m, Q;
Data a[(int)2e5 + 7];
pii spt[N][21];
struct Segment_Tree {
int m, H;
pii st[N << 2];
void init (int n, int _H) {
m = n;
H = _H;
}
pii mer (pii lf, pii rt) {
pii cur;
cur.fs = min(lf.fs, rt.fs);
cur.se = max(lf.se, rt.se);
return cur;
}
void build (int id, int l, int r) {
if (l == r) {
st[id] = spt[l][H];
return;
}
int mid = l + r >> 1;
build (id << 1, l, mid);
build (id << 1 | 1, mid + 1, r);
st[id] = mer(st[id << 1], st[id << 1 | 1]);
}
pii get(int id, int l, int r, int u, int v) {
if (l > v || r < u) return MP(INF, 0);
if (l >= u && r <= v) return st[id];
int mid = l + r >> 1;
return mer (get (id << 1, l, mid, u, v),
get (id << 1 | 1, mid + 1, r, u, v));
}
void build () {
build (1, 1, m);
}
pii get (int u, int v) {
return get (1, 1, m, u, v);
}
}ST[21];
void solution () {
cin >> n >> K ;
cin >> m;
rep (i, 1, m) {
cin >> a[i].u >> a[i].v;
a[i].typ = a[i].u > a[i].v;
a[i].x = a[i].u;
a[i].y = a[i].u < a[i].v ? min(a[i].u + K - 1, a[i].v) : max(a[i].u - K + 1, a[i].v);
if (a[i].x > a[i].y) swap(a[i].x, a[i].y);
if (a[i].u > a[i].v) swap(a[i].u, a[i].v);
}
sort (a + 1, a + 1 + m, [] (Data A, Data B) {
return A.x < B.x;
});
priority_queue<pii, vector<pii>, greater<pii>> pq[2];
multiset<int> val[2];
int ptr = 1;
rep (i, 1, n) {
while (ptr <= m && a[ptr].x <= i) {
pq[a[ptr].typ].push({a[ptr].y, ptr});
if (a[ptr].typ == 0) val[0].insert(a[ptr].v);
else val[1].insert(a[ptr].u);
++ptr;
}
while (!pq[0].empty() && pq[0].top().fs < i) {
int id = pq[0].top().se;
val[0].erase(val[0].find(a[id].v));
pq[0].pop();
}
while (!pq[1].empty() && pq[1].top().fs < i) {
int id = pq[1].top().se;
val[1].erase(val[1].find(a[id].u));
pq[1].pop();
}
spt[i][0] = MP(i, i);
if (!val[0].empty()) spt[i][0].se = *val[0].rbegin();
if (!val[1].empty()) spt[i][0].fs = *val[1].begin();
}
rep (i, 0, 17) ST[i].init (n, i);
ST[0].build();
rep (j, 1, 17) {
rep (i, 1, n) {
pii cur = spt[i][j - 1];
pii merg = ST[j - 1].get(cur.fs, cur.se);
spt[i][j] = merg;
}
ST[j].build();
}
cin >> Q;
rep (q, 1, Q) {
int S, T;
cin >> S >> T;
pii cur = MP(S, S);
int res = 0;
reb (j, 17, 0) {
pii merg = ST[j].get(cur.fs, cur.se);
if (merg.fs <= T && merg.se >= T) continue;
res += (1 << j);
cur = merg;
}
++res;
cur = ST[0].get(cur.fs, cur.se);
if (cur.fs <= T && cur.se >= T) cout <<res <<"\n";
else cout << -1 <<"\n";
}
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
// file("c");
ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
no bug challenge +2
4
1 2 3 4
2
0 3
2 1
*/
Compilation message (stderr)
Main.cpp: In member function 'void Segment_Tree::build(int, int, int)':
Main.cpp:55:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
55 | int mid = l + r >> 1;
| ~~^~~
Main.cpp: In member function 'std::pair<int, int> Segment_Tree::get(int, int, int, int, int)':
Main.cpp:64:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
64 | int mid = l + r >> 1;
| ~~^~~
# | 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... |