#include using namespace std; typedef signed long long ll; #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define FORR2(x,y,arr) for(auto& [x,y]:arr) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) template bool chmax(T &a, const T &b) { if(a bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;} //------------------------------------------------------- int N; int A[202020]; int B[202020]; int PA[404040]; int PB[404040]; static ll const def=0; template class SegTree_3 { public: vector val; vector> ma; SegTree_3(){ int i; val.resize(NV*2,0); ma.resize(NV*2); FOR(i,NV) ma[i+NV]={0,1}; for(i=NV-1;i>=1;i--) ma[i]={0,ma[2*i].second+ma[2*i+1].second}; }; pair getval(int x,int y,int l=0,int r=NV,int k=1) { if(r<=x || y<=l || y<=x) return {1<<20,0}; if(x<=l && r<=y) return ma[k]; auto a=getval(x,y,l,(l+r)/2,k*2); auto b=getval(x,y,(l+r)/2,r,k*2+1); if(a.first>b.first) swap(a,b); if(a.first==b.first) a.second+=b.second; a.first+=val[k]; return a; } pair update(int x,int y, V v,int l=0,int r=NV,int k=1) { if(l>=r||y<=x) return {1<<20,0}; if(x<=l && r<=y) { val[k]+=v; ma[k].first+=v; } else if(l < y && x < r) { auto a=update(x,y,v,l,(l+r)/2,k*2); auto b=update(x,y,v,(l+r)/2,r,k*2+1); if(a.first>b.first) swap(a,b); if(a.first==b.first) a.second+=b.second; a.first+=val[k]; ma[k]=a; } return ma[k]; } }; SegTree_3 st; void solve() { int i,j,k,l,r,x,y; string s; cin>>N; FOR(i,N) cin>>A[i+1]; FOR(i,N) cin>>B[i+1]; ll ret=0; for(i=1;i<=N;i++) { if(A[i]==B[i]) { x=min(PA[A[i]],PB[B[i]]); y=max(PA[A[i]],PB[B[i]]); st.update(x,y,-1); PA[A[i]]=i; PB[B[i]]=i; } else { x=min(PA[A[i]],PB[A[i]]); y=max(PA[A[i]],PB[A[i]]); st.update(x,y,-1); x=min(PA[B[i]],PB[B[i]]); y=max(PA[B[i]],PB[B[i]]); st.update(x,y,-1); PA[A[i]]=i; PB[B[i]]=i; x=min(PA[A[i]],PB[A[i]]); y=max(PA[A[i]],PB[A[i]]); st.update(x,y,1); x=min(PA[B[i]],PB[B[i]]); y=max(PA[B[i]],PB[B[i]]); st.update(x,y,1); } auto a=st.getval(0,i); if(a.first==0) ret+=a.second; } cout<