#include "bits/stdc++.h" // Tomasz Nowak using namespace std; // XIII LO Szczecin using LL = long long; // Poland #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif mt19937_64 rng(0); int rd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } // end of templates LL answer = 0; struct Node { int root = -1; array<int, 2> interval = {-1, -1}; array<int, 2> endpath = {-1, -1}; bool has_child(bool c) { return interval[c] != root or endpath[c] != -1; } void reroot(int v) { assert(interval[0] <= v and v <= interval[1]); answer += abs(root - v); root = v; } } empty_node; ostream& operator<<(ostream &o, Node &n) { return o << vector{n.interval[0], n.root, n.interval[1], n.endpath[0], n.endpath[1]}; } bool operator==(Node a, Node b) { return a.root == b.root and a.interval == b.interval and a.endpath == b.endpath; } void merge_down(vector<Node> &tree, int v, bool side) { debug(tree); int w = tree[v].endpath[side]; assert(w != -1 and not tree[w].has_child(not side)); assert(abs(tree[v].interval[side] - tree[w].root) == 1); tree[v].interval[side] = tree[w].interval[side]; tree[v].endpath[side] = tree[w].endpath[side]; tree[w] = empty_node; } void disappear_subtree(vector<Node> &tree, int root, bool side_rm) { debug(root, side_rm); tree[root].reroot(tree[root].interval[side_rm]); while(tree[root].has_child(side_rm)) { int son_rm = tree[root].endpath[side_rm]; disappear_subtree(tree, son_rm, not side_rm); merge_down(tree, root, side_rm); tree[root].reroot(tree[root].interval[side_rm]); } } int retrieve_child(vector<Node> &tree, int root, bool side) { if(tree[root].root == tree[root].interval[side]) return tree[root].endpath[side]; debug(root, side) << "retrieve"; int n = size(tree); tree.emplace_back(empty_node); int w = tree[root].root + (side == 0 ? -1 : 1); debug(root, w); tree[n].endpath[side] = tree[root].endpath[side]; tree[root].endpath[side] = n; tree[n].root = w; tree[n].interval = {w, w}; tree[n].interval[side] = tree[root].interval[side]; tree[root].interval[side] = tree[root].root; return n; } bool intersects(array<int, 2> &a, array<int, 2> &b) { return min(a[1], b[1]) - max(a[0], b[0]) >= 0; } void solve(vector<Node> *tree1, vector<Node> *tree2, int r1, int r2) { debug(r1, r2); debug(*tree1, *tree2) << "starting moving root"; if(r1 == -1 and r2 == -1) return; assert(r1 != -1 and r2 != -1); auto fix_root = [&] { auto it1 = (*tree1)[r1].interval, it2 = (*tree2)[r2].interval; if(intersects(it1, it2)) { int mid1 = (*tree1)[r1].root, mid2 = (*tree2)[r2].root; debug(it1, mid1, it2, mid2); vector<int> candidates = {max(it1[0], it2[0]), min(it1[1], it2[1]), mid1, mid2}; for(int c : candidates) if(max(it1[0], it2[0]) <= c and c <= min(it1[1], it2[1]) and min(mid1, mid2) <= c and c <= max(mid1, mid2)) { (*tree1)[r1].reroot(c); (*tree2)[r2].reroot(c); return; } assert(false); } if(it1[1] < it2[0]) { swap(r1, r2); swap(it1, it2); swap(tree1, tree2); } vector<Node> &t1 = *tree1, &t2 = *tree2; debug(t1[r1].interval, t2[r2].interval) << "START"; while(t2[r2].root < t1[r1].interval[0]) { // t1[r1].reroot(t1[r1].interval[0]); disappear_subtree(t1, t1[r1].endpath[0], 1); merge_down(t1, r1, 0); } debug(t1[r1].interval, t2[r2].interval, t2[r2].root) << "END"; t1[r1].reroot(t2[r2].root); }; fix_root(); vector<Node> &t1 = *tree1, &t2 = *tree2; debug(t1, t2) << "finished moving root"; for(int c : {0, 1}) solve(tree1, tree2, retrieve_child(t1, r1, c), retrieve_child(t2, r2, c)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; auto input_tree = [&] { vector<Node> ret(n); int root = -1; REP(v, n) { ret[v].root = v; ret[v].interval = {v, v}; int p; cin >> p; if(p == -1) root = v; else { --p; ret[p].endpath[bool(p < v)] = v; } } return make_pair(ret, root); }; auto [tree1, root1] = input_tree(); auto [tree2, root2] = input_tree(); debug(tree1, tree2); solve(&tree1, &tree2, root1, root2); debug(tree1, tree2); cout << answer << '\n'; }
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | #include "bits/stdc++.h" // Tomasz Nowak using namespace std; // XIII LO Szczecin using LL = long long; // Poland #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif mt19937_64 rng(0); int rd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } // end of templates LL answer = 0; struct Node { int root = -1; array<int, 2> interval = {-1, -1}; array<int, 2> endpath = {-1, -1}; bool has_child(bool c) { return interval[c] != root or endpath[c] != -1; } void reroot(int v) { assert(interval[0] <= v and v <= interval[1]); answer += abs(root - v); root = v; } } empty_node; ostream& operator<<(ostream &o, Node &n) { return o << vector{n.interval[0], n.root, n.interval[1], n.endpath[0], n.endpath[1]}; } bool operator==(Node a, Node b) { return a.root == b.root and a.interval == b.interval and a.endpath == b.endpath; } void merge_down(vector<Node> &tree, int v, bool side) { debug(tree); int w = tree[v].endpath[side]; assert(w != -1 and not tree[w].has_child(not side)); assert(abs(tree[v].interval[side] - tree[w].root) == 1); tree[v].interval[side] = tree[w].interval[side]; tree[v].endpath[side] = tree[w].endpath[side]; tree[w] = empty_node; } void disappear_subtree(vector<Node> &tree, int root, bool side_rm) { debug(root, side_rm); tree[root].reroot(tree[root].interval[side_rm]); while(tree[root].has_child(side_rm)) { int son_rm = tree[root].endpath[side_rm]; disappear_subtree(tree, son_rm, not side_rm); merge_down(tree, root, side_rm); tree[root].reroot(tree[root].interval[side_rm]); } } int retrieve_child(vector<Node> &tree, int root, bool side) { if(tree[root].root == tree[root].interval[side]) return tree[root].endpath[side]; debug(root, side) << "retrieve"; int n = size(tree); tree.emplace_back(empty_node); int w = tree[root].root + (side == 0 ? -1 : 1); debug(root, w); tree[n].endpath[side] = tree[root].endpath[side]; tree[root].endpath[side] = n; tree[n].root = w; tree[n].interval = {w, w}; tree[n].interval[side] = tree[root].interval[side]; tree[root].interval[side] = tree[root].root; return n; } bool intersects(array<int, 2> &a, array<int, 2> &b) { return min(a[1], b[1]) - max(a[0], b[0]) >= 0; } void solve(vector<Node> *tree1, vector<Node> *tree2, int r1, int r2) { debug(r1, r2); debug(*tree1, *tree2) << "starting moving root"; if(r1 == -1 and r2 == -1) return; assert(r1 != -1 and r2 != -1); auto fix_root = [&] { auto it1 = (*tree1)[r1].interval, it2 = (*tree2)[r2].interval; if(intersects(it1, it2)) { int mid1 = (*tree1)[r1].root, mid2 = (*tree2)[r2].root; debug(it1, mid1, it2, mid2); vector<int> candidates = {max(it1[0], it2[0]), min(it1[1], it2[1]), mid1, mid2}; for(int c : candidates) if(max(it1[0], it2[0]) <= c and c <= min(it1[1], it2[1]) and min(mid1, mid2) <= c and c <= max(mid1, mid2)) { (*tree1)[r1].reroot(c); (*tree2)[r2].reroot(c); return; } assert(false); } if(it1[1] < it2[0]) { swap(r1, r2); swap(it1, it2); swap(tree1, tree2); } vector<Node> &t1 = *tree1, &t2 = *tree2; debug(t1[r1].interval, t2[r2].interval) << "START"; while(t2[r2].root < t1[r1].interval[0]) { // t1[r1].reroot(t1[r1].interval[0]); disappear_subtree(t1, t1[r1].endpath[0], 1); merge_down(t1, r1, 0); } debug(t1[r1].interval, t2[r2].interval, t2[r2].root) << "END"; t1[r1].reroot(t2[r2].root); }; fix_root(); vector<Node> &t1 = *tree1, &t2 = *tree2; debug(t1, t2) << "finished moving root"; for(int c : {0, 1}) solve(tree1, tree2, retrieve_child(t1, r1, c), retrieve_child(t2, r2, c)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; auto input_tree = [&] { vector<Node> ret(n); int root = -1; REP(v, n) { ret[v].root = v; ret[v].interval = {v, v}; int p; cin >> p; if(p == -1) root = v; else { --p; ret[p].endpath[bool(p < v)] = v; } } return make_pair(ret, root); }; auto [tree1, root1] = input_tree(); auto [tree2, root2] = input_tree(); debug(tree1, tree2); solve(&tree1, &tree2, root1, root2); debug(tree1, tree2); cout << answer << '\n'; } |