Submission #885350

#TimeUsernameProblemLanguageResultExecution timeMemory
885350KoyoteFortune Telling 2 (JOI14_fortune_telling2)C++17
100 / 100
660 ms215632 KiB
#include <bits/stdc++.h> using namespace std; #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() // Persistent Segment Tree struct node { node *l, *r; int val; node() : l(0), r(0), val(0) {} node(int _v) : l(0), r(0), val(_v) {} node(node *_l, node *_r) : l(_l), r(_r), val(l->val + r->val) {} void extend() { if (!l) l = new node(); if (!r) r = new node(); } }; node* update(node *cur, int p, int v, int l, int r) { if (l == r) return new node(cur->val + v); cur->extend(); int mid = (l + r) >> 1; if (p <= mid) return new node(update(cur->l, p, v, l, mid), cur->r); else return new node(cur->l, update(cur->r, p, v, mid + 1, r)); } int query(node *cur, int qs, int qe, int l, int r) { if (r < qs || qe < l) return 0; if (qs <= l && r <= qe) return cur->val; cur->extend(); int mid = (l + r) >> 1; return query(cur->l, qs, qe, l, mid) + query(cur->r, qs, qe, mid + 1, r); } // End of Persistent Segment Tree const int N = 2e5 + 7, N2 = 6e5 + 2, LG = 22; int n, k, a[N], b[N], t[N], t_idx[N2], max_t_idx[LG][N2]; vector<node*> roots; // PST constexpr int lg2(const int x) { return 31 - __builtin_clz(x); } int main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> n >> k, roots.reserve(k + 1), roots.push_back(new node()); for (int i = 1; i <= n; i++) cin >> a[i] >> b[i]; for (int i = 1; i <= k; i++) cin >> t[i]; basic_string<int> cmpr; cmpr.reserve(2 * n + k + 1), cmpr += 0; for (int i = 1; i <= n; i++) cmpr += a[i], cmpr += b[i]; for (int i = 1; i <= k; i++) cmpr += t[i]; sort(all(cmpr)), cmpr.erase(unique(all(cmpr)), cmpr.end()); auto search = [&](const int v) -> int { return lower_bound(all(cmpr), v) - cmpr.begin(); }; for (int i = 1; i <= n; i++) a[i] = search(a[i]), b[i] = search(b[i]); for (int i = 1; i <= k; i++) t[i] = search(t[i]); // for (int i = 0; i <= sz(cmpr); i++) t_idx[i] = -1; for (int i = 1; i <= k; i++) t_idx[t[i]] = i; // Sparse table for (int i = 1; i <= sz(cmpr); i++) max_t_idx[0][i] = t_idx[i]; for (int j = 1; j < LG; j++) for (int i = 1; i + (1 << j) - 1 <= sz(cmpr); i++) max_t_idx[j][i] = max(max_t_idx[j - 1][i], max_t_idx[j - 1][i + (1 << (j - 1))]); auto query_max_t_idx = [&](int l, int r) -> int { int len = lg2(r - l + 1); return max(max_t_idx[len][l], max_t_idx[len][r - (1 << len) + 1]); }; // End of Sparse table // Persistent Segment Tree for (int i = 1; i <= k; i++) roots.push_back(update(roots.back(), t[i], 1, 1, sz(cmpr))); auto cnt_on_range = [&](int l, int r, int x, int y) { // Number of elements a[l..r] that x <= a[i] <= y; return query(roots[r], x, y, 1, sz(cmpr)) - query(roots[l - 1], x, y, 1, sz(cmpr)); }; // End of Persistent Segment Tree basic_string<int> ans; ans.reserve(n); for (int i = 1; i <= n; i++) { if (a[i] == b[i]) { ans += cmpr[a[i]]; continue; } bool swapped = (a[i] > b[i] ? (swap(a[i], b[i]), true) : false); int last_pos = query_max_t_idx(a[i], b[i] - 1); int cnt_flipped = cnt_on_range(last_pos + 1, k, b[i], sz(cmpr)); if (last_pos != 0 || swapped) swap(a[i], b[i]); ans += cmpr[~cnt_flipped & 1 ? a[i] : b[i]]; } cout << accumulate(ans.begin(), ans.end(), 0LL) << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...