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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int arr[1000009];
int pos[1000009];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;
    int left, right, curr = n, len;
    ll cnt = 1;
    for(int i = 1; i <= n; i++) {
        cin >> arr[i];
        pos[arr[i]] = i;
    }
    left = right = pos[n];
    for(int i = 2; i <= n; i++) {
        curr = n - i/2;
        if(pos[curr] < left)
            left = pos[curr];
        else if(right < pos[curr])
            right = pos[curr];
        len = right-left+1;
        cnt += max(0, min(left-1, i-len)+1 + min(n-right, i-len)+1 - (i-len+1));
    }
    cout << 2*n+1 << " " << cnt << "\n";

    return 0;
}