1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| #include <algorithm> #include <cassert> #include <functional> #include <iostream> #include <numeric> #include <random> #include <utility> #include <vector>
using u64 = unsigned long long; constexpr u64 P = 4816069; std::mt19937_64 rng(std::random_device{}()); std::vector<u64> base, rd;
struct ModTree { struct Node { int l, r; u64 fhash, bhash; }; std::vector<Node> tree; int n;
Node update(Node l, Node r) { Node res; res.l = l.l, res.r = r.r; res.fhash = l.fhash * base[r.r - r.l + 1] + r.fhash; res.bhash = r.bhash * base[l.r - l.l + 1] + l.bhash; return res; } void init(int n) { tree.assign(n * 4, Node()); this->n = n; } void build(std::vector<u64> &vec, int p, int l, int r) { tree[p].l = l, tree[p].r = r; if (l == r) { tree[p].fhash = tree[p].bhash = vec[l]; return; } int mid = (l + r) >> 1; build(vec, p << 1, l, mid); build(vec, p << 1 | 1, mid + 1, r); tree[p] = update(tree[p << 1], tree[p << 1 | 1]); } void modify(int p, int pos, u64 val) { if (tree[p].l == tree[p].r) { tree[p].fhash = tree[p].bhash = val; return; } int mid = (tree[p].l + tree[p].r) >> 1; if (pos <= mid) modify(p << 1, pos, val); else modify(p << 1 | 1, pos, val); tree[p] = update(tree[p << 1], tree[p << 1 | 1]); } Node query(int p, int l, int r) { if (l <= tree[p].l && tree[p].r <= r) return tree[p]; int mid = (tree[p].l + tree[p].r) >> 1; if (r <= mid) return query(p << 1, l, r); else if (l > mid) return query(p << 1 | 1, l, r); Node res = update(query(p << 1, l, mid), query(p << 1 | 1, mid + 1, r)); return res; } };
int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);
int n; std::cin >> n; [&] { base.resize(n + 1), rd.resize(n + 1); base[0] = 1; for (int i = 1; i <= n; i++) base[i] = base[i - 1] * P, rd[i] = rng(); }(); std::vector<std::pair<u64, int>> vec; std::vector<std::vector<int>> nodes(n + 1); for (int i = 3; i < n * 2; i++) { u64 x; std::cin >> x; vec.emplace_back(x, i); }
std::vector<int> fa(n + 1, 0); std::iota(fa.begin(), fa.end(), 0); std::function<int(int)> find = [&](int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); };
ModTree tree; tree.init(n); tree.build(rd, 1, 1, n); for (int i = 1; i <= n; i++) nodes[i].push_back(i);
auto unite = [&](int u, int v) { u = find(u), v = find(v); if (u == v) return; if (nodes[u].size() > nodes[v].size()) std::swap(u, v); fa[u] = v; for (int i : nodes[u]) { nodes[v].emplace_back(i); rd[i] = rd[v]; tree.modify(1, i, rd[i]); } nodes[u].clear(); };
std::sort(vec.begin(), vec.end());
long long ans = 0; for (auto &[E, id] : vec) { int l = (id - 1) / 2, r = id - l; int size = std::min(l, n - r + 1); int L = l - size + 1, R = r + size - 1;
while (r <= R) { if (tree.query(1, L, l).fhash == tree.query(1, r, R).bhash) break;
int lb = 0, ub = R - r; while (lb <= ub) { int mid = (lb + ub) >> 1; if (tree.query(1, l - mid, l).fhash != tree.query(1, r, r + mid).bhash) ub = mid - 1; else lb = mid + 1; }
l -= lb, r += lb; unite(l, r); ans += E; } } std::cout << ans << '\n';
return 0; }
|