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

using namespace std;

const int MAX_N=1000000;

int tab[MAX_N], poz[MAX_N], n, p, l, o, m;
long long int wyn;

int main()
{
	cin >> n;
	cout << 2 * n + 1<<" ";
	for (int i = 0; i < n; i++)
	{
		cin >> tab[i];
		poz[tab[i]] = i;
	}
	m = n;
	p = poz[n];
	l = p;
	int smalw;
	for (int i = 1; i <= n; i++)
	{
		if (i == n)
		{
			wyn++;
			continue;
		}
		if (0 == i % 2)
		{
			m--;
			if (poz[m] > p)p = poz[m];
			if (poz[m] < l)l = poz[m];
		}
		o = p - l;
		smalw = i - o;
		if (0 >= smalw)continue;
		if (smalw + p > n )smalw = n - p;
		if (smalw - 1 > l)smalw = l + 1;
		//cout <<i<< smalw << " ";
		if (0 >= smalw)continue;
		wyn += smalw;
	}
	cout << wyn;
}