#pragma GCC optimize("Ofast") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define mp make_pair #define eb emplace_back #define pb push_back #define e1 first #define e2 second #define uint unsigned int #define ll long long #define ull unsigned long long #define ld long double #define float long double #define size(x) (int)x.size() #define satori int testCases; cin>>testCases; while(testCases--) #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define all(r) begin(r),end(r) #define time chrono::high_resolution_clock().now().time_since_epoch().count() typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; mt19937 rng(chrono::high_resolution_clock().now().time_since_epoch().count()); /////////////////// #define debug if(false) /////////////////// const int MAXN=5e5+10,inf=1e9+2137; const ll mod=1e9+7; int ogson[2][MAXN],anson[2][MAXN]; ll result=0; int interval_count=0; vector<int> interval_root={0}; vector<int> interval_par={0}; vector<int> interval_lson={0}; vector<int> interval_rson={0}; vector<int> interval_lbound={0}; vector<int> interval_rbound={0}; set<pair<int,int>> left_ends; int my_interval(int x){ auto xd=left_ends.upper_bound(mp(x,inf)); xd--; return (*xd).e2; } void union_subtree(int interval_id){ int par_id=interval_par[interval_id]; if(interval_root[interval_id]<interval_root[par_id]){ if(interval_rson[interval_id]) union_subtree(interval_rson[interval_id]); if(interval_lson[interval_id]) union_subtree(interval_lson[interval_id]); result+=abs(interval_root[interval_id]-interval_rbound[interval_id]); left_ends.erase(mp(interval_lbound[par_id],par_id)); left_ends.erase(mp(interval_lbound[interval_id],interval_id)); interval_lbound[par_id]=interval_lbound[interval_id]; interval_lson[par_id]=interval_lson[interval_id]; if(interval_lson[par_id]) interval_par[interval_lson[interval_id]]=par_id; left_ends.insert(mp(interval_lbound[par_id],par_id)); } else{ if(interval_rson[interval_id]) union_subtree(interval_rson[interval_id]); if(interval_lson[interval_id]) union_subtree(interval_lson[interval_id]); result+=abs(interval_root[interval_id]-interval_lbound[interval_id]); left_ends.erase(mp(interval_lbound[interval_id],interval_id)); interval_rson[par_id]=interval_rson[interval_id]; if(interval_rson[par_id]) interval_par[interval_rson[interval_id]]=par_id; interval_rbound[par_id]=interval_rbound[interval_id]; } } void union_up(int interval_id){ int par_id=interval_par[interval_id]; if(interval_root[interval_id]<interval_root[par_id]){ if(interval_rson[interval_id]) union_subtree(interval_rson[interval_id]); result+=abs(interval_root[interval_id]-interval_rbound[interval_id]); left_ends.erase(mp(interval_lbound[par_id],par_id)); left_ends.erase(mp(interval_lbound[interval_id],interval_id)); interval_lbound[par_id]=interval_lbound[interval_id]; interval_lson[par_id]=interval_lson[interval_id]; if(interval_lson[par_id]) interval_par[interval_lson[interval_id]]=par_id; left_ends.insert(mp(interval_lbound[par_id],par_id)); } else{ if(interval_lson[interval_id]) union_subtree(interval_lson[interval_id]); result+=abs(interval_root[interval_id]-interval_lbound[interval_id]); left_ends.erase(mp(interval_lbound[interval_id],interval_id)); interval_rson[par_id]=interval_rson[interval_id]; if(interval_rson[par_id]) interval_par[interval_rson[interval_id]]=par_id; interval_rbound[par_id]=interval_rbound[interval_id]; } } void solve(int anroot){ int interval_id=-1,par_id=-1; while(true){ interval_id=my_interval(anroot); par_id=interval_par[interval_id]; if(!par_id) break; union_up(interval_id); } result+=abs(interval_root[interval_id]-anroot); interval_root[interval_id]=anroot; left_ends.erase(mp(interval_lbound[interval_id],interval_id)); if(interval_lbound[interval_id]==anroot){ if(interval_lson[interval_id]){ interval_par[interval_lson[interval_id]]=0; if(anson[0][anroot]) solve(anson[0][anroot]); } } else{ interval_count++; left_ends.insert(mp(interval_lbound[interval_id],interval_count)); interval_root.eb(anroot-1); interval_par.eb(0); interval_rson.eb(0); interval_lbound.eb(interval_lbound[interval_id]); interval_rbound.eb(anroot-1); if(interval_lson[interval_id]) interval_lson.eb(interval_lson[interval_id]),interval_par[interval_lson[interval_id]]=interval_count; else interval_lson.eb(0); if(anson[0][anroot]) solve(anson[0][anroot]); } if(interval_rbound[interval_id]==anroot){ if(interval_rson[interval_id]){ interval_par[interval_rson[interval_id]]=0; if(anson[1][anroot]) solve(anson[1][anroot]); } } else{ interval_count++; left_ends.insert(mp(anroot+1,interval_count)); interval_root.eb(anroot+1); interval_par.eb(0); interval_lson.eb(0); interval_lbound.eb(anroot+1); interval_rbound.eb(interval_rbound[interval_id]); if(interval_rson[interval_id]) interval_rson.eb(interval_rson[interval_id]),interval_par[interval_rson[interval_id]]=interval_count; else interval_rson.eb(0); if(anson[1][anroot]) solve(anson[1][anroot]); } } void make_intervals(int root_par,int root){ int left_bound=root,right_bound=root; while(ogson[0][left_bound]&&!ogson[1][ogson[0][left_bound]]) left_bound=ogson[0][left_bound]; while(ogson[1][right_bound]&&!ogson[0][ogson[1][right_bound]]) right_bound=ogson[1][right_bound]; interval_count++; interval_root.eb(root); interval_par.eb(root_par); left_ends.insert(mp(left_bound,interval_count)); interval_lson.eb(0); interval_rson.eb(0); interval_lbound.eb(left_bound); interval_rbound.eb(right_bound); if(root<interval_root[root_par]) interval_lson[root_par]=interval_count; else interval_rson[root_par]=interval_count; int interval_num=interval_count; if(ogson[0][left_bound]) make_intervals(interval_num,ogson[0][left_bound]); if(ogson[1][right_bound]) make_intervals(interval_num,ogson[1][right_bound]); } int32_t main() { fastio; int n,par,ogroot=-1,anroot=-1; cin>>n; for(int i=1;i<=n;i++){ cin>>par; if(par==-1){ ogroot=i; continue; } if(par>i) ogson[0][par]=i; else ogson[1][par]=i; } for(int i=1;i<=n;i++){ cin>>par; if(par==-1){ anroot=i; continue; } if(par>i) anson[0][par]=i; else anson[1][par]=i; } make_intervals(0,ogroot); solve(anroot); result%=mod; cout<<result<<'\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 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 | #pragma GCC optimize("Ofast") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define mp make_pair #define eb emplace_back #define pb push_back #define e1 first #define e2 second #define uint unsigned int #define ll long long #define ull unsigned long long #define ld long double #define float long double #define size(x) (int)x.size() #define satori int testCases; cin>>testCases; while(testCases--) #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define all(r) begin(r),end(r) #define time chrono::high_resolution_clock().now().time_since_epoch().count() typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; mt19937 rng(chrono::high_resolution_clock().now().time_since_epoch().count()); /////////////////// #define debug if(false) /////////////////// const int MAXN=5e5+10,inf=1e9+2137; const ll mod=1e9+7; int ogson[2][MAXN],anson[2][MAXN]; ll result=0; int interval_count=0; vector<int> interval_root={0}; vector<int> interval_par={0}; vector<int> interval_lson={0}; vector<int> interval_rson={0}; vector<int> interval_lbound={0}; vector<int> interval_rbound={0}; set<pair<int,int>> left_ends; int my_interval(int x){ auto xd=left_ends.upper_bound(mp(x,inf)); xd--; return (*xd).e2; } void union_subtree(int interval_id){ int par_id=interval_par[interval_id]; if(interval_root[interval_id]<interval_root[par_id]){ if(interval_rson[interval_id]) union_subtree(interval_rson[interval_id]); if(interval_lson[interval_id]) union_subtree(interval_lson[interval_id]); result+=abs(interval_root[interval_id]-interval_rbound[interval_id]); left_ends.erase(mp(interval_lbound[par_id],par_id)); left_ends.erase(mp(interval_lbound[interval_id],interval_id)); interval_lbound[par_id]=interval_lbound[interval_id]; interval_lson[par_id]=interval_lson[interval_id]; if(interval_lson[par_id]) interval_par[interval_lson[interval_id]]=par_id; left_ends.insert(mp(interval_lbound[par_id],par_id)); } else{ if(interval_rson[interval_id]) union_subtree(interval_rson[interval_id]); if(interval_lson[interval_id]) union_subtree(interval_lson[interval_id]); result+=abs(interval_root[interval_id]-interval_lbound[interval_id]); left_ends.erase(mp(interval_lbound[interval_id],interval_id)); interval_rson[par_id]=interval_rson[interval_id]; if(interval_rson[par_id]) interval_par[interval_rson[interval_id]]=par_id; interval_rbound[par_id]=interval_rbound[interval_id]; } } void union_up(int interval_id){ int par_id=interval_par[interval_id]; if(interval_root[interval_id]<interval_root[par_id]){ if(interval_rson[interval_id]) union_subtree(interval_rson[interval_id]); result+=abs(interval_root[interval_id]-interval_rbound[interval_id]); left_ends.erase(mp(interval_lbound[par_id],par_id)); left_ends.erase(mp(interval_lbound[interval_id],interval_id)); interval_lbound[par_id]=interval_lbound[interval_id]; interval_lson[par_id]=interval_lson[interval_id]; if(interval_lson[par_id]) interval_par[interval_lson[interval_id]]=par_id; left_ends.insert(mp(interval_lbound[par_id],par_id)); } else{ if(interval_lson[interval_id]) union_subtree(interval_lson[interval_id]); result+=abs(interval_root[interval_id]-interval_lbound[interval_id]); left_ends.erase(mp(interval_lbound[interval_id],interval_id)); interval_rson[par_id]=interval_rson[interval_id]; if(interval_rson[par_id]) interval_par[interval_rson[interval_id]]=par_id; interval_rbound[par_id]=interval_rbound[interval_id]; } } void solve(int anroot){ int interval_id=-1,par_id=-1; while(true){ interval_id=my_interval(anroot); par_id=interval_par[interval_id]; if(!par_id) break; union_up(interval_id); } result+=abs(interval_root[interval_id]-anroot); interval_root[interval_id]=anroot; left_ends.erase(mp(interval_lbound[interval_id],interval_id)); if(interval_lbound[interval_id]==anroot){ if(interval_lson[interval_id]){ interval_par[interval_lson[interval_id]]=0; if(anson[0][anroot]) solve(anson[0][anroot]); } } else{ interval_count++; left_ends.insert(mp(interval_lbound[interval_id],interval_count)); interval_root.eb(anroot-1); interval_par.eb(0); interval_rson.eb(0); interval_lbound.eb(interval_lbound[interval_id]); interval_rbound.eb(anroot-1); if(interval_lson[interval_id]) interval_lson.eb(interval_lson[interval_id]),interval_par[interval_lson[interval_id]]=interval_count; else interval_lson.eb(0); if(anson[0][anroot]) solve(anson[0][anroot]); } if(interval_rbound[interval_id]==anroot){ if(interval_rson[interval_id]){ interval_par[interval_rson[interval_id]]=0; if(anson[1][anroot]) solve(anson[1][anroot]); } } else{ interval_count++; left_ends.insert(mp(anroot+1,interval_count)); interval_root.eb(anroot+1); interval_par.eb(0); interval_lson.eb(0); interval_lbound.eb(anroot+1); interval_rbound.eb(interval_rbound[interval_id]); if(interval_rson[interval_id]) interval_rson.eb(interval_rson[interval_id]),interval_par[interval_rson[interval_id]]=interval_count; else interval_rson.eb(0); if(anson[1][anroot]) solve(anson[1][anroot]); } } void make_intervals(int root_par,int root){ int left_bound=root,right_bound=root; while(ogson[0][left_bound]&&!ogson[1][ogson[0][left_bound]]) left_bound=ogson[0][left_bound]; while(ogson[1][right_bound]&&!ogson[0][ogson[1][right_bound]]) right_bound=ogson[1][right_bound]; interval_count++; interval_root.eb(root); interval_par.eb(root_par); left_ends.insert(mp(left_bound,interval_count)); interval_lson.eb(0); interval_rson.eb(0); interval_lbound.eb(left_bound); interval_rbound.eb(right_bound); if(root<interval_root[root_par]) interval_lson[root_par]=interval_count; else interval_rson[root_par]=interval_count; int interval_num=interval_count; if(ogson[0][left_bound]) make_intervals(interval_num,ogson[0][left_bound]); if(ogson[1][right_bound]) make_intervals(interval_num,ogson[1][right_bound]); } int32_t main() { fastio; int n,par,ogroot=-1,anroot=-1; cin>>n; for(int i=1;i<=n;i++){ cin>>par; if(par==-1){ ogroot=i; continue; } if(par>i) ogson[0][par]=i; else ogson[1][par]=i; } for(int i=1;i<=n;i++){ cin>>par; if(par==-1){ anroot=i; continue; } if(par>i) anson[0][par]=i; else anson[1][par]=i; } make_intervals(0,ogroot); solve(anroot); result%=mod; cout<<result<<'\n'; } |