Submission #873106

#TimeUsernameProblemLanguageResultExecution timeMemory
873106boris_mihovPassport (JOI23_passport)C++17
0 / 100
356 ms569980 KiB
#include <algorithm> #include <iostream> #include <numeric> #include <cassert> #include <vector> #include <queue> typedef long long llong; const int MAXN = 200000 + 10; const int INF = 1e9; int n; std::vector <std::pair <int,int>> g[MAXN]; struct SegmentTree { int cnt; int tree[4*MAXN]; void build(int l, int r, int node) { if (l == r) { tree[node] = l; return; } tree[node] = ++cnt; int mid = (l + r) / 2; build(l, mid, 2*node); build(mid + 1, r, 2*node + 1); g[tree[2 * node]].push_back({tree[node], 0}); g[tree[2 * node + 1]].push_back({tree[node], 0}); } void addEdges(int l, int r, int node, int queryL, int queryR, int queryNode) { if (queryL <= l && r <= queryR) { g[tree[node]].push_back({queryNode, 1}); return; } int mid = (l + r) / 2; if (queryL <= mid) addEdges(l, mid, 2*node, queryL, queryR, queryNode); if (mid + 1 <= queryR) addEdges(mid + 1, r, 2*node + 1, queryL, queryR, queryNode); } void build() { cnt = n; build(1, n, 1); } void addEdges(int to, int l, int r) { addEdges(1, n, 1, l, r, to); } }; int queries; int l[MAXN]; int r[MAXN]; int dist[2 * MAXN]; int distA[2 * MAXN]; int distB[2 * MAXN]; std::queue <int> q[2 * MAXN]; std::deque <int> dq; SegmentTree tree; bool vis[MAXN]; void solve() { tree.build(); for (int i = 1 ; i <= n ; ++i) { tree.addEdges(i, l[i], r[i]); } std::fill(distA + 1, distA + 1 + tree.cnt, INF); std::fill(distB + 1, distB + 1 + tree.cnt, INF); dq.push_back(1); distA[1] = 0; while (!dq.empty()) { int top = dq.front(); dq.pop_front(); for (const auto &[u, d] : g[top]) { if (distA[u] > distA[top] + d) { distA[u] = distA[top] + d; if (d == 0) dq.push_front(u); else dq.push_back(u); } } } dq.push_back(n); distB[n] = 0; while (!dq.empty()) { int top = dq.front(); dq.pop_front(); for (const auto &[u, d] : g[top]) { if (distB[u] > distB[top] + d) { distB[u] = distB[top] + d; if (d == 0) dq.push_front(u); else dq.push_back(u); } } } for (int i = 1 ; i <= n ; ++i) { dist[i] = distA[i] + distB[i] - (distA[i] > 0 && distB[i] > 0); if (dist[i] <= 2 * n) q[dist[i]].push(i); } for (int d = 0 ; d <= n ; ++d) { while (!q[d].empty()) { int top = q[d].front(); q[d].pop(); if (vis[top]) { continue; } vis[top] = true; for (const auto &[u, d] : g[top]) { if (dist[u] > dist[top] + d) { dist[u] = dist[top] + d; q[dist[u]].push(u); } } } } for (int i = 1 ; i <= queries ; ++i) { int node; std::cin >> node; if (dist[node] <= n) std::cout << dist[node] << '\n'; else std::cout << -1 << '\n'; } } void input() { std::cin >> n; for (int i = 1 ; i <= n ; ++i) { std::cin >> l[i] >> r[i]; } std::cin >> queries; } void fastIOI() { std::ios_base :: sync_with_stdio(0); std::cout.tie(nullptr); std::cin.tie(nullptr); } int main() { fastIOI(); input(); solve(); 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...