Submission #1029603

#TimeUsernameProblemLanguageResultExecution timeMemory
1029603NeroZeinRailway Trip 2 (JOI22_ho_t4)C++17
27 / 100
2072 ms3916 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...