Submission #855146

#TimeUsernameProblemLanguageResultExecution timeMemory
855146boris_mihovPassport (JOI23_passport)C++17
48 / 100
622 ms669012 KiB
#include <algorithm> #include <iostream> #include <numeric> #include <cassert> #include <cstring> #include <vector> #include <queue> typedef long long llong; const int MAXN = 2500 + 10; const int INF = 1e9; int n, q; int l[MAXN]; int r[MAXN]; int dist[MAXN * MAXN]; bool vis[MAXN * MAXN]; int encode(int l, int r) { return (l - 1) * n + r; } std::vector <std::pair <int,int>> g[MAXN * MAXN]; std::deque <int> dq; void solve() { for (int i = 1 ; i <= n ; ++i) { int maxR = 0; int minL = n + 1; for (int j = i ; j <= n ; ++j) { maxR = std::max(maxR, r[j]); minL = std::min(minL, l[j]); g[encode(minL, j)].push_back({encode(i, j), 1}); g[encode(i, maxR)].push_back({encode(i, j), 1}); g[encode(l[i], r[i])].push_back({encode(i, j), 1}); if (i != j) g[encode(i + 1, j)].push_back({encode(i, j), 0}); } } std::fill(dist, dist + MAXN * MAXN, INF); dq.push_back(encode(1, n)); dist[encode(1, n)] = 0; while (!dq.empty()) { int top = dq.front(); dq.pop_front(); if (vis[top]) continue; vis[top] = true; for (const auto &[u, d] : g[top]) { if (d == 0 && dist[u] > dist[top]) { dist[u] = dist[top]; dq.push_front(u); } if (d == 1 && dist[u] > dist[top] + d) { dist[u] = dist[top] + d; dq.push_back(u); } } } for (int i = 1 ; i <= n ; ++i) { if (dist[encode(i, i)] == INF) { dist[encode(i, i)] = -1; } } for (int i = 1 ; i <= q ; ++i) { int idx; std::cin >> idx; std::cout << dist[encode(idx, idx)] << '\n'; } } void input() { std::cin >> n; for (int i = 1 ; i <= n ; ++i) { std::cin >> l[i] >> r[i]; } std::cin >> q; } 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...