#include <bits/stdc++.h> #define MP make_pair #define PB push_back #define int long long #define st first #define nd second #define rd third #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define REP(i, n) for(int i = 0;i <(n); ++i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define __builtin_ctz __builtin_ctzll #define __builtin_clz __builtin_clzll #define __builtin_popcount __builtin_popcountll using namespace std; template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; } template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) { while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...); } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }} #else #define debug(...) (__VA_ARGS__) #define debugv(x) #define cerr if(0)cout #endif #define next ____next #define prev ____prev #define left ____left #define hash ____hash typedef long long ll; typedef long double LD; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VLL; typedef vector<pair<int, int> > VPII; typedef vector<pair<ll, ll> > VPLL; template<class C> void mini(C&a4, C b4){a4=min(a4, b4); } template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); } template<class T1, class T2> ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";} template<class A, class B, class C> struct Triple { A first; B second; C third; bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } }; template<class T> void ResizeVec(T&, vector<int>) {} template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) { vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; } for (T& v : vec) { ResizeVec(v, sz); } } typedef Triple<int, int, int> TIII; template<class A, class B, class C> ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; } template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } struct Node { int left, right; Node() { left = -1; right = -1; } }; typedef vector<Node> Tree; // struct Tree { // vector<Node> nodes; // }; // int signum(int x) { // if (x > 0) { return 1; } // if (x < 0) { return -1; } // return 0; // } int Right(Tree* T, int root, int x, int y); int Left(Tree* T, int root, int x, int y) { if (root == -1) { return 0; } return Left(T, (*T)[root].left, x, root - 1) + Right(T, (*T)[root].right, root + 1, y) + y - root; } int Right(Tree* T, int root, int x, int y) { if (root == -1) { return 0; } return Left(T, (*T)[root].left, x, root - 1) + Right(T, (*T)[root].right, root + 1, y) + root - x ; } void Deb(Tree* T, int root) { if (root == -1) { cerr<<"."; return; } cerr<<"("; Deb(T, (*T)[root].left); cerr<<", "<<root<<", "; Deb(T, (*T)[root].right); cerr<<")"; } int f(Tree* A, int whA, int A_path_beg, int A_path_end, Tree* B, int whB, int B_path_beg, int B_path_end, int path_dir) { // Deb(A, whA); // cerr<<endl; // Deb(B, whB); // cerr<<endl; //debug(whA, A_path_beg, A_path_end, whB, B_path_beg, B_path_end, path_dir); // int sA = signum(A_path_end - A_path_beg); // if (sA && sA != path_dir) { // A_path_beg = -1; // A_path_end = -1; // } // int sB = signum(B_path_end - B_path_beg); // if (sB && sB != path_dir) { // B_path_beg = -1; // B_path_end = -1; // } // // if (whA == -1 && A_path_beg == -1) { // assert(whB == -1 && B_path_beg == -1); // return 0; // } if (A_path_end != -1 && B_path_end != -1) { // chce skrocic czesc wspolna sciezek assert(A_path_end == B_path_end); if (A_path_beg == B_path_beg) { return f(A, whA, -1, -1, B, whB, -1, -1, 0); } if (path_dir == -1) { // korzen najwiekszy i maleje w dol if (A_path_beg > B_path_beg) { return f(A, whA, -1, -1, B, whB, B_path_beg, A_path_beg - 1, -1); } else { return f(A, whA, A_path_beg, B_path_beg - 1, B, whB, -1, -1, -1); } } else { if (A_path_beg < B_path_beg) { return f(A, whA, -1, -1, B, whB, B_path_beg, A_path_beg + 1, 1); } else { return f(A, whA, A_path_beg, B_path_beg + 1, B, whB, -1, -1, 1); } } } if (whB == -1) { swap(A, B); swap(whA, whB); swap(A_path_beg, B_path_beg); swap(A_path_end, B_path_end); } if (whA == -1) { if (A_path_beg == -1) { return 0; } if (path_dir == -1) { int L = Left(B, whB, A_path_beg, A_path_end); debug(L); return L; } else { int R = Right(B, whB, A_path_end, A_path_beg); debug(R); return R; } } int rootA = whA, rootB = whB; if (A_path_end != -1) { rootA = A_path_end; } if (B_path_end != -1) { rootB = B_path_end; } debug(rootA, rootB); if (rootA > rootB) { swap(A, B); swap(whA, whB); swap(rootA, rootB); swap(A_path_beg, B_path_beg); swap(A_path_end, B_path_end); } //debug( int diff = rootB - rootA; debug(diff); if (A_path_end == -1 && B_path_end == -1) { // nie ma zadnych sciezek if (rootA == rootB) { return f(A, (*A)[whA].left, -1, -1, B, (*B)[whB].left, -1, -1, 0) + f(A, (*A)[whA].right, -1, -1, B, (*B)[whB].right, -1, -1, 0); } return diff + f(A, (*A)[whA].left, rootA, rootB - 1, B, (*B)[whB].left, -1, -1, -1) + f(A, (*A)[whA].right, -1, -1, B, (*B)[whB].right, rootB, rootA + 1, 1); } else { if (rootA == rootB) { if ((A_path_end == -1) != (path_dir == -1)) { swap(A, B); swap(whA, whB); swap(rootA, rootB); swap(A_path_beg, B_path_beg); swap(A_path_end, B_path_end); } } if (A_path_end == -1) { // jest sciezka w prawym assert(path_dir == -1); int e1 = Right(A, (*A)[whA].right, rootA + 1, rootB); int e2 = f(A, (*A)[whA].left, rootA, rootB, B, whB, B_path_beg, B_path_end, path_dir); debug(diff, e1, e2); return diff + e1 + e2; } else {// jest sciezka w lewym assert(B_path_end == -1); assert(path_dir == 1); return diff + Left(B, (*B)[whB].left, rootA, rootB - 1) + f(A, whA, A_path_beg, A_path_end, B, (*B)[whB].right, rootB, rootA, path_dir); } } } pair<Tree, int> ReadTree(int n) { vector<Node> nodes(n + 2); int root = -1; RE (i, n) { int par; cin>>par; if (par == -1) { root = i; } else { if (par < i) { nodes[par].right = i; } else { nodes[par].left = i; } } } return {Tree{nodes}, root}; } int32_t main() { ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); cin.tie(0); //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC; int n; cin>>n; pair<Tree, int> t1 = ReadTree(n); pair<Tree, int> t2 = ReadTree(n); debug(n); const int P = 1e9 + 7; cout<<f(&t1.st, t1.nd, -1, -1, &t2.st, t2.nd, -1, -1, 0) % P<<endl; 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | #include <bits/stdc++.h> #define MP make_pair #define PB push_back #define int long long #define st first #define nd second #define rd third #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define REP(i, n) for(int i = 0;i <(n); ++i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define __builtin_ctz __builtin_ctzll #define __builtin_clz __builtin_clzll #define __builtin_popcount __builtin_popcountll using namespace std; template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; } template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) { while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...); } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }} #else #define debug(...) (__VA_ARGS__) #define debugv(x) #define cerr if(0)cout #endif #define next ____next #define prev ____prev #define left ____left #define hash ____hash typedef long long ll; typedef long double LD; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VLL; typedef vector<pair<int, int> > VPII; typedef vector<pair<ll, ll> > VPLL; template<class C> void mini(C&a4, C b4){a4=min(a4, b4); } template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); } template<class T1, class T2> ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";} template<class A, class B, class C> struct Triple { A first; B second; C third; bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } }; template<class T> void ResizeVec(T&, vector<int>) {} template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) { vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; } for (T& v : vec) { ResizeVec(v, sz); } } typedef Triple<int, int, int> TIII; template<class A, class B, class C> ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; } template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } struct Node { int left, right; Node() { left = -1; right = -1; } }; typedef vector<Node> Tree; // struct Tree { // vector<Node> nodes; // }; // int signum(int x) { // if (x > 0) { return 1; } // if (x < 0) { return -1; } // return 0; // } int Right(Tree* T, int root, int x, int y); int Left(Tree* T, int root, int x, int y) { if (root == -1) { return 0; } return Left(T, (*T)[root].left, x, root - 1) + Right(T, (*T)[root].right, root + 1, y) + y - root; } int Right(Tree* T, int root, int x, int y) { if (root == -1) { return 0; } return Left(T, (*T)[root].left, x, root - 1) + Right(T, (*T)[root].right, root + 1, y) + root - x ; } void Deb(Tree* T, int root) { if (root == -1) { cerr<<"."; return; } cerr<<"("; Deb(T, (*T)[root].left); cerr<<", "<<root<<", "; Deb(T, (*T)[root].right); cerr<<")"; } int f(Tree* A, int whA, int A_path_beg, int A_path_end, Tree* B, int whB, int B_path_beg, int B_path_end, int path_dir) { // Deb(A, whA); // cerr<<endl; // Deb(B, whB); // cerr<<endl; //debug(whA, A_path_beg, A_path_end, whB, B_path_beg, B_path_end, path_dir); // int sA = signum(A_path_end - A_path_beg); // if (sA && sA != path_dir) { // A_path_beg = -1; // A_path_end = -1; // } // int sB = signum(B_path_end - B_path_beg); // if (sB && sB != path_dir) { // B_path_beg = -1; // B_path_end = -1; // } // // if (whA == -1 && A_path_beg == -1) { // assert(whB == -1 && B_path_beg == -1); // return 0; // } if (A_path_end != -1 && B_path_end != -1) { // chce skrocic czesc wspolna sciezek assert(A_path_end == B_path_end); if (A_path_beg == B_path_beg) { return f(A, whA, -1, -1, B, whB, -1, -1, 0); } if (path_dir == -1) { // korzen najwiekszy i maleje w dol if (A_path_beg > B_path_beg) { return f(A, whA, -1, -1, B, whB, B_path_beg, A_path_beg - 1, -1); } else { return f(A, whA, A_path_beg, B_path_beg - 1, B, whB, -1, -1, -1); } } else { if (A_path_beg < B_path_beg) { return f(A, whA, -1, -1, B, whB, B_path_beg, A_path_beg + 1, 1); } else { return f(A, whA, A_path_beg, B_path_beg + 1, B, whB, -1, -1, 1); } } } if (whB == -1) { swap(A, B); swap(whA, whB); swap(A_path_beg, B_path_beg); swap(A_path_end, B_path_end); } if (whA == -1) { if (A_path_beg == -1) { return 0; } if (path_dir == -1) { int L = Left(B, whB, A_path_beg, A_path_end); debug(L); return L; } else { int R = Right(B, whB, A_path_end, A_path_beg); debug(R); return R; } } int rootA = whA, rootB = whB; if (A_path_end != -1) { rootA = A_path_end; } if (B_path_end != -1) { rootB = B_path_end; } debug(rootA, rootB); if (rootA > rootB) { swap(A, B); swap(whA, whB); swap(rootA, rootB); swap(A_path_beg, B_path_beg); swap(A_path_end, B_path_end); } //debug( int diff = rootB - rootA; debug(diff); if (A_path_end == -1 && B_path_end == -1) { // nie ma zadnych sciezek if (rootA == rootB) { return f(A, (*A)[whA].left, -1, -1, B, (*B)[whB].left, -1, -1, 0) + f(A, (*A)[whA].right, -1, -1, B, (*B)[whB].right, -1, -1, 0); } return diff + f(A, (*A)[whA].left, rootA, rootB - 1, B, (*B)[whB].left, -1, -1, -1) + f(A, (*A)[whA].right, -1, -1, B, (*B)[whB].right, rootB, rootA + 1, 1); } else { if (rootA == rootB) { if ((A_path_end == -1) != (path_dir == -1)) { swap(A, B); swap(whA, whB); swap(rootA, rootB); swap(A_path_beg, B_path_beg); swap(A_path_end, B_path_end); } } if (A_path_end == -1) { // jest sciezka w prawym assert(path_dir == -1); int e1 = Right(A, (*A)[whA].right, rootA + 1, rootB); int e2 = f(A, (*A)[whA].left, rootA, rootB, B, whB, B_path_beg, B_path_end, path_dir); debug(diff, e1, e2); return diff + e1 + e2; } else {// jest sciezka w lewym assert(B_path_end == -1); assert(path_dir == 1); return diff + Left(B, (*B)[whB].left, rootA, rootB - 1) + f(A, whA, A_path_beg, A_path_end, B, (*B)[whB].right, rootB, rootA, path_dir); } } } pair<Tree, int> ReadTree(int n) { vector<Node> nodes(n + 2); int root = -1; RE (i, n) { int par; cin>>par; if (par == -1) { root = i; } else { if (par < i) { nodes[par].right = i; } else { nodes[par].left = i; } } } return {Tree{nodes}, root}; } int32_t main() { ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); cin.tie(0); //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC; int n; cin>>n; pair<Tree, int> t1 = ReadTree(n); pair<Tree, int> t2 = ReadTree(n); debug(n); const int P = 1e9 + 7; cout<<f(&t1.st, t1.nd, -1, -1, &t2.st, t2.nd, -1, -1, 0) % P<<endl; return 0; } |