Submission #1015226

#TimeUsernameProblemLanguageResultExecution timeMemory
1015226aufanPassport (JOI23_passport)C++17
100 / 100
290 ms73084 KiB
#include <bits/stdc++.h> #define int long long #define fi first #define se second using namespace std; const int INFF = 1e18; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> l(n), r(n); for (int i = 0; i < n; i++) cin >> l[i] >> r[i]; // build somewhat iterative segtree-like segments (?) int sz = 1; while (sz < n) { sz *= 2; } vector<vector<int>> v(2 * sz); for (int i = 0; i < n; i++) { int cl = l[i] + sz - 1; int cr = r[i] + sz; while (cl < cr) { if (cl & 1) { v[cl++].push_back(i); } if (cr & 1) { v[--cr].push_back(i); } cl >>= 1; cr >>= 1; } } auto bfs = [&](int x) { vector<int> dist(n, INFF), q(1, x), vis(n), used(2 * sz); dist[x] = 0; for (int i = 0; i < (int)q.size(); i++) { int x = q[i]; if (!vis[x]) { vis[x] = 1; int id = x + sz; while (id >= 1 && !used[id]) { used[id] = 1; for (auto z : v[id]) { if (dist[z] == INFF) { dist[z] = dist[x] + 1; q.push_back(z); } } id >>= 1; } } } return dist; }; vector<int> df = bfs(0), db = bfs(n - 1), dc(n, INFF); priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; df[0] = db[n - 1] = 1; for (int i = 0; i < n; i++) { dc[i] = min(dc[i], df[i] + db[i]); pq.push({dc[i], i}); } vector<int> vis(n), used(2 * sz); while (!pq.empty()) { auto [w, x] = pq.top(); pq.pop(); if (!vis[x]) { vis[x] = 1; int id = x + sz; while (id >= 1 && !used[id]) { used[id] = 1; for (auto z : v[id]) { if (dc[x] + 1 < dc[z]) { dc[z] = dc[x] + 1; pq.push({dc[z], z}); } } id >>= 1; } } } vector<int> ans(n); for (int i = 0; i < n; i++) { ans[i] = (dc[i] == INFF ? -1 : dc[i] - 1); } int q; cin >> q; for (int i = 0; i < q; i++) { int x; cin >> x; --x; cout << ans[x] << '\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...