제출 #576628

#제출 시각아이디문제언어결과실행 시간메모리
576628InternetPerson10Event Hopping (BOI22_events)C++17
0 / 100
1594 ms29556 KiB
#include <bits/stdc++.h> typedef long long ll; using namespace std; set<int> s; map<int, int> idx; map<pair<int, int>, int> toEvent; vector<pair<int, int>> p; vector<int> mini; struct SegTree { int lx, rx; int val = 1e9, id = -1; SegTree *ls, *rs; SegTree(int l, int r) { lx = l; rx = r; if(rx - lx > 1) { int mid = (lx + rx) / 2; ls = new SegTree(lx, mid); rs = new SegTree(mid, rx); val = min(ls->val, rs->val); if(val == ls->val) id = ls->id; if(val == rs->val) id = rs->id; } else { val = mini[lx]; id = lx; } } pair<int, int> getMin(int l, int r) { if(l <= lx && rx <= r) return {val, id}; if(r <= lx || rx <= l) return {1e9, -1}; return min(ls->getMin(l, r), rs->getMin(l, r)); } }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; int g = 1; while(g <= n) g *= 2; p.resize(n); mini.resize(g, 1e9); for(int i = 0; i < n; i++) { cin >> p[i].first >> p[i].second; s.insert(p[i].first); s.insert(p[i].second); } for(int g : s) { idx[g] = idx.size(); } for(int i = 0; i < n; i++) { p[i].first = idx[p[i].first]; p[i].second = idx[p[i].second]; int x, y; tie(x, y) = p[i]; mini[y] = min(mini[y], x); toEvent[p[i]] = i; } SegTree st(0, g); int a = 5e8, b = 0; while(a--) { b++; } while(q--) { int x, y; cin >> x >> y; x--; y--; int ans = 0; while(p[x] != p[y]) { if(p[y].first <= p[x].second && p[x].second <= p[y].second) { ans++; break; } pair<int, int> pa = st.getMin(p[y].first, p[y].second + 1); if(pa.second == -1 || pa == p[y]) { ans = -1; break; } ans++; y = toEvent[pa]; } if(ans == -1) { cout << "impossible\n"; continue; } else { cout << ans << '\n'; } } }
#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...