Submission #941158

#TimeUsernameProblemLanguageResultExecution timeMemory
941158peterandvoiRailway Trip 2 (JOI22_ho_t4)C++17
100 / 100
613 ms71176 KiB
#include <bits/stdc++.h> using namespace std; #ifdef ngu #include "debug.h" #else #define debug(...) 42 #endif const int N = (int) 1e5 + 5; const int LOG = 18; int n, k, m, q; pair<int, int> st[LOG][N]; int a[N << 1], b[N << 1]; vector<int> in[N], out[N]; struct segment_tree { int n; vector<pair<int, int>> tree; segment_tree() {}; segment_tree(int n): n(n) { tree.resize(4 << __lg(n)); } pair<int, int> cmb(pair<int, int> a, pair<int, int> b) { return {min(a.first, b.first), max(a.second, b.second)}; } void upd(int i, pair<int, int> v) { int id = 1, l = 1, r = n; while (l < r) { int mid = l + r >> 1; id <<= 1; if (i <= mid) { r = mid; } else { l = mid + 1; id |= 1; } } tree[id] = v; while (id > 1) { id >>= 1; tree[id] = cmb(tree[id << 1], tree[id << 1 | 1]); } } pair<int, int> get(int u, int v, int id, int l, int r) { if (u <= l && r <= v) { return tree[id]; } int mid = l + r >> 1; if (u <= mid && mid < v) { return cmb(get(u, v, id << 1, l, mid), get(u, v, id << 1 | 1, mid + 1, r)); } if (mid < u) { return get(u, v, id << 1 | 1, mid + 1, r); } return get(u, v, id << 1, l, mid); } pair<int, int> get(int l, int r) { return get(l, r, 1, 1, n); } }; signed main() { ios::sync_with_stdio(false); cin.tie(0); #ifdef ngu freopen("test.inp", "r", stdin); freopen("test.out", "w", stdout); #endif cin >> n >> k >> m; vector<int> inc, dec; for (int i = 1; i <= m; ++i) { cin >> a[i] >> b[i]; (a[i] < b[i] ? inc : dec).emplace_back(i); } for (int i : inc) { in[a[i]].emplace_back(b[i]); out[min(b[i] - 1, a[i] + k - 1)].emplace_back(b[i]); } multiset<int> s; for (int i = 1; i <= n; ++i) { st[0][i] = {i, i}; for (int r : in[i]) { s.insert(r); } if (s.size()) { st[0][i].second = *s.rbegin(); } for (int r : out[i]) { s.erase(s.find(r)); } in[i].clear(); out[i].clear(); } s.clear(); for (int i : dec) { in[a[i]].emplace_back(b[i]); out[max(b[i] + 1, a[i] - k + 1)].emplace_back(b[i]); } for (int i = n; i >= 1; --i) { for (int l : in[i]) { s.insert(l); } if (s.size()) { st[0][i].first = *s.begin(); } for (int l : out[i]) { s.erase(s.find(l)); } } vector<segment_tree> it(LOG); for (int i = 0; i < LOG; ++i) { it[i] = segment_tree(n); } for (int i = 1; i <= n; ++i) { it[0].upd(i, st[0][i]); } for (int j = 1; j < LOG; ++j) { for (int i = 1; i <= n; ++i) { st[j][i] = it[j - 1].get(st[j - 1][i].first, st[j - 1][i].second); } for (int i = 1; i <= n; ++i) { it[j].upd(i, st[j][i]); } } auto reach = [&](pair<int, int> x, int i) -> bool { return x.first <= i && i <= x.second; }; cin >> q; while (q--) { int s, t; cin >> s >> t; if (reach(st[LOG - 1][s], t)) { pair<int, int> cur = {s, s}; int res = 0; for (int i = LOG - 2; i >= 0; --i) { pair<int, int> new_cur = it[i].get(cur.first, cur.second); if (!reach(new_cur, t)) { cur = new_cur; res += 1 << i; } } cout << res + 1 << "\n"; } else { cout << -1 << "\n"; } } }

Compilation message (stderr)

Main.cpp: In member function 'void segment_tree::upd(int, std::pair<int, int>)':
Main.cpp:36:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   36 |             int mid = l + r >> 1;
      |                       ~~^~~
Main.cpp: In member function 'std::pair<int, int> segment_tree::get(int, int, int, int, int)':
Main.cpp:56:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   56 |         int mid = l + r >> 1;
      |                   ~~^~~
#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...