Submission #795759

#TimeUsernameProblemLanguageResultExecution timeMemory
795759hugo_pmPassport (JOI23_passport)C++17
100 / 100
963 ms87616 KiB
#include <bits/stdc++.h> #define int long long using namespace std; #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define rep(i, a, b) for(int i = (a); i < (b); i++) #define sz(v) ((int)((v).size())) template<typename T> void chmax(T &x, const T &v) { if (x < v) x = v; } template<typename T> void chmin(T &x, const T &v) { if (x > v) x = v; } using pii = pair<int, int>; using vi = vector<int>; string to_string(string s) { return s; } template <typename T> string to_string(T v) { bool first = true; string res = "["; for (const auto &x : v) { if (!first) res += ", "; first = false; res += to_string(x); } res += "]"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } void dbg_out() { cout << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << to_string(H); dbg_out(T...); } #ifdef DEBUG #define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif const int MAX_N = 1 << 18; const int INF = 1e9; pii itvs[MAX_N]; vector<int> tree[2*MAX_N]; // inclus void addItv(int node, int leftNode, int rightNode, int idItv) { const auto [leftAdd, rightAdd] = itvs[idItv]; if (rightNode < leftAdd || rightAdd < leftNode) { return; } if (leftAdd <= leftNode && rightNode <= rightAdd) { // dbg(idItv, "added"s, node); tree[node].push_back(idItv); return; } if (leftNode < rightNode) { int mid = (leftNode + rightNode) / 2; addItv(2*node, leftNode, mid, idItv); addItv(2*node+1, mid+1, rightNode, idItv); } } void popItv(int node, int leftNode, int rightNode, int leaf, vector<int> &acc) { // dbg(node, leftNode, rightNode, tree[node]); for (int x : tree[node]) { acc.push_back(x); } tree[node].clear(), tree[node].shrink_to_fit(); if (leftNode < rightNode) { int mid = (leftNode + rightNode) / 2; if (leaf <= mid) { popItv(2*node, leftNode, mid, leaf, acc); } else { popItv(2*node+1, mid+1, rightNode, leaf, acc); } } } int N, Q; vector<int> calcDist(int source) { rep(node, 0, N) { addItv(1, 0, N-1, node); } vector<int> dist(N, INF); dist[source] = 0; queue<int> bfs; bfs.push(source); while (!bfs.empty()) { int cur = bfs.front(); bfs.pop(); vector<int> todo; popItv(1, 0, N-1, cur, todo); for (int voisin : todo) if (dist[voisin] == INF) { dist[voisin] = dist[cur]+1; bfs.push(voisin); } } return dist; } vector<int> d0, dN; signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N; rep(i, 0, N) { cin >> itvs[i].first >> itvs[i].second; --itvs[i].first, --itvs[i].second; } d0 = calcDist(0); dbg(d0); dN = calcDist(N-1); dbg(dN); d0[0]++, dN[N-1]++; priority_queue<pii, vector<pii>, greater<pii>> pq; vector<int> dist(N, INF); rep(node, 0, N) { addItv(1, 0, N-1, node); dist[node] = d0[node] + dN[node] - 1; if (dist[node] < INF) pq.push({dist[node], node}); } while (!pq.empty()) { auto [cd, node] = pq.top(); pq.pop(); if (cd != dist[node]) continue; vector<int> todo; popItv(1, 0, N-1, node, todo); for (int voisin : todo) if (dist[voisin] > dist[node]+1) { dist[voisin] = dist[node]+1; pq.push({dist[voisin], voisin}); } } for (int &x : dist) if (x >= INF) { x = -1; } dbg(dist); cin >> Q; rep(iReq, 0, Q) { int node; cin >> node; --node; cout << dist[node] << '\n'; } }
#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...