Submission #722710

#TimeUsernameProblemLanguageResultExecution timeMemory
722710dxz05Event Hopping (BOI22_events)C++17
100 / 100
612 ms27984 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define all(x) (x).begin(), (x).end() #define MP make_pair const int N = 100500; template <typename T> struct SegmentTree{ int n = 0; vector<T> tree; T neutral_element = make_pair(1e9, -1); // e.g. 0 for sum, INF for min ... SegmentTree(){}; SegmentTree(int _n){ n = _n; tree.assign(n * 4 + 5, neutral_element); } inline T combine(T lf, T rg){ return min(lf, rg); // change it } inline void build(int v, int tl, int tr, const vector<T> &a){ if (tl == tr){ tree[v] = a[tl]; return; } int tm = (tl + tr) >> 1; build(v << 1, tl, tm, a); build(v << 1 | 1, tm + 1, tr, a); tree[v] = combine(tree[v << 1], tree[v << 1 | 1]); } inline void init(const vector<T> &a){ n = (int)a.size(); tree.assign(n * 4 + 5, neutral_element); build(1, 0, n - 1, a); } inline void update(int v, int tl, int tr, int pos, T val){ if (tl == tr){ tree[v] = min(tree[v], val); // pay attention! return; } int tm = (tl + tr) >> 1; if (pos <= tm){ update(v << 1, tl, tm, pos, val); } else { update(v << 1 | 1, tm + 1, tr, pos, val); } tree[v] = combine(tree[v << 1], tree[v << 1 | 1]); } inline void update(int pos, T val){ update(1, 0, n - 1, pos, val); } inline T get(int v, int tl, int tr, int l, int r){ if (l <= tl && tr <= r) return tree[v]; if (tl > r || tr < l) return neutral_element; int tm = (tl + tr) >> 1; return combine(get(v << 1, tl, tm, l, r), get(v << 1 | 1, tm + 1, tr, l, r)); } inline T get(int l, int r){ return get(1, 0, n - 1, l, r); } }; int f[N][18]; int l[N], r[N]; bool can_switch(int i, int j){ return l[j] <= r[i] && r[i] <= r[j]; } void solve(){ int n, q; cin >> n >> q; map<int, int> mp; for (int i = 0; i < n; i++){ cin >> l[i] >> r[i]; mp[l[i]], mp[r[i]]; } int z = 0; for (auto &now : mp) now.second = z++; SegmentTree<pair<int, int>> st(n * 2); for (int i = 0; i < n; i++) { st.update(mp[r[i]], make_pair(mp[l[i]], i)); } for (int i = 0; i < n; i++){ int j = st.get(mp[l[i]], mp[r[i]]).second; f[i][0] = (j != -1 && l[j] < l[i] ? j : -1); } for (int j = 1; j < 18; j++){ for (int i = 0; i < n; i++){ int mid = f[i][j - 1]; f[i][j] = (mid != -1 ? f[mid][j - 1] : -1); } } while (q--){ int x, y; cin >> x >> y; --x, --y; if (x == y){ cout << 0 << endl; continue; } if (can_switch(x, y)){ cout << 1 << endl; continue; } int cnt = 2; for (int i = 17; i >= 0; i--){ int to = f[y][i]; if (to != -1 && l[to] > r[x]){ cnt += 1 << i; y = to; } } y = f[y][0]; if (y != -1 && can_switch(x, y)){ cout << cnt << endl; } else { cout << "impossible" << endl; } } } int main() { ios_base::sync_with_stdio(false); #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif 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...
#Verdict Execution timeMemoryGrader output
Fetching results...