#include using namespace std; // #define LOCAL // 提出時はコメントアウト #define DEBUG_ typedef long long ll; const double EPS = 1e-9; const ll INF = ((1LL<<62)-(1LL<<31)); typedef vector vecl; typedef pair pairl; template using mapv = map>; #define ALL(v) v.begin(), v.end() #define REP(i, x, n) for(int i = x; i < n; i++) #define rep(i, n) REP(i, 0, n) #define contains(S,x) find(ALL(S),x) != S.end() ll llceil(ll a,ll b) { return (a+b-1)/b; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template vector> genarr(ll n, ll m, T init) { return vector>(n,vector(m,init)); } ///// DEBUG #define DUMPOUT cerr #define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++) templateistream&operator>>(istream&is,vector&vec){for(T&x:vec)is>>x;return is;} templateostream&operator<<(ostream&os,pair&pair_var){os<<"("<ostream&operator<<(ostream&os,const vector&vec){os<<"{";for(int i=0;iostream&operator<<(ostream&os,map&map_var){os<<"{";repi(itr,map_var){os<<*itr;itr++;if(itr!=map_var.end())os<<", ";itr--;} os<<"}";return os;} templateostream&operator<<(ostream&os,set&set_var){os<<"{";repi(itr,set_var){os<<*itr;itr++;if(itr!=set_var.end())os<<", ";itr--;} os<<"}";return os;} void dump_func(){DUMPOUT<void dump_func(Head&&head,Tail&&...tail){DUMPOUT<0){DUMPOUT<<", ";} dump_func(std::move(tail)...);} #ifndef LOCAL #undef DEBUG_ #endif #ifdef DEBUG_ #define DEB #define dump(...) \ DUMPOUT << " " << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" \ << endl \ << " ", \ dump_func(__VA_ARGS__) #else #define DEB if (false) #define dump(...) #endif ////////// // SegTree: https://atcoder.github.io/ac-library/document_ja/segtree.html // @param n `0 <= n` // @return minimum non-negative `x` s.t. `n <= 2**x` int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } // @param n `1 <= n` // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0` int bsf(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } template struct segtree { public: segtree() : segtree(0) {} segtree(int n) : segtree(std::vector(n, e())) {} segtree(const std::vector& v) : _n(int(v.size())) { log = ceil_pow2(_n); size = 1 << log; d = std::vector(2 * size, e()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { // O(logn) assert(0 <= p && p < _n); p += size; d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) { // O(1) assert(0 <= p && p < _n); return d[p + size]; } S prod(int l, int r) { // [l,r): O(logn) assert(0 <= l && l <= r && r <= _n); S sml = e(), smr = e(); l += size; r += size; while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } // [0,n) template int max_right(int l) { // segtree上で二分探索 O(logn) return max_right(l, [](S x) { return f(x); }); } template int max_right(int l, F f) { // segtree上で二分探索 O(logn) assert(0 <= l && l <= _n); assert(f(e())); if (l == _n) return _n; l += size; S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!f(op(sm, d[l]))) { while (l < size) { l = (2 * l); if (f(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template int min_left(int r) { // segtree上で二分探索 O(logn) return min_left(r, [](S x) { return f(x); }); } template int min_left(int r, F f) { // segtree上で二分探索 O(logn) assert(0 <= r && r <= _n); assert(f(e())); if (r == 0) return 0; r += size; S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!f(op(d[r], sm))) { while (r < size) { r = (2 * r + 1); if (f(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; std::vector d; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } }; // (S,*): 結合律の成立と単位元の存在を満たせばOK // 交換律は必要ないので演算の向きを恣意的に決めてOK struct S { ll a; ll size; }; S op(S l, S r) { return {l.a+r.a,l.size+r.size}; } // S * S S e() { return {0,0}; } // モノイドの単位元 int main() { #ifdef LOCAL ifstream in("../../Atcoder/input.txt"); cin.rdbuf(in.rdbuf()); #endif ll N; cin>>N; vecl A(N),B(N); rep(i,N) { cin>>A[i]; A[i]--; } vecl X(N); rep(i,N) { cin>>B[i]; X[--B[i]] = i; } ll invans = 0; segtree seg(N); rep(i,N) { A[i] = X[A[i]]; seg.set(A[i],{1,1}); invans += seg.prod(0,A[i]).a; } ll ans = N * (N-1) / 2 - invans; cout << ans << endl; return 0; }