Submission #864756

#TimeUsernameProblemLanguageResultExecution timeMemory
864756NeroZeinPassport (JOI23_passport)C++17
48 / 100
2093 ms40876 KiB
#include "bits/stdc++.h" using namespace std; #ifdef Nero #include "Deb.h" #else #define deb(...) #endif using arr = pair<int, int>; const int N = 2e5 + 5; int seg[N * 2]; vector<int> vec[N * 2]; void init() { for (int i = 0; i < N * 2; ++i) { seg[i] = N; } } void upd(int nd, int l, int r, int p, int v) { if (l == r) { seg[nd] = v; return; } int mid = (l + r) >> 1; int rs = nd + ((mid - l + 1) << 1); if (p <= mid) { upd(nd + 1, l, mid, p, v); } else { upd(rs, mid + 1, r, p, v); } seg[nd] = min(seg[nd + 1], seg[rs]); } int qry(int nd, int l, int r, int s, int e) { if (l >= s && r <= e) { return seg[nd]; } int mid = (l + r) >> 1; int rs = nd + ((mid - l + 1) << 1); if (mid >= e) { return qry(nd + 1, l, mid, s, e); } else { if (mid < s) { return qry(rs, mid + 1, r, s, e); } else { return min(qry(nd + 1, l, mid, s, e), qry(rs, mid + 1, r, s, e)); } } } void add(int nd, int l, int r, int s, int e, int x) { if (l >= s && r <= e) { vec[nd].push_back(x); return; } int mid = (l + r) >> 1; int rs = nd + ((mid - l + 1) << 1); if (mid >= e) { add(nd + 1, l, mid, s, e, x); } else { if (mid < s) { add(rs, mid + 1, r, s, e, x); } else { add(nd + 1, l, mid, s, e, x); add(rs, mid + 1, r, s, e, x); } } } vector<int> tv; void fill(int nd, int l, int r, int p) { for (int i : vec[nd]) { tv.push_back(i); } vec[nd].clear(); if (l == r) { return; } int mid = (l + r) >> 1; int rs = nd + ((mid - l + 1) << 1); if (p <= mid) { fill(nd + 1, l, mid, p); } else { fill(rs, mid + 1, r, p); } } int l[N], r[N]; int a[N], b[N], ans[N]; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> l[i] >> r[i]; --l[i], --r[i]; } vector<int> id(n); iota(id.begin(), id.end(), 0); sort(id.begin(), id.end(), [&](int i, int j) { return l[i] < l[j]; }); init(); for (int i = 0; i < n; ++i) { int cur = id[i]; if (l[cur] == 0) { a[cur] = 0; } else { a[cur] = 1 + qry(0, 0, n - 1, l[cur], r[cur]); } upd(0, 0, n - 1, cur, a[cur]); } sort(id.begin(), id.end(), [&](int i, int j) { return r[i] > r[j]; }); init(); priority_queue<arr, vector<arr>, greater<arr>> pq; for (int i = 0; i < n; ++i) { int cur = id[i]; if (r[cur] == n - 1) { b[cur] = 0; } else { b[cur] = 1 + qry(0, 0, n - 1, l[cur], r[cur]); } upd(0, 0, n - 1, cur, b[cur]); pq.emplace(a[cur] + b[cur], cur); add(0, 0, n - 1, l[cur], r[cur], cur); ans[cur] = a[cur] + b[cur]; } while (!pq.empty()) { auto [c, v] = pq.top(); pq.pop(); if (c > ans[v]) { continue; } fill(0, 0, n - 1, v); for (int i : tv) { if (ans[v] + 1 < ans[i]) { ans[i] = ans[v] + 1; pq.emplace(ans[i], i); } } } int q; cin >> q; while (q--) { int v; cin >> v; --v; cout << (ans[v] >= N ? -1 : ans[v] + 1) << '\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...