제출 #579649

#제출 시각아이디문제언어결과실행 시간메모리
579649EliasEvent Hopping (BOI22_events)C++17
0 / 100
1588 ms21792 KiB
#include <bits/stdc++.h> #ifndef _DEBUG #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx") #endif using namespace std; #define int int64_t template <class T> istream &operator>>(istream &in, vector<T> &v) { for (T &x : v) in >> x; return in; } struct Range { int s, e, i; bool operator<(Range other) const { return tuple<int, int, int>{e, s, i} < tuple<int, int, int>{other.e, other.s, other.i}; } }; int n, q; vector<Range> ranges, sR; signed main() { cin.tie(0); ios_base::sync_with_stdio(false); cin >> n >> q; ranges = vector<Range>(n); for (int i = 0; i < n; i++) { cin >> ranges[i].s >> ranges[i].e; ranges[i].i = i; } sR = ranges; sort(sR.begin(), sR.end()); vector<vector<int>> next(20, vector<int>(n, -1)); set<Range> reachable; for (int j = n - 2; j >= 0; j--) { auto [s, e, i] = sR[j]; while (reachable.size() && prev(reachable.end())->s > e) reachable.erase(prev(reachable.end())); if (reachable.size()) next[0][j] = prev(reachable.end())->i; reachable.insert({s, e, i}); } for (int k = 1; k < 20; k++) for (int i = 0; i < n; i++) { int a = next[k - 1][i]; if (a != -1) next[k][i] = next[k - 1][a]; } while (q--) { int s, e; cin >> s >> e; s--, e--; if (s == e) { cout << "0\n"; continue; } int count = 0; for (int k = 19; k >= 0; k--) { if (next[k][s] != -1 && ranges[next[k][s]].e < ranges[e].s) { s = next[k][s]; count += pow(2, k); } } while (s != -1 && ranges[s].e < ranges[e].s) { s = next[0][s]; count++; } if (s == -1 || ranges[s].e > ranges[e].e) { cout << "impossible\n"; } else { cout << count + 1 << "\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...