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
38
#include <iostream>
#include <algorithm>

using namespace std;

const int MAX_N = 1e6;

int war[MAX_N + 1];
int kub[MAX_N + 1];

int main() {
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    cout << n * 2 + 1 << " ";
    for (int i = 1; i <= n; i++) {
        cin >> war[i];
        kub[war[i]] = i;
    }
    
    int p = MAX_N + 1, k = 0;
    unsigned long long res = 0;
    for (int i = 1; i <= n; i++) {
        int ileLiczb = i;
        p = min(p, kub[n - (i / 2)]);
        k = max(k, kub[n - (i / 2)]);
        if (k - p + 1 <= ileLiczb) {
            //int reszta = ileLiczb - k + p - 1;
            int pocz = max(1, k - i + 1);
            int kon = min(p + i - 1, n);

            res += kon - (pocz + i - 1) + 1;
        }
    }
    cout << res << "\n";

    return 0;
}