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<bits/stdc++.h>
#define pb push_back

using namespace std;

typedef long long lld;

constexpr int MAX_N = 1000000;

int t[MAX_N+9], poz[MAX_N+9];

int main() {
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", &t[i]);
		poz[t[i]] = i;
	}
	int l = n;
	int r = 1;
	lld wyn = 0;
	for(int i = n; i >= 1; --i) {
		l = min(l, poz[i]);
		r = max(r, poz[i]);
		int ll = 0;
		int rr = 0;
		int spots = (n-i)*2+1 - (r - l + 1);
		if(poz[i-1] < l) {
			ll = l - poz[i-1] - 1;
			rr = n - r;
		}
		else if(r < poz[i-1]) {
			ll = l-1;
			rr = poz[i-1] - r - 1;
		}
		else {
			spots = -1;
		}
		ll = min(ll, spots);
		rr = min(rr, spots);
		if(ll > rr) {
			swap(ll, rr);
		}
		//lld old_wyn = wyn;
		for(int j = 0; j <= ll; ++j) {
			wyn += min(rr, spots-j)+1;
		}
		//printf("%d: %lld\n", i, wyn-old_wyn);
	}
	printf("%d %lld\n", n*2+1, wyn);
}
/*
5
1 4 3 5 2

//*/