#include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } const int nax = 1e6 + 5; #define left left_compile #define right right_compile int left[nax], right[nax]; int subtree[nax]; long long dfs(int a, int b, int pref, int suf) { assert(pref == 0 || suf == 0); if(a == 0 && b == 0) { return 0; } long long my_value = 0; debug() << imie(a) imie(b) imie(pref) imie(suf); for(int rep = 0; rep < 2; ++rep) { if(b == 0) { assert(pref || suf); my_value += dfs(left[a], 0, 0, subtree[left[a]]); my_value += dfs(right[a], 0, subtree[right[a]], 0); if(pref) { my_value += subtree[left[a]]; } else { my_value += subtree[right[a]]; } debug() << "a" imie(a) imie(b) imie(pref) imie(suf) imie(my_value); return my_value; } swap(a, b); } assert(a > 0 && b > 0); for(int rep = 0; rep < 2; ++rep) { if(subtree[a] > subtree[b] && pref >= 1 + subtree[left[a]]) { my_value += subtree[left[a]]; my_value += dfs(right[a], b, pref - (1+subtree[left[a]]), 0); my_value += dfs(left[a], 0, 0, subtree[left[a]]); debug() << "b" imie(a) imie(b) imie(pref) imie(suf) imie(my_value); return my_value; } // if(left[a] == 0 && subtree[a] == subtree[b]) { // assert(pref == 0 && suf == 0); // dfs(right[a], b, 1, 0); // return; // } if(subtree[a] > subtree[b] && suf >= 1 + subtree[right[a]]) { my_value += subtree[right[a]]; my_value += dfs(left[a], b, 0, suf - (1+subtree[right[a]])); my_value += dfs(right[a], 0, subtree[right[a]], 0); // dfs(left[a], b, 0, suf - 1); debug() << "c" imie(a) imie(b) imie(pref) imie(suf) imie(my_value); return my_value; } // if(right[a] == 0 && subtree[a] == subtree[b]) { // assert(pref == 0 && suf == 0); // dfs(left[a], b, 0, 1); // } swap(a, b); } if(pref == 0 && suf == 0) { my_value += abs(subtree[left[a]] - subtree[left[b]]); } if(pref == 0) { my_value += dfs(left[a], left[b], 0, abs(subtree[left[a]] - subtree[left[b]])); } else { my_value += subtree[left[a]] + subtree[left[b]]; my_value += dfs(left[a], 0, 0, subtree[left[a]]); my_value += dfs(0, left[b], 0, subtree[left[b]]); } if(suf == 0) { my_value += dfs(right[a], right[b], abs(subtree[right[a]] - subtree[right[b]]), 0); } else { // debug() << imie(answer) imie(a) imie(b); my_value += subtree[right[a]] + subtree[right[b]]; // debug() << imie(answer); my_value += dfs(right[a], 0, subtree[right[a]], 0); my_value += dfs(0, right[b], subtree[right[b]], 0); } debug() << "d" imie(a) imie(b) imie(pref) imie(suf) imie(my_value); return my_value; } void dfs_subtree(int a) { subtree[a] = 1; for(int b : {left[a], right[a]}) { if(b != 0) { dfs_subtree(b); subtree[a] += subtree[b]; } } } int main() { int n; scanf("%d", &n); int root[2] = {-1, -1}; for(int rep = 0; rep < 2; ++rep) { int add = rep * n; for(int i = 1; i <= n; ++i) { int par; scanf("%d", &par); if(par == -1) { root[rep] = i+add; } else { if(i < par) { left[par+add] = i+add; } else { right[par+add] = i+add; } } } } dfs_subtree(root[0]); dfs_subtree(root[1]); debug() << range(subtree + 1, subtree + n + 1); debug() << range(subtree + n + 1, subtree + 2 * n + 1); long long answer = dfs(root[0], root[1], 0, 0); printf("%lld\n", answer % 1'000'000'007); }
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 | #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } const int nax = 1e6 + 5; #define left left_compile #define right right_compile int left[nax], right[nax]; int subtree[nax]; long long dfs(int a, int b, int pref, int suf) { assert(pref == 0 || suf == 0); if(a == 0 && b == 0) { return 0; } long long my_value = 0; debug() << imie(a) imie(b) imie(pref) imie(suf); for(int rep = 0; rep < 2; ++rep) { if(b == 0) { assert(pref || suf); my_value += dfs(left[a], 0, 0, subtree[left[a]]); my_value += dfs(right[a], 0, subtree[right[a]], 0); if(pref) { my_value += subtree[left[a]]; } else { my_value += subtree[right[a]]; } debug() << "a" imie(a) imie(b) imie(pref) imie(suf) imie(my_value); return my_value; } swap(a, b); } assert(a > 0 && b > 0); for(int rep = 0; rep < 2; ++rep) { if(subtree[a] > subtree[b] && pref >= 1 + subtree[left[a]]) { my_value += subtree[left[a]]; my_value += dfs(right[a], b, pref - (1+subtree[left[a]]), 0); my_value += dfs(left[a], 0, 0, subtree[left[a]]); debug() << "b" imie(a) imie(b) imie(pref) imie(suf) imie(my_value); return my_value; } // if(left[a] == 0 && subtree[a] == subtree[b]) { // assert(pref == 0 && suf == 0); // dfs(right[a], b, 1, 0); // return; // } if(subtree[a] > subtree[b] && suf >= 1 + subtree[right[a]]) { my_value += subtree[right[a]]; my_value += dfs(left[a], b, 0, suf - (1+subtree[right[a]])); my_value += dfs(right[a], 0, subtree[right[a]], 0); // dfs(left[a], b, 0, suf - 1); debug() << "c" imie(a) imie(b) imie(pref) imie(suf) imie(my_value); return my_value; } // if(right[a] == 0 && subtree[a] == subtree[b]) { // assert(pref == 0 && suf == 0); // dfs(left[a], b, 0, 1); // } swap(a, b); } if(pref == 0 && suf == 0) { my_value += abs(subtree[left[a]] - subtree[left[b]]); } if(pref == 0) { my_value += dfs(left[a], left[b], 0, abs(subtree[left[a]] - subtree[left[b]])); } else { my_value += subtree[left[a]] + subtree[left[b]]; my_value += dfs(left[a], 0, 0, subtree[left[a]]); my_value += dfs(0, left[b], 0, subtree[left[b]]); } if(suf == 0) { my_value += dfs(right[a], right[b], abs(subtree[right[a]] - subtree[right[b]]), 0); } else { // debug() << imie(answer) imie(a) imie(b); my_value += subtree[right[a]] + subtree[right[b]]; // debug() << imie(answer); my_value += dfs(right[a], 0, subtree[right[a]], 0); my_value += dfs(0, right[b], subtree[right[b]], 0); } debug() << "d" imie(a) imie(b) imie(pref) imie(suf) imie(my_value); return my_value; } void dfs_subtree(int a) { subtree[a] = 1; for(int b : {left[a], right[a]}) { if(b != 0) { dfs_subtree(b); subtree[a] += subtree[b]; } } } int main() { int n; scanf("%d", &n); int root[2] = {-1, -1}; for(int rep = 0; rep < 2; ++rep) { int add = rep * n; for(int i = 1; i <= n; ++i) { int par; scanf("%d", &par); if(par == -1) { root[rep] = i+add; } else { if(i < par) { left[par+add] = i+add; } else { right[par+add] = i+add; } } } } dfs_subtree(root[0]); dfs_subtree(root[1]); debug() << range(subtree + 1, subtree + n + 1); debug() << range(subtree + n + 1, subtree + 2 * n + 1); long long answer = dfs(root[0], root[1], 0, 0); printf("%lld\n", answer % 1'000'000'007); } |