#include #include #include using namespace atcoder; using mint = modint998244353; using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf 1000000001 int main(){ int N; cin>>N; vector a(N),b(N),z(N); vector posa(N),posb(N); rep(i,N){ cin>>a[i]; a[i]--; posa[a[i]] = i; } rep(i,N){ cin>>b[i]; b[i]--; posb[b[i]] = i; } vector rz(N); rep(i,N){ cin>>z[i]; z[i]--; rz[z[i]] = i; } vector ok(N,N),ng(N,-1); vector> qs(N); vector> Add(N),Del(N); vector> Ps; { vector pa(N),pb(N); rep(i,N){ pa[i] = i; pb[i] = i; } vector> inda(N),indb(N); rep(i,N){ inda[i].push_back(i); indb[i].push_back(i); } rep(i,N){ Ps.emplace_back(posa[i],posb[z[i]]); } vector>> AP(N),DP(N); rep(i,N){ int A = posa[i]; if(A!=0 && a[A]>a[A-1]){ int x = pa[A],y = pa[A-1]; if(inda[x].size()>inda[y].size())swap(x,y); while(inda[x].size()>0){ int ba = inda[x].back(); inda[x].pop_back(); int v = a[ba]; DP[i].emplace_back(pa[posa[v]],pb[posb[z[v]]]); Ps.emplace_back(pa[posa[v]],pb[posb[z[v]]]); pa[ba] = y; inda[y].push_back(ba); AP[i].emplace_back(pa[posa[v]],pb[posb[z[v]]]); Ps.emplace_back(pa[posa[v]],pb[posb[z[v]]]); } } if(A!=N-1 && a[A]>a[A+1]){ int x = pa[A],y = pa[A+1]; if(inda[x].size()>inda[y].size())swap(x,y); while(inda[x].size()>0){ int ba = inda[x].back(); inda[x].pop_back(); int v = a[ba]; DP[i].emplace_back(pa[posa[v]],pb[posb[z[v]]]); Ps.emplace_back(pa[posa[v]],pb[posb[z[v]]]); pa[ba] = y; inda[y].push_back(ba); AP[i].emplace_back(pa[posa[v]],pb[posb[z[v]]]); Ps.emplace_back(pa[posa[v]],pb[posb[z[v]]]); } } A = posb[i]; if(A!=0 && b[A]>b[A-1]){ int x = pb[A],y = pb[A-1]; if(indb[x].size()>indb[y].size())swap(x,y); while(indb[x].size()>0){ int ba = indb[x].back(); indb[x].pop_back(); int v = b[ba]; DP[i].emplace_back(pa[posa[rz[v]]],pb[posb[v]]); Ps.emplace_back(pa[posa[rz[v]]],pb[posb[v]]); pb[ba] = y; indb[y].push_back(ba); AP[i].emplace_back(pa[posa[rz[v]]],pb[posb[v]]); Ps.emplace_back(pa[posa[rz[v]]],pb[posb[v]]); } } if(A!=N-1 && b[A]>b[A+1]){ int x = pb[A],y = pb[A+1]; if(indb[x].size()>indb[y].size())swap(x,y); while(indb[x].size()>0){ int ba = indb[x].back(); indb[x].pop_back(); int v = b[ba]; DP[i].emplace_back(pa[posa[rz[v]]],pb[posb[v]]); Ps.emplace_back(pa[posa[rz[v]]],pb[posb[v]]); pb[ba] = y; indb[y].push_back(ba); AP[i].emplace_back(pa[posa[rz[v]]],pb[posb[v]]); Ps.emplace_back(pa[posa[rz[v]]],pb[posb[v]]); } } } sort(Ps.begin(),Ps.end()); Ps.erase(unique(Ps.begin(),Ps.end()),Ps.end()); rep(i,N){ rep(j,AP[i].size()){ Add[i].push_back(distance(Ps.begin(),lower_bound(Ps.begin(),Ps.end(),AP[i][j]))); } rep(j,DP[i].size()){ Del[i].push_back(distance(Ps.begin(),lower_bound(Ps.begin(),Ps.end(),DP[i][j]))); } } } rep(_,18){ qs.assign(N,vector()); rep(i,N){ if(ok[i]-ng[i]==1)continue; qs[(ok[i]+ng[i])/2].push_back(i); } vector pa(N),pb(N); rep(i,N){ pa[i] = i; pb[i] = i; } vector> inda(N),indb(N); rep(i,N){ inda[i].push_back(i); indb[i].push_back(i); } vector cnt(Ps.size(),0); rep(i,N){ cnt[distance(Ps.begin(),lower_bound(Ps.begin(),Ps.end(),make_pair(posa[i],posb[z[i]])))]++; } rep(i,N){ rep(j,qs[i].size()){ int q = qs[i][j]; int X = pa[q],Y = pb[q]; int d = distance(Ps.begin(),lower_bound(Ps.begin(),Ps.end(),make_pair(X,Y))); if(d==Ps.size()||Ps[d]!=make_pair(X,Y)||cnt[d]==0){ ng[q] = i; } else{ ok[q] = i; } } int A = posa[i]; if(A!=0 && a[A]>a[A-1]){ int x = pa[A],y = pa[A-1]; if(inda[x].size()>inda[y].size())swap(x,y); while(inda[x].size()>0){ int ba = inda[x].back(); inda[x].pop_back(); pa[ba] = y; inda[y].push_back(ba); } } if(A!=N-1 && a[A]>a[A+1]){ int x = pa[A],y = pa[A+1]; if(inda[x].size()>inda[y].size())swap(x,y); while(inda[x].size()>0){ int ba = inda[x].back(); inda[x].pop_back(); pa[ba] = y; inda[y].push_back(ba); } } A = posb[i]; if(A!=0 && b[A]>b[A-1]){ int x = pb[A],y = pb[A-1]; if(indb[x].size()>indb[y].size())swap(x,y); while(indb[x].size()>0){ int ba = indb[x].back(); indb[x].pop_back(); pb[ba] = y; indb[y].push_back(ba); } } if(A!=N-1 && b[A]>b[A+1]){ int x = pb[A],y = pb[A+1]; if(indb[x].size()>indb[y].size())swap(x,y); while(indb[x].size()>0){ int ba = indb[x].back(); indb[x].pop_back(); pb[ba] = y; indb[y].push_back(ba); } } rep(j,Add[i].size())cnt[Add[i][j]]++; rep(j,Del[i].size())cnt[Del[i][j]]--; } } vector ans(N+1,0); rep(i,N){ //cout<