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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <time.h>
#include <queue>

using namespace std;

int main () {
	int n;
	scanf("%d", &n);
	vector<int> a;
	vector<int> pos(1000007, -1);
	for (int i = 0; i < n; ++i) {
		int x;
		scanf("%d", &x);
		a.push_back(x);
		pos[x] = i;
	}
	int l = pos[n];
	int r = pos[n];
	int seq_start = n;
	int64_t res = 0;
	for (int seq_start = n; seq_start > 0; --seq_start) {
		if (pos[seq_start] < l) {
			l = pos[seq_start];
		}
		if (pos[seq_start] > r) {
			r = pos[seq_start];
		}
		int forbid = seq_start == 1 ? -1 : pos[seq_start - 1];
		int lbound = 0;
		int rbound = n -1;
		if (forbid < l) {
			lbound = forbid + 1;
		}
		else if (forbid > r) {
			rbound = forbid - 1;
		}
		else {
			continue;
		}
		int max_extra = 2 * (n - seq_start) - (r - l);
		int max1 = l - lbound;
		int max2 = rbound - r;
		if (max1 > max2) {
			int tmp = max1;
			max1 = max2;
			max2 = tmp;
		}
		if (max_extra >= max1 + max2) {
			res += (max1 + 1) * (max2 + 1);
		}
		else if (max_extra > max2) {
			res += (max1 + 1) * (max2 + 1) - (max1 + max2 - max_extra) * (max1 + max2 - max_extra + 1) / 2;
		}
		else if (max_extra > max1) {
			res += (max_extra + 1) * (max1 + 1) - max1 * (max1 + 1) / 2;
		}
		else if (max_extra >= 0) {
			res += (max_extra + 1) * (max_extra + 2) / 2;
		}
	}
	printf("%d %lld\n", 2 * n + 1, res);
	return 0;
}