#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include using namespace std; int main(){ int n; cin >> n; vectora(n), b(n); for(int i = 0; i < n; i++)cin >> a[i]; for(int i = 0; i < n; i++)cin >> b[i]; vectorc(n); const int sz = 400005; vector pre_a(sz, -1), pre_b(sz, -1); long long ans = 0; int csum = 0; for(int i = 0; i < n; i++){ vectorupd; upd.push_back(a[i]); if(a[i] != b[i])upd.push_back(b[i]); for(auto val:upd){ int l = min(pre_a[val], pre_b[val]); int r = max(pre_a[val], pre_b[val]); for(int i = l + 1; i < r + 1; i++){ c[i]--; if(c[i] == 0)csum--; } } pre_a[a[i]] = i; pre_b[b[i]] = i; for(auto val:upd){ int l = min(pre_a[val], pre_b[val]); int r = max(pre_a[val], pre_b[val]); for(int i = l + 1; i < r + 1; i++){ if(c[i] == 0)csum++; c[i]++; } } ans += i + 1 - csum; } cout << ans << endl; }