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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

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

    int n;
    cin >> n;
    vector<long long> v;
    for (int i = 0; i < n; i++) {
        long long e;
        cin >> e;
        v.push_back(e);
    }

	vector<long> mp(n + 1);
	vector<long> left;
	vector<long> right;

	for (int i = 0; i < n; i++) {
		mp[v[i]] = i;
	}
	for (int i = n, j = 0; i > 0; i--, j++) {
		long val = mp[i];
		if (left.empty()) {
			left.push_back(val);
			right.push_back(val);
		}
		else {
			left.push_back(min(left[j - 1], val));
			right.push_back(max(right[j - 1], val));
		}
	}

	long long count = 0;
	for (int i = 1; i <= n; i++) {
		int k = i / 2;
		long l = left[k];
		long r = right[k];
		long length = r - l + 1;
		if (length <= i) {
			long pocz = max(r - i + 1, 0l);
			long konc = min(l + i - 1, n - 1l);
			long dist = konc - pocz + 1;
			long long add = dist - i + 1;
			count += add;
		}
	}
	cout << n * 2 + 1 << " " << count;

    return 0;
}