General
 
 
# Author Problem Lang Verdict Time Memory Sent Judged  
193333734 Practice:
tgp07
1793D - 23 C++20 (GCC 11-64) Accepted 62 ms 6252 KB 2023-02-12 14:06:33 2023-02-12 14:06:33
→ Source
//#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <tuple>
#include <numeric>
#include <climits>
#include <bitset>
#include <iomanip>
#include <random>
#include <ctime>

using namespace std;

//change the long long to int if you need to save memory/time really badly
typedef long long ll;

//Comment this define when working on interactive problems
#define endl "\n"
#define sqrt(n) sqrt((long double) n)

const ll MAXN = 5e5 + 5;
const ll ZER = 0;
const ll ONE = 1;
const ll INF = LLONG_MAX;
const ll MOD = 1e9 + 7;

ll min(ll a, ll b) {
    if (a < b) {
        return a;
    } else {
        return b;
    }
}
 
ll max(ll a, ll b) {
    if (a > b) {
        return a;
    } else {
        return b;
    }
}

ll mod(ll num) {
    return (num >= MOD ? num % MOD : num);
}

ll __gcd(ll a, ll b) {
    if (b == 0) {
        return a;
    }
    
    if (a == 0) {
        return b;
    }
    
    if (a < b) {
        swap(a, b);
    }
    
    return __gcd(b, a % b);
}

ll __lcm(ll a, ll b) {
    ll prod = a*b;
    return prod/__gcd(a, b);
}

void solve(ll ca)
{
    ll n; cin >> n;
    ll p[n]; ll q[n];
    ll rev1[n]; ll rev2[n];
    for (ll i = 0; i < n; i++) {
        cin >> p[i];
        rev1[p[i]-1] = i;
    }
    for (ll i = 0; i < n; i++) {
        cin >> q[i];
        rev2[q[i]-1] = i;
    }
    /*
    for (ll i = 0; i < n; i++) {
        
    }*/
    
    ll ans = 1;
    
    ll i1 = INF, j1 = -INF, i2 = INF, j2 = -INF;
    for (ll mex = 1; mex <= n; mex++) {
        if (mex == 1) {
            i1 = min(i1, rev1[0]);
            j1 = max(j1, rev1[0]);
            
            i2 = min(i2, rev2[0]);
            j2 = max(j2, rev2[0]);
            
            //cout << i1 << " " << i2 << " " << j1 << " " << j2 << endl;
            
            ll pref = min(i1, i2);
            ll suff = n - 1 - max(j1, j2);
            ll mid = abs(i1-i2)-1;
            
            ll v1 = pref * (pref + 1)/2;
            ll v2 = suff * (suff + 1)/2;
            ll v3 = mid * (mid + 1)/2;
            ans += v1;
            ans += v2;
            ans += v3;
            //cout << v1 << " " << v2 << " " << v3 << endl;
            continue;
        }
        
        if (rev1[mex-1] >= min(i1, i2) && rev1[mex-1] <= max(j1, j2)) {
            
        } else if (rev2[mex-1] >= min(i1, i2) && rev2[mex-1] <= max(j1, j2)) {
            
        } else {
            ll lpref = min(i1, i2)+1;
            ll rsuff = n-max(j1, j2);
            
            if (rev1[mex-1] < min(i1, i2)) {
                lpref = min(lpref, min(i1, i2)-rev1[mex-1]);
            } else {
                rsuff = min(rsuff, rev1[mex-1]-max(j1, j2));
            }
            if (rev2[mex-1] < min(i1, i2)) {
                lpref = min(lpref, min(i1, i2)-rev2[mex-1]);
            } else {
                rsuff = min(rsuff, rev2[mex-1]-max(j1, j2));
            }
            
            //cout << mex << " " << lpref << " " << rsuff << endl;
            ans += (lpref * rsuff);
        }
        
        i1 = min(i1, rev1[mex-1]);
        j1 = max(j1, rev1[mex-1]);
        
        i2 = min(i2, rev2[mex-1]);
        j2 = max(j2, rev2[mex-1]);
    }
    
    cout << ans << endl;
    
    return;
}

int main()
{
    //mt19937 rng(0);
    
    //Fast IO
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    /*
    freopen("sabotage.in", "r", stdin);
    freopen("sabotage.out", "w", stdout);
    */
    
    ll t = 1;
    //cin >> t;

    ll co = 1;
    while (t--) {
        solve(co);
        ++co;
    }
}

//Always write your algorithm down on a piece of paper and question EACH and EVERY step if you aren't able to find an implementation mistake in your code.

?
Time: ? ms, memory: ? KB
Verdict: ?
Input
?
Participant's output
?
Jury's answer
?
Checker comment
?
Diagnostics
?
Click to see test details