Submission #674499

#TimeUsernameProblemLanguageResultExecution timeMemory
674499stanislavpolynEvent Hopping (BOI22_events)C++17
100 / 100
183 ms17200 KiB
#include <bits/stdc++.h> #define fr(i, a, b) for (int i = (a); i <= (b); i++) #define rf(i, a, b) for (int i = (a); i >= (b); i--) #define fe(x, y) for (auto& x : y) #define fi first #define se second #define pb push_back #define mp make_pair #define mt make_tuple #define pw(x) (1LL << (x)) #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() using namespace std; mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define fbo find_by_order #define ook order_of_key template <typename T> bool umn(T& a, T b) { return a > b ? a = b, 1 : 0; } template <typename T> bool umx(T& a, T b) { return a < b ? a = b, 1 : 0; } using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; template <typename T> using ve = vector<T>; const int N = 1e5 + 5; int n, q; ve<pii> a; int to[N]; int go[N][20]; ve<int> u; pii t[4 * N]; void upd(int v, int tl, int tr, int pos, int val, int idx) { if (tl == tr) { umn(t[v], mp(val, idx)); return; } int tm = (tl + tr) >> 1; if (pos <= tm) upd(v << 1, tl, tm, pos, val, idx); else upd(v << 1 | 1, tm + 1, tr, pos, val, idx); t[v] = min(t[v << 1], t[v << 1 | 1]); } void upd(int pos, int val, int idx) { upd(1, 0, sz(u) - 1, pos, val, idx); } pii get(int v, int tl, int tr, int l, int r) { if (l > r) { return mp(int(1e9), 0); } if (tl == l && tr == r) { return t[v]; } int tm = (tl + tr) >> 1; return min(get(v << 1, tl, tm, l, min(r, tm)), get(v << 1 | 1, tm + 1, tr, max(tm + 1, l), r)); } pii get(int l, int r) { return get(1, 0, sz(u) - 1, l, r); } int main() { #ifndef LOCAL // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios::sync_with_stdio(0); cin.tie(0); #else // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif cin >> n >> q; a.resize(n + 1); fr (i, 1, n) { cin >> a[i].fi >> a[i].se; // to[i] = i; u.pb(a[i].se); } sort(all(u)); u.erase(unique(all(u)), u.end()); fill(t, t + 4 * N, mp(int(1e9), 0)); fr (i, 1, n) { int id = lower_bound(all(u), a[i].se) - u.begin(); upd(id, a[i].fi, i); } fr (i, 1, n) { int L = lower_bound(all(u), a[i].fi) - u.begin(); int R = upper_bound(all(u), a[i].se) - u.begin() - 1; to[i] = get(L, R).se; } // fr (i, 1, n) { // fr (j, 1, n) { // if (i == j) continue; // if (a[j].se >= a[i].fi && a[j].se <= a[i].se) { // if (!to[i] || a[to[i]].fi > a[j].fi) { // to[i] = j; // } // } // } // } fr (i, 1, n) { go[i][0] = to[i]; } fr (p, 1, 19) { fr (i, 1, n) { go[i][p] = go[go[i][p - 1]][p - 1]; } } fr (i, 0, q - 1) { int s, e; cin >> s >> e; if (a[s].se > a[e].se) { cout << "impossible\n"; continue; } if (s == e) { cout << "0\n"; continue; } int v = e; int ans = 0; rf (p, 19, 0) { if (go[v][p] && a[go[v][p]].fi > a[s].se) { ans += pw(p); v = go[v][p]; } } if (to[v] && a[v].fi > a[s].se) { v = to[v]; ans++; } if (a[v].fi <= a[s].se) { ans++; cout << ans << "\n"; } else { cout << "impossible\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...
#Verdict Execution timeMemoryGrader output
Fetching results...