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 IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
string _reset = "\u001b[0m", _yellow = "\u001b[33m", _bold = "\u001b[1m";
void DBG() { cerr << "]" << _reset << endl; }
template<class H, class...T> void DBG(H h, T ...t) {
cerr << to_string(h);
if(sizeof ...(t)) cerr << ", ";
DBG(t...);
}
#ifdef CHEISSMART
#define debug(...) cerr << _yellow << _bold << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define debug(...)
#endif
const int INF = 1e9 + 7, N = 1e5 + 7;
vi rig[N], lef[N];
vi ls[20], rs[20];
int n;
struct segtree {
pi t[N * 4]; // min, max
vi a;
void init(vi _a) {
a = _a;
assert(SZ(a) == n);
build();
}
void pull(int v) {
t[v].F = min(t[v * 2].F, t[v * 2 + 1].F);
t[v].S = max(t[v * 2].S, t[v * 2 + 1].S);
}
void build(int v = 1, int tl = 0, int tr = n) {
if(tr - tl == 1) {
t[v] = {a[tl], a[tl]};
return;
}
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm, tr);
pull(v);
}
int qry_min(int l, int r, int v = 1, int tl = 0, int tr = n) {
if(l <= tl && tr <= r)
return t[v].F;
int tm = (tl + tr) / 2, ans = INF;
if(l < tm) ans = min(ans, qry_min(l, r, v * 2, tl, tm));
if(r > tm) ans = min(ans, qry_min(l, r, v * 2 + 1, tm, tr));
return ans;
}
int qry_max(int l, int r, int v = 1, int tl = 0, int tr = n) {
if(l <= tl && tr <= r)
return t[v].S;
int tm = (tl + tr) / 2, ans = -INF;
if(l < tm) ans = max(ans, qry_max(l, r, v * 2, tl, tm));
if(r > tm) ans = max(ans, qry_max(l, r, v * 2 + 1, tm, tr));
return ans;
}
} lt[20], rt[20];
signed main()
{
IO_OP;
int k, m;
cin >> n >> k >> m;
for(int i = 0; i < m; i++) {
int a, b;
cin >> a >> b, a--, b--;
if(a < b) {
rig[a].PB(b);
} else {
lef[a].PB(b);
}
}
vi l(n), r(n);
{
multiset<int> s;
for(int i = 0; i < n; i++) {
for(int j:rig[i])
s.insert(j);
if(i - k >= 0) for(int j:rig[i - k])
s.erase(s.find(j));
r[i] = i;
if(s.size()) r[i] = max(r[i], *s.rbegin());
}
}
{
multiset<int> s;
for(int i = n - 1; i >= 0; i--) {
for(int j:lef[i])
s.insert(j);
if(i + k < n) for(int j:lef[i + k])
s.erase(s.find(j));
l[i] = i;
if(s.size()) l[i] = min(l[i], *s.begin());
}
}
auto go = [&] (int k) {
ls[k] = l, rs[k] = r;
lt[k].init(l), rt[k].init(r);
};
go(0);
for(int k = 1; k < 20; k++) {
l.clear(), r.clear();
for(int i = 0; i < n; i++) {
l.PB(lt[k - 1].qry_min(ls[k - 1][i], rs[k - 1][i] + 1));
r.PB(rt[k - 1].qry_max(ls[k - 1][i], rs[k - 1][i] + 1));
}
go(k);
}
int q;
cin >> q;
while(q--) {
int s, t;
cin >> s >> t, s--, t--;
assert(s != t);
int l = s, r = s, ans = 0;
for(int i = 19; i >= 0; i--) {
int new_l = lt[i].qry_min(l, r + 1);
int new_r = rt[i].qry_max(l, r + 1);
if(new_l <= t && t <= new_r) continue;
ans += 1 << i;
l = new_l, r = new_r;
}
int new_l = lt[0].qry_min(l, r + 1);
int new_r = rt[0].qry_max(l, r + 1);
l = new_l, r = new_r;
ans++;
if(l <= t && t <= r) {
cout << ans << '\n';
} else {
cout << -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... |