#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll=long long; template using V = vector; template using P = pair; using vll = V; using vvll = V; #define rep(i, k, n) for (ll i=k; i<(ll)n; ++i) #define REP(i, n) rep(i, 0, n) #define ALL(v) v.begin(),v.end() template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; } template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; } #define DEBUG_VLL(vec) REP(sz, vec.size()) std::cerr< l, r; ll merge(V& a, int n, int left, int mid, int right) { int i, j, k; ll cnt = 0; int n1 = mid - left, n2 = right - mid; for (i = 0; i < n1; i++) l[i] = a[left + i]; for (i = 0; i < n2; i++) r[i] = a[mid + i]; l[n1] = r[n2] = INF; i = j = 0; for (k = left; k < right; k++) { if (l[i] <= r[j]) { a[k] = l[i++]; } else { a[k] = r[j++]; cnt += n1 - i; } } return cnt; } ll mergesort(V& a, int n, int left, int right) { int mid; ll v1, v2, v3; if (left + 1 < right) { mid = (left + right) >> 1; v1 = mergesort(a, n, left, mid); v2 = mergesort(a, n, mid, right); v3 = merge(a, n, left, mid, right); return v1 + v2 + v3; } else return 0; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; V a(n), b(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { int tb; cin >> tb; b[(--tb)] = i; } V aidx(n); for (int i = 0; i < n; i++) aidx[i] = b[a[i] - 1]; l.resize((n + 1) / 2 + 2), r.resize((n + 1) / 2 + 2); ll ans = mergesort(aidx, n, 0, n); cout << ans << '\n'; return 0; }