Submission #885540

#TimeUsernameProblemLanguageResultExecution timeMemory
885540huutuanPassport (JOI23_passport)C++14
6 / 100
762 ms654776 KiB
#include<bits/stdc++.h> using namespace std; #define int long long #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define isz(x) ((int)(x).size()) #define sumof(x) accumulate(all(x), 0ll) const int N=2e5+1; int n, pos[N], dist[3][N*4]; vector<pair<int, int>> g[N*4]; queue<int> q[N*4]; void build(int k, int l, int r){ if (l==r){ pos[l]=k; return; } int mid=(l+r)>>1; build(k<<1, l, mid); build(k<<1|1, mid+1, r); g[k<<1].emplace_back(k, 0); g[k<<1|1].emplace_back(k, 0); } void update(int k, int l, int r, int L, int R, int u){ if (r<L || R<l) return; if (L<=l && r<=R){ g[k].emplace_back(pos[u], 1); return; } int mid=(l+r)>>1; update(k<<1, l, mid, L, R, u); update(k<<1|1, mid+1, r, L, R, u); } void bfs(int t){ for (int i=1; i<=n*4; ++i) if (dist[t][i]<1e9) q[dist[t][i]].push(i); for (int i=0; i<=n*4; ++i){ while (q[i].size()){ int u=q[i].front(); q[i].pop(); if (dist[t][u]!=i) continue; for (auto &e:g[u]){ int v=e.first, w=e.second; if (dist[t][v]>dist[t][u]+w) q[dist[t][v]=dist[t][u]+w].push(v); } } } } void solve(){ cin >> n; build(1, 1, n); for (int i=1; i<=n; ++i){ int l, r; cin >> l >> r; update(1, 1, n, l, r, i); } memset(dist, 0x3f, sizeof dist); dist[0][pos[1]]=0; dist[1][pos[n]]=0; bfs(0); bfs(1); for (int i=1; i<=n*4; ++i) dist[2][i]=dist[0][i]+dist[1][i]; bfs(2); int tc; cin >> tc; while (tc--){ int x; cin >> x; x=pos[x]; cout << (dist[2][x]<1e9?dist[2][x]:-1) << '\n'; } } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int ntests=1; // cin >> ntests; for (int i=1; i<=ntests; ++i) 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...