#include using namespace std; using i64 = long long; using u64 = unsigned long long; #define REP(i, n) for (int i = 0; (i64)(i) < (i64)(n); ++i) #ifdef ENABLE_DEBUG template void debug(T value) { cerr << value; } template void debug(T value, Ts... args) { cerr << value << ", "; debug(args...); } #define DEBUG(...) \ do { \ cerr << " \033[33m (L" << __LINE__ << ") "; \ cerr << #__VA_ARGS__ << ":\033[0m "; \ debug(__VA_ARGS__); \ cerr << endl; \ } while (0) #else #define debug(...) #define DEBUG(...) #endif template struct SegTree { using Func = function; const Func F; const Monoid UNITY; int size; vector dat; SegTree(int n, const Func f, const Monoid &unity) : F(f), UNITY(unity), size(n), dat(2 * n, unity) {} // Sets i-th value (0-indexed) to x for initial setup. // build() must be called after set() calls. void set(int i, const Monoid &x) { dat[size + i] = x; } void build() { for (int k = size - 1; k > 0; --k) { dat[k] = F(dat[k * 2], dat[k * 2 + 1]); } } // Sets i-th value (0-indexed) to x. void update(int i, const Monoid &x) { int k = size + i; dat[k] = x; while (k > 1) { k >>= 1; dat[k] = F(dat[k * 2], dat[k * 2 + 1]); } } // Queries by [l,r) range (0-indexed, open interval). Monoid fold(int l, int r) { l += size; r += size; Monoid vleft = UNITY, vright = UNITY; for (; l < r; l >>= 1, r >>= 1) { if (l & 1) vleft = F(vleft, dat[l++]); if (r & 1) vright = F(dat[--r], vright); } return F(vleft, vright); } // Queries by a single index (0-indexed). Monoid operator[](int i) { return dat[size + i]; } /* debug */ void print() { for (int i = 0; i < size; ++i) { cout << (*this)[i]; if (i != size - 1) cout << ","; } cout << endl; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector A(n), B(n); for (auto &x : A) { cin >> x; --x; } for (auto &x : B) { cin >> x; --x; } vector ar(n), br(n); REP(i, n) { ar[A[i]] = i; br[B[i]] = i; } SegTree ast( n, [](i64 x, i64 y) { return x + y; }, 0LL); REP(i, n) { ast.set(i, 1); } ast.build(); i64 steps = 0; REP(i, n) { int k = ar[B[i]]; i64 s = ast.fold(0, k); steps += s; ast.update(k, 0); } cout << steps << endl; }