#pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; const int INF = 0x3f3f3f3f; #define FOR(i,b,e) for(int i = (b); i < (e); i++) #define TRAV(x,a) for(auto &x: (a)) #define SZ(x) ((int)x.size()) #define PB push_back #define X first #define Y second const int mod = 1e9+7; const int N = 5e5+5; struct vertex{ int leftInt, rightInt; int parent, leftSon, rightSon; bool increasing; }; int n, ans; vertex v[N]; int lewy[N], prawy[N]; vi indexy; bool inRange(int a, int b, int c){ return b <= a && a <= c; } void zmienSyna(int p, int oldRoot, int newRoot){ if(p == -1) return; if(v[p].leftSon == oldRoot){ v[p].leftSon = newRoot; return; } if(v[p].rightSon == oldRoot){ v[p].rightSon = newRoot; return; } } int rotuj(int root, bool syn, int cel){ int wRoocie = (v[root].increasing ? v[root].leftInt : v[root].rightInt); ans = (ans + abs(cel-wRoocie)) % mod; if(syn == 0){ int newRoot = v[root].leftSon; v[root].leftSon = -1; v[root].leftInt = cel+1; v[newRoot].rightInt = cel; v[root].increasing = 1; zmienSyna(v[root].parent, root, newRoot); v[newRoot].rightSon = root; v[newRoot].parent = v[root].parent; v[root].parent = newRoot; if(v[newRoot].leftInt != cel){ int newSon = indexy.back(); indexy.pop_back(); v[newSon] = v[newRoot]; v[newSon].rightInt = cel-1; v[newSon].parent = newRoot; v[newSon].rightSon = -1; v[newRoot].leftInt = cel; if(v[newRoot].leftSon != -1) v[v[newRoot].leftSon].parent = newSon; v[newRoot].leftSon = newSon; } return newRoot; } else{ int newRoot = v[root].rightSon; v[root].rightSon = -1; v[root].rightInt = cel-1; v[newRoot].leftInt = cel; v[root].increasing = 0; zmienSyna(v[root].parent, root, newRoot); v[newRoot].leftSon = root; v[newRoot].parent = v[root].parent; v[root].parent = newRoot; if(v[newRoot].rightInt != cel){ int newSon = indexy.back(); indexy.pop_back(); v[newSon] = v[newRoot]; v[newSon].leftInt = cel+1; v[newSon].parent = newRoot; v[newSon].leftSon = -1; v[newRoot].rightInt = cel; if(v[newRoot].rightSon != -1) v[v[newRoot].rightSon].parent = newSon; v[newRoot].rightSon = newSon; } return newRoot; } } void korzeniuj(int root, int cel, bool rozw); void wyprostuj(int root, int cel, bool inc){ if(!inc){ int aktParent = v[root].parent; int aktValue = v[aktParent].leftInt-1; korzeniuj(root, aktValue, 0); root = v[aktParent].leftSon; int lSon = v[root].leftSon; if(lSon != -1 && !v[lSon].increasing && v[lSon].rightInt+1 == v[root].leftInt){ indexy.PB(lSon); v[root].leftInt = v[lSon].leftInt; if(v[lSon].leftSon != -1) v[v[lSon].leftSon].parent = root; v[root].leftSon = v[lSon].leftSon; v[root].increasing = 0; } if(v[root].leftInt > cel){ wyprostuj(v[root].leftSon, cel, inc); lSon = v[root].leftSon; indexy.PB(lSon); v[root].leftInt = v[lSon].leftInt; if(v[lSon].leftSon != -1) v[v[lSon].leftSon].parent = root; v[root].leftSon = v[lSon].leftSon; v[root].increasing = 0; } } else{ int aktParent = v[root].parent; int aktValue = v[aktParent].rightInt+1; korzeniuj(root, aktValue, 0); root = v[aktParent].rightSon; int rSon = v[root].rightSon; if(rSon != -1 && v[rSon].increasing && v[rSon].leftInt-1 == v[root].rightInt){ indexy.PB(rSon); v[root].rightInt = v[rSon].rightInt; if(v[rSon].rightSon != -1) v[v[rSon].rightSon].parent = root; v[root].rightSon = v[rSon].rightSon; v[root].increasing = 1; } if(v[root].rightInt < cel){ wyprostuj(v[root].rightSon, cel, inc); rSon = v[root].rightSon; indexy.PB(rSon); v[root].rightInt = v[rSon].rightInt; if(v[rSon].rightSon != -1) v[v[rSon].rightSon].parent = root; v[root].rightSon = v[rSon].rightSon; v[root].increasing = 1; } } } void korzeniuj(int root, int cel, bool rozw){ if(!inRange(cel, v[root].leftInt, v[root].rightInt)){ int bottom = (v[root].increasing ? v[root].rightInt : v[root].leftInt); if(cel < bottom){ wyprostuj(v[root].leftSon, cel, 0); root = rotuj(root, 0, cel); } else{ wyprostuj(v[root].rightSon, cel, 1); root = rotuj(root, 1, cel); } } else if(!((cel == v[root].leftInt && v[root].increasing) || (cel == v[root].rightInt && !v[root].increasing))){ int fake = indexy.back(); indexy.pop_back(); v[fake] = v[root]; if(!v[root].increasing){ v[fake].leftSon = root; v[fake].rightSon = -1; } else{ v[fake].rightSon = root; v[fake].leftSon = -1; } zmienSyna(v[root].parent, root, fake); v[root].parent = fake; root = rotuj(fake, v[root].increasing, cel); } if(v[root].leftInt != cel){ int newSon = indexy.back(); indexy.pop_back(); v[newSon] = v[root]; v[newSon].rightInt = cel-1; v[newSon].parent = root; v[root].leftInt = cel; if(v[root].leftSon != -1) v[v[root].leftSon].parent = newSon; v[root].leftSon = newSon; } if(v[root].rightInt != cel){ int newSon = indexy.back(); indexy.pop_back(); v[newSon] = v[root]; v[newSon].leftInt = cel+1; v[newSon].parent = root; v[root].rightInt = cel; if(v[root].rightSon != -1) v[v[root].rightSon].parent = newSon; v[root].rightSon = newSon; } if(rozw && lewy[cel]){ korzeniuj(v[root].leftSon, lewy[cel], 1); } if(rozw && prawy[cel]){ korzeniuj(v[root].rightSon, prawy[cel], 1); } } void readInt(int &x){ x = 0; char c = getchar(); bool negative = 0; while(c < 33) c = getchar(); if(c == '-') negative = 1, c = getchar(); while('0' <= c && c <= '9') x = 10*x + c - '0', c = getchar(); if(negative) x *= -1; } void solve(){ int root1 = 0, root2 = 0; readInt(n); FOR(i, 1, n+1) v[i].leftSon = v[i].rightSon = -1; FOR(i, 1, n+1){ int a; readInt(a); v[i].leftInt = v[i].rightInt = i; v[i].parent = a; v[i].increasing = 0; if(a == -1) root1 = i; else if(a < i) v[a].rightSon = i; else v[a].leftSon = i; } FOR(i, 1, n+1){ int a; readInt(a); if(a == -1) root2 = i; else if(a < i) prawy[a] = i; else lewy[a] = i; } korzeniuj(root1, root2, 1); cout << ans << '\n'; } int main(){ // ios::sync_with_stdio(0); // cin.tie(0); // int tt; cin >> tt; // FOR(te, 0, tt) solve(); 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 | #pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; const int INF = 0x3f3f3f3f; #define FOR(i,b,e) for(int i = (b); i < (e); i++) #define TRAV(x,a) for(auto &x: (a)) #define SZ(x) ((int)x.size()) #define PB push_back #define X first #define Y second const int mod = 1e9+7; const int N = 5e5+5; struct vertex{ int leftInt, rightInt; int parent, leftSon, rightSon; bool increasing; }; int n, ans; vertex v[N]; int lewy[N], prawy[N]; vi indexy; bool inRange(int a, int b, int c){ return b <= a && a <= c; } void zmienSyna(int p, int oldRoot, int newRoot){ if(p == -1) return; if(v[p].leftSon == oldRoot){ v[p].leftSon = newRoot; return; } if(v[p].rightSon == oldRoot){ v[p].rightSon = newRoot; return; } } int rotuj(int root, bool syn, int cel){ int wRoocie = (v[root].increasing ? v[root].leftInt : v[root].rightInt); ans = (ans + abs(cel-wRoocie)) % mod; if(syn == 0){ int newRoot = v[root].leftSon; v[root].leftSon = -1; v[root].leftInt = cel+1; v[newRoot].rightInt = cel; v[root].increasing = 1; zmienSyna(v[root].parent, root, newRoot); v[newRoot].rightSon = root; v[newRoot].parent = v[root].parent; v[root].parent = newRoot; if(v[newRoot].leftInt != cel){ int newSon = indexy.back(); indexy.pop_back(); v[newSon] = v[newRoot]; v[newSon].rightInt = cel-1; v[newSon].parent = newRoot; v[newSon].rightSon = -1; v[newRoot].leftInt = cel; if(v[newRoot].leftSon != -1) v[v[newRoot].leftSon].parent = newSon; v[newRoot].leftSon = newSon; } return newRoot; } else{ int newRoot = v[root].rightSon; v[root].rightSon = -1; v[root].rightInt = cel-1; v[newRoot].leftInt = cel; v[root].increasing = 0; zmienSyna(v[root].parent, root, newRoot); v[newRoot].leftSon = root; v[newRoot].parent = v[root].parent; v[root].parent = newRoot; if(v[newRoot].rightInt != cel){ int newSon = indexy.back(); indexy.pop_back(); v[newSon] = v[newRoot]; v[newSon].leftInt = cel+1; v[newSon].parent = newRoot; v[newSon].leftSon = -1; v[newRoot].rightInt = cel; if(v[newRoot].rightSon != -1) v[v[newRoot].rightSon].parent = newSon; v[newRoot].rightSon = newSon; } return newRoot; } } void korzeniuj(int root, int cel, bool rozw); void wyprostuj(int root, int cel, bool inc){ if(!inc){ int aktParent = v[root].parent; int aktValue = v[aktParent].leftInt-1; korzeniuj(root, aktValue, 0); root = v[aktParent].leftSon; int lSon = v[root].leftSon; if(lSon != -1 && !v[lSon].increasing && v[lSon].rightInt+1 == v[root].leftInt){ indexy.PB(lSon); v[root].leftInt = v[lSon].leftInt; if(v[lSon].leftSon != -1) v[v[lSon].leftSon].parent = root; v[root].leftSon = v[lSon].leftSon; v[root].increasing = 0; } if(v[root].leftInt > cel){ wyprostuj(v[root].leftSon, cel, inc); lSon = v[root].leftSon; indexy.PB(lSon); v[root].leftInt = v[lSon].leftInt; if(v[lSon].leftSon != -1) v[v[lSon].leftSon].parent = root; v[root].leftSon = v[lSon].leftSon; v[root].increasing = 0; } } else{ int aktParent = v[root].parent; int aktValue = v[aktParent].rightInt+1; korzeniuj(root, aktValue, 0); root = v[aktParent].rightSon; int rSon = v[root].rightSon; if(rSon != -1 && v[rSon].increasing && v[rSon].leftInt-1 == v[root].rightInt){ indexy.PB(rSon); v[root].rightInt = v[rSon].rightInt; if(v[rSon].rightSon != -1) v[v[rSon].rightSon].parent = root; v[root].rightSon = v[rSon].rightSon; v[root].increasing = 1; } if(v[root].rightInt < cel){ wyprostuj(v[root].rightSon, cel, inc); rSon = v[root].rightSon; indexy.PB(rSon); v[root].rightInt = v[rSon].rightInt; if(v[rSon].rightSon != -1) v[v[rSon].rightSon].parent = root; v[root].rightSon = v[rSon].rightSon; v[root].increasing = 1; } } } void korzeniuj(int root, int cel, bool rozw){ if(!inRange(cel, v[root].leftInt, v[root].rightInt)){ int bottom = (v[root].increasing ? v[root].rightInt : v[root].leftInt); if(cel < bottom){ wyprostuj(v[root].leftSon, cel, 0); root = rotuj(root, 0, cel); } else{ wyprostuj(v[root].rightSon, cel, 1); root = rotuj(root, 1, cel); } } else if(!((cel == v[root].leftInt && v[root].increasing) || (cel == v[root].rightInt && !v[root].increasing))){ int fake = indexy.back(); indexy.pop_back(); v[fake] = v[root]; if(!v[root].increasing){ v[fake].leftSon = root; v[fake].rightSon = -1; } else{ v[fake].rightSon = root; v[fake].leftSon = -1; } zmienSyna(v[root].parent, root, fake); v[root].parent = fake; root = rotuj(fake, v[root].increasing, cel); } if(v[root].leftInt != cel){ int newSon = indexy.back(); indexy.pop_back(); v[newSon] = v[root]; v[newSon].rightInt = cel-1; v[newSon].parent = root; v[root].leftInt = cel; if(v[root].leftSon != -1) v[v[root].leftSon].parent = newSon; v[root].leftSon = newSon; } if(v[root].rightInt != cel){ int newSon = indexy.back(); indexy.pop_back(); v[newSon] = v[root]; v[newSon].leftInt = cel+1; v[newSon].parent = root; v[root].rightInt = cel; if(v[root].rightSon != -1) v[v[root].rightSon].parent = newSon; v[root].rightSon = newSon; } if(rozw && lewy[cel]){ korzeniuj(v[root].leftSon, lewy[cel], 1); } if(rozw && prawy[cel]){ korzeniuj(v[root].rightSon, prawy[cel], 1); } } void readInt(int &x){ x = 0; char c = getchar(); bool negative = 0; while(c < 33) c = getchar(); if(c == '-') negative = 1, c = getchar(); while('0' <= c && c <= '9') x = 10*x + c - '0', c = getchar(); if(negative) x *= -1; } void solve(){ int root1 = 0, root2 = 0; readInt(n); FOR(i, 1, n+1) v[i].leftSon = v[i].rightSon = -1; FOR(i, 1, n+1){ int a; readInt(a); v[i].leftInt = v[i].rightInt = i; v[i].parent = a; v[i].increasing = 0; if(a == -1) root1 = i; else if(a < i) v[a].rightSon = i; else v[a].leftSon = i; } FOR(i, 1, n+1){ int a; readInt(a); if(a == -1) root2 = i; else if(a < i) prawy[a] = i; else lewy[a] = i; } korzeniuj(root1, root2, 1); cout << ans << '\n'; } int main(){ // ios::sync_with_stdio(0); // cin.tie(0); // int tt; cin >> tt; // FOR(te, 0, tt) solve(); return 0; } |