Submission #873003

#TimeUsernameProblemLanguageResultExecution timeMemory
873003boris_mihovPassport (JOI23_passport)C++17
0 / 100
1783 ms1048576 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; int queries; int l[MAXN]; int r[MAXN]; int dist[MAXN]; int distA[MAXN]; int distB[MAXN]; std::vector <int> g[MAXN]; std::vector <int> rev[MAXN]; std::queue <int> q[2 * MAXN]; std::queue <int> bfsQueue; bool vis[MAXN]; void solve() { for (int i = 1 ; i <= n ; ++i) { for (int j = l[i] ; j <= r[i] ; ++j) { // std::cout << "edge: " << i << ' ' << j << '\n'; g[i].push_back(j); rev[j].push_back(i); } } std::fill(distA + 1, distA + 1 + n, INF); std::fill(distB + 1, distB + 1 + n, INF); bfsQueue.push(1); distA[1] = 0; while (!bfsQueue.empty()) { int top = bfsQueue.front(); bfsQueue.pop(); for (const int &u : rev[top]) { if (distA[u] > distA[top] + 1) { distA[u] = distA[top] + 1; bfsQueue.push(u); } } } bfsQueue.push(n); distB[n] = 0; while (!bfsQueue.empty()) { int top = bfsQueue.front(); bfsQueue.pop(); for (const int &u : rev[top]) { if (distB[u] > distB[top] + 1) { distB[u] = distB[top] + 1; bfsQueue.push(u); } } } for (int i = 1 ; i <= n ; ++i) { dist[i] = distA[i] + distB[i]; if (l[i] == 1 && r[i] == n) dist[i] = 1; 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 int &u : rev[top]) { if (dist[u] > dist[top] + 1) { dist[u] = dist[top] + 1; 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...