Submission #928753

#TimeUsernameProblemLanguageResultExecution timeMemory
928753juliany2Passport (JOI23_passport)C++17
100 / 100
448 ms51980 KiB
#include<bits/stdc++.h> using namespace std; using ll = long long; #define all(x) (x).begin(), (x).end() template<class T> struct ST { static constexpr T ID = {{{(int) 1e9, 0}, {0, 0}}}; // or whatever ID inline T comb(T a, T b) { return {min(a[0], b[0]), max(a[1], b[1])}; } // or whatever function int sz; vector<T> t; void init(int _sz, T val = ID) { t.assign((sz = _sz) * 2, ID); } void init(vector<T> &v) { t.resize((sz = v.size()) * 2); for (int i = 0; i < sz; ++i) t[i + sz] = v[i]; for (int i = sz - 1; i; --i) t[i] = comb(t[i * 2], t[(i * 2) | 1]); } void upd(int i, T x) { for (t[i += sz] = x; i > 1; i >>= 1) t[i >> 1] = comb(t[i], t[i ^ 1]); } T query(int l, int r) { T ql = ID, qr = ID; for (l += sz, r += sz + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) ql = comb(ql, t[l++]); if (r & 1) qr = comb(t[--r], qr); } return comb(ql, qr); } }; template<class T> struct ST_SEG { static constexpr T ID = {0, 0}; // or whatever ID inline T comb(T a, T b) { return max(a, b); } // or whatever function int sz; vector<T> t; void init(int _sz, T val = ID) { t.assign((sz = _sz) * 2, ID); } void init(vector<T> &v) { t.resize((sz = v.size()) * 2); for (int i = 0; i < sz; ++i) t[i + sz] = v[i]; for (int i = sz - 1; i; --i) t[i] = comb(t[i * 2], t[(i * 2) | 1]); } void upd(int i, T x) { for (t[i += sz] = x; i > 1; i >>= 1) t[i >> 1] = comb(t[i], t[i ^ 1]); } T query(int l, int r) { T ql = ID, qr = ID; for (l += sz, r += sz + 1; l < r; l >>= 1, r >>= 1) { if (l & 1) ql = comb(ql, t[l++]); if (r & 1) qr = comb(t[--r], qr); } return comb(ql, qr); } }; const int N = 2e5 + 7, L = 20; int n; int l[N], r[N]; int lift[N][L][2]; int costl[N], costr[N], cost[N]; int bmin(int a, int b) { return (l[a] < l[b] ? a : b); } int bmax(int a, int b) { return (r[a] > r[b] ? a : b); } int main() { cin.tie(0)->sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) cin >> l[i] >> r[i]; ST<array<array<int, 2>, 2>> st; st.init(n + 1); for (int i = 1; i <= n; i++) st.upd(i, {{{l[i], i}, {r[i], i}}}); for (int i = 1; i <= n; i++) { lift[i][0][0] = st.query(l[i], r[i])[0][1]; lift[i][0][1] = st.query(l[i], r[i])[1][1]; } for (int j = 1; j < L; j++) { for (int i = 1; i <= n; i++) { lift[i][j][0] = bmin(lift[lift[i][j - 1][0]][j - 1][0], lift[lift[i][j - 1][1]][j - 1][0]); lift[i][j][1] = bmax(lift[lift[i][j - 1][0]][j - 1][1], lift[lift[i][j - 1][1]][j - 1][1]); } } for (int i = 1; i <= n; i++) { int cl = i, cr = i, cost = 0; for (int j = L - 1; ~j; j--) { int nl = bmin(lift[cl][j][0], lift[cr][j][0]), nr = bmax(lift[cl][j][1], lift[cr][j][1]); if (l[nl] != 1) cl = nl, cr = nr, cost += 1 << j; } costl[i] = cost + (l[cl] != 1); } for (int i = 1; i <= n; i++) { int cl = i, cr = i, cost = 0; for (int j = L - 1; ~j; j--) { int nl = bmin(lift[cl][j][0], lift[cr][j][0]), nr = bmax(lift[cl][j][1], lift[cr][j][1]); if (r[nr] != n) cl = nl, cr = nr, cost += 1 << j; } costr[i] = cost + (r[cr] != n); } ST_SEG<array<int, 2>> seg; seg.init(n + 1); vector<int> ord(n + 1), vals(n + 1); iota(all(ord), 0); sort(all(ord), [&](int x, int y) { return l[x] < l[y]; }); priority_queue<array<int, 2>, vector<array<int, 2>>, greater<>> q; for (int i = 1; i <= n; i++) { vals[i] = l[ord[i]]; seg.upd(i, {r[ord[i]], i}); cost[i] = costl[i] + costr[i] + 1; q.push({cost[i], i}); } while (q.size()) { auto [c, i] = q.top(); q.pop(); int idx = upper_bound(all(vals), i) - vals.begin() - 1; while (seg.query(1, idx)[0] >= i) { int j = seg.query(1, idx)[1]; int x = ord[j]; if (cost[i] + 1 < cost[x]) { cost[x] = cost[i] + 1; q.push({cost[x], x}); } seg.upd(j, {0, 0}); } } for (int i = 1; i <= n; i++) if (cost[i] >= (1 << L)) cost[i] = -1; int Q; cin >> Q; while (Q--) { int x; cin >> x; cout << cost[x] << '\n'; } 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...