// Karol Kosinski 2020 #include <cstdio> #include <utility> #include <queue> #define FR_(i,a,b) for(int i=(a);i<=(b);++i) #define FD_(i,b,a) for(int i=(b);i>=(a);--i) //#define DEBUG(x...) printf(x) #define DEBUG(x...) using namespace std; //typedef long long LL; const int NX = 500'003; const int MOD = 1'000'000'007; int ipre; int Aim[NX], LA[NX], RA[NX]; int Par[NX], L[NX], R[NX], S[NX]; int LinL[NX], LinR[NX]; void fill_rec(int v) { if (L[v] != 0) fill_rec(L[v]); if (R[v] != 0) fill_rec(R[v]); int left = L[v], right = R[v]; int aux = (LinL[left] + LinR[right]) % MOD; S[v] = S[left] + 1 + S[right]; LinL[v] = (aux + S[right]) % MOD; LinR[v] = (aux + S[left]) % MOD; } void print_tabs(int n) { FR_(i,1,n) DEBUG("%4d", i); DEBUG("\n"); FR_(i,1,n) DEBUG("%4d", Par[i]); DEBUG("\n"); FR_(i,1,n) DEBUG("%4d", L[i]); DEBUG("\n"); FR_(i,1,n) DEBUG("%4d", R[i]); DEBUG("\n"); FR_(i,1,n) DEBUG("%4d", S[i]); DEBUG("\n"); FR_(i,1,n) DEBUG("%4d", LinL[i]); DEBUG("\n"); FR_(i,1,n) DEBUG("%4d", LinR[i]); DEBUG("\n\n"); } int count(int x, int aim_root, int& mlp, int& mrp) { int res = 0; int l_lin = LinL[ L[x] ], r_lin = LinR[ R[x] ]; int l_nodes = S[ L[x] ], r_nodes = S[ R[x] ]; mlp = mrp = x; do { int px = Par[x]; if (x == L[px]) { res = (res + r_lin) % MOD; res = (res + 2 * r_nodes + 1) % MOD; DEBUG("[L %d] res = %d\n", x, res); while (px != aim_root and px == L[ Par[px] ]) { x = px; px = Par[px]; res = (res + LinR[ R[x] ]) % MOD; res = (res + 2 * S[ R[x] ] + 1) % MOD; DEBUG("+[L %d] res = %d\n", x, res); r_nodes += S[ R[x] ] + 1; } r_lin = LinR[ R[px] ]; r_nodes += S[ R[px] ] + 1; mrp = px; } else // x == R[px] { res = (res + l_lin) % MOD; res = (res + 2 * l_nodes + 1) % MOD; DEBUG("[R %d] res = %d\n", x, res); while (px != aim_root and px == R[ Par[px] ]) { x = px; px = Par[px]; res = (res + LinL[ L[x] ]) % MOD; res = (res + 2 * S[ L[x] ] + 1) % MOD; DEBUG("+[R %d] res = %d\n", x, res); l_nodes += S[ L[x] ] + 1; } l_lin = LinL[ L[px] ]; l_nodes += S[ L[px] ] + 1; mlp = px; } x = px; } while (x != aim_root); return res; } void update(int x, int old_x, int left, int right) { int f = S[ L[left] ], g = S[ R[right] ]; int fl = LinL[ L[left] ], gr = LinR[ R[right] ]; if (old_x == L[ Par[old_x] ]) L[ Par[old_x] ] = x; else R[ Par[old_x] ] = x; Par[x] = Par[old_x]; if (x != left) L[x] = x - 1; if (x != right) R[x] = x + 1; S[x] = f + g + right - left + 1; FR_(i, left, x - 1) { Par[i] = i + 1; if (i != left) L[i] = i - 1; R[i] = 0; LinL[i] = fl; LinR[i] = (fl + f) % MOD; S[i] = ++f; } FD_(i, right, x + 1) { Par[i] = i - 1; L[i] = 0; if (i != right) R[i] = i + 1; LinL[i] = (gr + g) % MOD; LinR[i] = gr; S[i] = ++g; } int aux = (LinL[ L[x] ] + LinR[ R[x] ]) % MOD; LinL[x] = (aux + S[ R[x] ]) % MOD; LinR[x] = (aux + S[ L[x] ]) % MOD; } int calc_result(int n, int root, int aim_root) { int sum = 0; queue<pair<int, int>> Q; Q.emplace(aim_root, root); while (not Q.empty()) { auto& p = Q.front(); DEBUG("--[ %d --> %d ]--\n", p.first, p.second); if (p.first != p.second) { int mlp, mrp; sum += count(p.first, p.second, mlp, mrp); sum %= MOD; update(p.first, p.second, mlp, mrp); //print_tabs(n); // DEBUG } if (LA[p.first] != 0) Q.emplace(LA[p.first], L[p.first]); if (RA[p.first] != 0) Q.emplace(RA[p.first], R[p.first]); Q.pop(); DEBUG("sum = %d\n", sum); } return sum; } int main() { int n, root, aim_root, sum = 0; scanf("%d", &n); FR_(i,1,n) scanf("%d", Par + i); FR_(i,1,n) scanf("%d", Aim + i); FR_(i,1,n) { if (Par[i] == -1) root = i; else if (i < Par[i]) L[ Par[i] ] = i; else R[ Par[i] ] = i; if (Aim[i] == -1) aim_root = i; else if (i < Aim[i]) LA[ Aim[i] ] = i; else RA[ Aim[i] ] = i; } fill_rec(root); //print_tabs(n); // DEBUG printf("%d\n", calc_result(n, root, aim_root)); return 0; }
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 | // Karol Kosinski 2020 #include <cstdio> #include <utility> #include <queue> #define FR_(i,a,b) for(int i=(a);i<=(b);++i) #define FD_(i,b,a) for(int i=(b);i>=(a);--i) //#define DEBUG(x...) printf(x) #define DEBUG(x...) using namespace std; //typedef long long LL; const int NX = 500'003; const int MOD = 1'000'000'007; int ipre; int Aim[NX], LA[NX], RA[NX]; int Par[NX], L[NX], R[NX], S[NX]; int LinL[NX], LinR[NX]; void fill_rec(int v) { if (L[v] != 0) fill_rec(L[v]); if (R[v] != 0) fill_rec(R[v]); int left = L[v], right = R[v]; int aux = (LinL[left] + LinR[right]) % MOD; S[v] = S[left] + 1 + S[right]; LinL[v] = (aux + S[right]) % MOD; LinR[v] = (aux + S[left]) % MOD; } void print_tabs(int n) { FR_(i,1,n) DEBUG("%4d", i); DEBUG("\n"); FR_(i,1,n) DEBUG("%4d", Par[i]); DEBUG("\n"); FR_(i,1,n) DEBUG("%4d", L[i]); DEBUG("\n"); FR_(i,1,n) DEBUG("%4d", R[i]); DEBUG("\n"); FR_(i,1,n) DEBUG("%4d", S[i]); DEBUG("\n"); FR_(i,1,n) DEBUG("%4d", LinL[i]); DEBUG("\n"); FR_(i,1,n) DEBUG("%4d", LinR[i]); DEBUG("\n\n"); } int count(int x, int aim_root, int& mlp, int& mrp) { int res = 0; int l_lin = LinL[ L[x] ], r_lin = LinR[ R[x] ]; int l_nodes = S[ L[x] ], r_nodes = S[ R[x] ]; mlp = mrp = x; do { int px = Par[x]; if (x == L[px]) { res = (res + r_lin) % MOD; res = (res + 2 * r_nodes + 1) % MOD; DEBUG("[L %d] res = %d\n", x, res); while (px != aim_root and px == L[ Par[px] ]) { x = px; px = Par[px]; res = (res + LinR[ R[x] ]) % MOD; res = (res + 2 * S[ R[x] ] + 1) % MOD; DEBUG("+[L %d] res = %d\n", x, res); r_nodes += S[ R[x] ] + 1; } r_lin = LinR[ R[px] ]; r_nodes += S[ R[px] ] + 1; mrp = px; } else // x == R[px] { res = (res + l_lin) % MOD; res = (res + 2 * l_nodes + 1) % MOD; DEBUG("[R %d] res = %d\n", x, res); while (px != aim_root and px == R[ Par[px] ]) { x = px; px = Par[px]; res = (res + LinL[ L[x] ]) % MOD; res = (res + 2 * S[ L[x] ] + 1) % MOD; DEBUG("+[R %d] res = %d\n", x, res); l_nodes += S[ L[x] ] + 1; } l_lin = LinL[ L[px] ]; l_nodes += S[ L[px] ] + 1; mlp = px; } x = px; } while (x != aim_root); return res; } void update(int x, int old_x, int left, int right) { int f = S[ L[left] ], g = S[ R[right] ]; int fl = LinL[ L[left] ], gr = LinR[ R[right] ]; if (old_x == L[ Par[old_x] ]) L[ Par[old_x] ] = x; else R[ Par[old_x] ] = x; Par[x] = Par[old_x]; if (x != left) L[x] = x - 1; if (x != right) R[x] = x + 1; S[x] = f + g + right - left + 1; FR_(i, left, x - 1) { Par[i] = i + 1; if (i != left) L[i] = i - 1; R[i] = 0; LinL[i] = fl; LinR[i] = (fl + f) % MOD; S[i] = ++f; } FD_(i, right, x + 1) { Par[i] = i - 1; L[i] = 0; if (i != right) R[i] = i + 1; LinL[i] = (gr + g) % MOD; LinR[i] = gr; S[i] = ++g; } int aux = (LinL[ L[x] ] + LinR[ R[x] ]) % MOD; LinL[x] = (aux + S[ R[x] ]) % MOD; LinR[x] = (aux + S[ L[x] ]) % MOD; } int calc_result(int n, int root, int aim_root) { int sum = 0; queue<pair<int, int>> Q; Q.emplace(aim_root, root); while (not Q.empty()) { auto& p = Q.front(); DEBUG("--[ %d --> %d ]--\n", p.first, p.second); if (p.first != p.second) { int mlp, mrp; sum += count(p.first, p.second, mlp, mrp); sum %= MOD; update(p.first, p.second, mlp, mrp); //print_tabs(n); // DEBUG } if (LA[p.first] != 0) Q.emplace(LA[p.first], L[p.first]); if (RA[p.first] != 0) Q.emplace(RA[p.first], R[p.first]); Q.pop(); DEBUG("sum = %d\n", sum); } return sum; } int main() { int n, root, aim_root, sum = 0; scanf("%d", &n); FR_(i,1,n) scanf("%d", Par + i); FR_(i,1,n) scanf("%d", Aim + i); FR_(i,1,n) { if (Par[i] == -1) root = i; else if (i < Par[i]) L[ Par[i] ] = i; else R[ Par[i] ] = i; if (Aim[i] == -1) aim_root = i; else if (i < Aim[i]) LA[ Aim[i] ] = i; else RA[ Aim[i] ] = i; } fill_rec(root); //print_tabs(n); // DEBUG printf("%d\n", calc_result(n, root, aim_root)); return 0; } |