#include #include #include #include #include using namespace std; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; #define rep(i,n) for(int i=0; i<(n); i++) const int maxN = 100000; namespace RQ{ struct S { int Amax, Bmax, q, i; }; S op(S l, S r){ S res = { max(l.Amax,r.Amax), max(l.Bmax,r.Bmax), 0,0 }; res.q = max(l.q,r.Bmax); res.i = l.i; if(res.q > max(l.Amax,r.q)){ res.q = max(l.Amax,r.q); res.i = r.i; } return res; } S e(){ return { 0,0,maxN*2,0 }; } using RQ = atcoder::segtree; RQ createRQ1(int N, const vector& A, const vector& B){ vector tmp(2*N-1); rep(i,N-1) tmp[2*i+1].Amax = max(A[i],A[i+1]); rep(i,N-1) tmp[2*i+1].Bmax = max(B[i],B[i+1]); rep(i,N-1) tmp[2*i+1].q = max(tmp[2*i+1].Amax, tmp[2*i+1].Bmax); rep(i,2*N-1) tmp[i].i = i/2; auto t = op(tmp[4],tmp[5]); return RQ(tmp); } RQ createRQ2(int N, const vector& A, const vector& B){ vector tmp(2*N-1); rep(i,N-1) tmp[2*i+1].Bmax = max(A[i],A[i+1]); rep(i,N-1) tmp[2*i+1].Amax = max(B[i],B[i+1]); rep(i,N-1) tmp[2*i+1].q = max(tmp[2*i+1].Amax, tmp[2*i+1].Bmax); rep(i,2*N-1) tmp[i].i = i/2; return RQ(tmp); } } struct CompNode{ int pos; int type; bool operator<(const CompNode& r) const { if(pos != r.pos) return pos < r.pos; return type < r.type; } }; struct ValNode{ int w; int cnt; }; int main(){ int N; cin >> N; vector A(N); rep(i,N){ cin >> A[i]; A[i]--; } vector B(N); rep(i,N){ cin >> B[i]; B[i]--; } vector Z(N); rep(i,N){ cin >> Z[i]; Z[i]--; } vector invA(N); rep(i,N) invA[A[i]] = i; vector invB(N); rep(i,N) invB[B[i]] = i; map G; rep(i,N+1) G[{i,0}] = {0,0}; rep(i,N+1) G[{i,1}] = {0,0}; for(int i=1; i<=N; i++) G[{i,0}].w += 1; auto rq1 = RQ::createRQ1(N,A,B); auto rq2 = RQ::createRQ2(N,A,B); vector> toAddCnt(N); rep(i,N){ int za = invA[i], zb = invB[Z[i]]; RQ::S q; if(za <= zb) q = rq1.prod(za*2, zb*2+1); else q = rq2.prod(zb*2, za*2+1); toAddCnt[q.q].push_back(q.i); } vector> toEraseSep(N); rep(i,N-1) toEraseSep[max(A[i],A[i+1])].push_back({i+1,0}); rep(i,N-1) toEraseSep[max(B[i],B[i+1])].push_back({i+1,1}); int ans = 0; rep(i,N){ for(auto s : toEraseSep[i]){ auto itr = G.find(s); auto nxitr = itr; nxitr++; ans -= min(itr->second.cnt, itr->second.w); ans -= min(nxitr->second.cnt, nxitr->second.w); nxitr->second.cnt += itr->second.cnt; nxitr->second.w += itr->second.w; ans += min(nxitr->second.cnt, nxitr->second.w); G.erase(itr); } for(auto s : toAddCnt[i]){ auto itr = G.upper_bound({s,2}); ans -= min(itr->second.cnt, itr->second.w); itr->second.cnt += 1; ans += min(itr->second.cnt, itr->second.w); } cout << ans << endl; } return 0; } struct ios_do_not_sync { ios_do_not_sync() { ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;