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 Nero
#include "Deb.h"
#else
#define debug(...)
#endif
const int N = 1e5 + 5;
int cost[N];
int l[N], r[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k, m;
cin >> n >> k >> m;
for (int i = 1; i <= n; ++i) {
l[i] = r[i] = i;
}
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
if (u < v) {
r[u] = max(r[u], v);
} else {
l[u] = min(l[u], v);
}
}
int q;
cin >> q;
while (q--) {
int u, v;
cin >> u >> v;
for (int i = 1; i <= n; ++i) {
cost[i] = n + 1;
}
cost[u] = 0;
int cur = 0;
int cl = u, cr = u;//range of reachable nodes
int lastcl = u + 1, lastcr = u - 1;
while (true) {
cur++;
bool change = false;
int mnL = n, mxR = 0;
for (int i = cl; i < lastcl; ++i) {
mxR = max(mxR, r[i]);
mnL = min(mnL, l[i]);
}
lastcl = cl;
for (int i = lastcr + 1; i <= cr; ++i) {
mxR = max(mxR, r[i]);
mnL = min(mnL, l[i]);
}
lastcr = cr;
for (int i = cl - 1; i >= max(1, cl - k + 1); --i) {
mxR = max(mxR, r[i]);
}
for (int i = cr + 1; i <= min(n, cr + k - 1); ++i) {
mnL = min(mnL, l[i]);
}
if (cl > mnL) {
for (int i = mnL; i < cl; ++i) {
cost[i] = cur;
}
cl = mnL;
change = true;
}
if (cr < mxR) {
for (int i = cr + 1; i <= mxR; ++i) {
cost[i] = cur;
}
cr = mxR;
change = true;
}
if (!change) {
break;
}
}
if (cost[v] == n + 1) {
cost[v] = -1;
}
cout << cost[v] << '\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... |