Submission #786892

#TimeUsernameProblemLanguageResultExecution timeMemory
786892penguin133Passport (JOI23_passport)C++17
100 / 100
812 ms142680 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define pi pair<int, int> #define pii pair<int, pi> #define fi first #define se second #ifdef _WIN32 #define getchar_unlocked _getchar_nolock #endif mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); int n, q, L[200005], R[200005], cl[600005], cr[600005]; struct node{ int s, e, m, val; node *l, *r; node(int _s, int _e){ s = _s, e = _e, m = (s + e) >> 1; val = 0; if(s != e)l = new node(s, m), r = new node(m+1, e); } void upd(int p, int v){ if(s == e){ val = v; return; } if(p <= m)l->upd(p, v); else r->upd(p, v); val = min(l->val, r->val); } int qry(int a, int b){ if(s == a && b == e)return val; else if(b <= m)return l->qry(a, b); else if(a > m)return r->qry(a, b); else return min(l->qry(a, m), r->qry(m+1, b)); } }*root; vector <pi> adj[600005]; int dist[600005]; int cnt; struct node2{ int s, e, m, nd; node2 *l, *r; node2(int _s, int _e){ s = _s, e = _e, m = (s + e) >> 1; if(s != e){ nd = ++cnt; l = new node2(s, m); r = new node2(m+1, e); adj[l->nd].push_back({nd, 0}); adj[r->nd].push_back({nd, 0}); } else nd = s; } void upd(int a, int b, int u){ if(s == a && e == b){ adj[nd].push_back({u, 1}); return; } if(b <= m)l->upd(a, b, u); else if(a > m)r->upd(a, b, u); else l->upd(a, m, u), r->upd(m+1, b, u); } }*root2; void solve(){ cin >> n; for(int i=1;i<=n;i++)cin >> L[i] >> R[i]; /* root = new node(1, n); cl[1] = cr[n] = 1; for(int i=2;i<=n;i++){ if(L[i] == i){ cl[i] = 1e18; } else cl[i] = root->qry(L[i], i - 1) + 1; root->upd(i, cl[i]); } root = new node(1, n); for(int i=n-1;i>=1;i--){ if(R[i] == i){ cr[i] = 1e18; } else cr[i] = root->qry(i + 1, R[i]) + 1; root->upd(i, cr[i]); } */ cnt = n; root2 = new node2(1, n); for(int i=1;i<=n;i++)root2->upd(L[i], R[i], i); priority_queue <pi, vector <pi>, greater <pi> > pq; for(int i=1;i<=cnt;i++)cl[i] = 1e18; cl[1] = 0; pq.push({0, 1}); while(!pq.empty()){ int x = pq.top().fi, y = pq.top().se; pq.pop(); if(cl[y] < x)continue; for(auto [i, j] : adj[y]){ if(cl[i] > x + j)cl[i] = x + j, pq.push({cl[i], i}); } } for(int i=1;i<=cnt;i++)cr[i] = 1e18; cr[n] = 0; pq.push({0, n}); while(!pq.empty()){ int x = pq.top().fi, y = pq.top().se; pq.pop(); if(cr[y] < x)continue; for(auto [i, j] : adj[y]){ if(cr[i] > x + j)cr[i] = x + j, pq.push({cr[i], i}); } } for(int i=1;i<=n;i++){ if(i == 1)dist[i] = cr[i]; else if(i == n)dist[i] = cl[i]; else dist[i] = cl[i] + cr[i] - 1; //cout << cl[i] << ' ' << cr[i] << ' ' << dist[i] << '\n'; //if(L[i] == 1 && R[i] == n)dist[i] = 1; //cout << dist[i] << ' '; pq.push({dist[i], i}); } for(int i=n+1;i<=cnt;i++)dist[i] = 1e18; while(!pq.empty()){ int x = pq.top().fi, y = pq.top().se; pq.pop(); if(dist[y] < x)continue; for(auto [i, j] : adj[y]){ if(dist[i] > x + j)dist[i] = x + j, pq.push({dist[i], i}); } } //for(int i=1;i<=cnt;i++)cout << dist[i] << ' '; //cout << '\n'; cin >> q; while(q--){ int x; cin >> x; if(dist[x] > 1e9)cout << -1 << '\n'; else cout << dist[x] << '\n'; } } main(){ ios::sync_with_stdio(0);cin.tie(0); int tc = 1; //cin >> tc; for(int tc1=1;tc1<=tc;tc1++){ // cout << "Case #" << tc1 << ": "; solve(); } }

Compilation message (stderr)

passport.cpp:150:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  150 | main(){
      | ^~~~
#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...