#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace atcoder; #define rep(i,n) for (int i=0;i using vec = vector; template using vvec = vec>; template using vvvec = vec>; using ll = long long; using pii = pair; using pll = pair; template bool chmin(T &a, T b){ if (a>b){ a = b; return true; } return false; } template bool chmax(T &a, T b){ if (a T sum(vec x){ T res=0; for (auto e:x){ res += e; } return res; } template void printv(vec x){ for (auto e:x){ cout< ostream& operator<<(ostream& os, const vec& A){ os << "["; rep(i,A.size()){ os << A[i]; if (i!=A.size()-1){ os << ", "; } } os << "]" ; return os; } template ostream& operator<<(ostream& os, const deque& A){ os << "deque{["; rep(i,A.size()){ os << A[i]; if (i!=A.size()-1){ os << ", "; } } os << "]}" ; return os; } template ostream& operator<<(ostream& os, const pair& A){ os << "("; os << A.first ; os << ", "; os << A.second; os << ")"; return os; } struct S{ int val,cnt; }; S op(S a,S b){ if (a.val < b.val){ return a; } if (b.val < a.val){ return b; } return {a.val,a.cnt+b.cnt}; } S e(){ return {(int)1e9,0}; } using F = int; S mapping(F f,S x){ return {x.val+f,x.cnt}; } F composition(F f,F g){ return f+g; } F id(){ return 0; } int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); int N; cin>>N; vec A(N),B(N); rep(i,N){cin>>A[i];} rep(i,N){cin>>B[i];} int M = 4e5; vec first_A(M+1,N); vec first_B(M+1,N); vec init(N,{0,1}); lazy_segtree seg(init); ll res = 0; int l,r; for (int i=N-1;0<=i;i--){ l = min(first_A[A[i]],first_B[A[i]]); r = max(first_A[A[i]],first_B[A[i]]); seg.apply(l,r,-1); first_A[A[i]] = i; l = min(first_A[A[i]],first_B[A[i]]); r = max(first_A[A[i]],first_B[A[i]]); seg.apply(l,r,1); l = min(first_A[B[i]],first_B[B[i]]); r = max(first_A[B[i]],first_B[B[i]]); seg.apply(l,r,-1); first_B[B[i]] = i; l = min(first_A[B[i]],first_B[B[i]]); r = max(first_A[B[i]],first_B[B[i]]); seg.apply(l,r,1); S tmp = seg.prod(i,N); //cout << tmp.val << " " << tmp.cnt << endl; if (tmp.val==0){ res += (ll)tmp.cnt; } } cout << res << endl; }