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
71
72
73
74
75
76
77
//Szymon Januszek 3LO
#include <bits/stdc++.h>

using namespace std;

int ocen[1000005];

int b_tree[3000000], e_tree[3000000];
int tree_size;

int find(const int *tree, int n, int x) {
    if(n >= tree_size) return n - tree_size;
    if(tree[2*n] >= x) return find(tree, 2*n, x);
    if(tree[2*n + 1] >= x) return find(tree, 2*n + 1, x);
    return -1;
}

void add_point_tree(int *tree, int n, int v) {
    tree[n] = v;

    while((n >>= 1)) tree[n] = max(tree[2*n], tree[(2*n) + 1]);
}

int getInt() {
    char c = getchar_unlocked();
    int w = 0;

    while(c < '0' && c != '-') c = getchar_unlocked();

    while(c >= '0') {
        w *= 10;
        w += c - '0';
        c = getchar_unlocked();
    }

    return w;
}


int main() {
    int n;
    cin >> n;

    tree_size = 1 << (int)ceil(log2(n + 2));

    for(int i = 0; i < n; i++) ocen[i] = getInt();//scanf("%d", ocen + i);

    cout << (2*n + 1) << " ";

    for(int i = 0; i < n; i++) {
        b_tree[tree_size + i] = n + 1;
        e_tree[tree_size + i] = n + 1;
    }

    for(int i = tree_size - 1; i > 0; i--) {
        b_tree[i] = max(b_tree[2*i], b_tree[(2*i) + 1]);
        e_tree[i] = max(e_tree[2*i], e_tree[2*i + 1]);
    }

    int64_t w = 2;

    for(int i = n + 2; i < 2*n; i++) {
        int k = i - n - 1;

        add_point_tree(b_tree, tree_size + k - 1, ocen[k - 1]);
        add_point_tree(e_tree, tree_size + k - 1, ocen[n - k]);

        int len = max(find(b_tree, 1, i >> 1), 0);
        len += max(find(e_tree, 1, i >> 1), 0);

        w += max(len - k + 1, 0);
    }

    cout << w << endl;

    return 0;
}