1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <vector>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<int>V(n);
    for(int &v : V)
        cin >> v;

    vector<int>Vinv(n+1);
    for(int i=0;i<n;i++)
        Vinv[V[i]] = i;

    long long result = 0;
    for(int i = 1, rl = Vinv[n], rr = Vinv[n], li = n; i <= n; i++) {
        if (i % 2 == 0)
            li--;
        rl = min(rl, Vinv[li]);
        rr = max(rr, Vinv[li]);
        int left = rl, right = n-rr-1;
        int base = rr-rl+1;
        if (i < base)
            continue;
        int minr = max(i-base-left, 0);
        int maxr = min(right, i-base);
        result += maxr - minr + 1;
    }

    cout << 2*n+1 << " " << result << "\n";

    return 0;
}