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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

struct seg_tree {
    vector<int>t;
    int leaf_count;
    seg_tree(int n) {
        for(leaf_count = 1; leaf_count < n; leaf_count *= 2) {}
        t.resize(leaf_count * 2);
    }
    void add(int x, int value) {
        for(x += leaf_count; x > 0; x /= 2)
            t[x] += value;
    }
    int suffix_sum(int x) {
        if(x < 0 || x >= leaf_count)
            return 0;
        int answer = t[x + leaf_count];
        for(x += leaf_count; x > 1; x /= 2) {
            if(x % 2 == 0) {
                answer += t[x + 1];
            }
        }
        return answer;
    }
};

int get_id(int x, int y, vector<pair<int, int>>&v) { 
    return lower_bound(v.begin(), v.end(), make_pair(x, y)) - v.begin();
}

struct Event {
    int x, y, id;
    Event() {}
    Event(int _x, int _y, int _id) : x(_x), y(_y), id(_id) {}
};

bool operator< (Event a, Event b) {
    if(a.x == b.x)
        return a.id > b.id;
    return a.x > b.x;
}

vector<long long>solve(vector<int>y) {
    int n = y.size();
    vector<long long>answer(n, n - 1);
    for(int q = 0; q < 2; q++) {
        
        vector<int>x(n);
        for(int i = 1; i < n; i++)
            x[y[i]] = i;
        vector<int>y_max(n), x_max(n);
        y_max[0] = y[0];
        x_max[0] = x[0];
        for(int i = 1; i < n; i++) {
            y_max[i] = max(y_max[i - 1], y[i]);
            x_max[i] = max(x_max[i - 1], x[i]);
        }
        
        set<pair<int, int>>states_set;
        vector<pair<int, int>>states;
        
        for(int i = 0; i < n; i++) {
            int my_x = x_max[y[i]];
            int my_y = y_max[i];
            
            while(states_set.find(make_pair(my_x, my_y)) == states_set.end()) {
                
                states_set.insert(make_pair(my_x, my_y));
                states.emplace_back(my_x, my_y);
                
                int new_my_x = x_max[my_y];
                int new_my_y = y_max[my_x];
                
                my_x = new_my_x;
                my_y = new_my_y;
            }
        }
        
        sort(states.begin(), states.end());
        vector<long long>states_values(states.size());
        
        vector<Event>events;
        for(int i = 0; i < (int)states.size(); i++)
            events.emplace_back(states[i].first, states[i].second, i + 1);
        
        for(int i = 0; i < n; i++)
            events.emplace_back(i, y[i], -(i + 1));
        
        sort(events.begin(), events.end());
        
        seg_tree t(n);
        for(Event e : events) {
            int id = abs(e.id) - 1;
            if(e.id < 0) {
                answer[q % 2 == 0 ? id : n - id - 1] += t.suffix_sum(e.y + 1);
                t.add(e.y, 1);
            } else {
                states_values[id] += t.suffix_sum(e.y + 1);
            }
        }
        
        for(int i = (int)states.size() - 2; i >= 0; i--) {
            int next_x = x_max[states[i].second];
            int next_y = y_max[states[i].first];
            states_values[i] += states_values[get_id(next_x, next_y, states)];
        }
        
        for(int i = 0; i < n; i++) {
            int my_x = x_max[y[i]];
            int my_y = y_max[i];
            int ind = q % 2 == 0 ? i : n - i - 1;
            answer[ind] += states_values[get_id(my_x, my_y, states)];
        }
        
        reverse(y.begin(), y.end());
        for(int &t : y)
            t = n - t - 1;
    }
    
    return answer;
}

int main() {
    ios_base::sync_with_stdio(0);
    int n; cin >> n;
    vector<int>p(n);
    for(int &x : p) {
        cin >> x;
        x--;
    }
    for(int i = 0; i < n; i++) {
        int j = i, mx = p[i], mn = p[i];
        while(mx > j) {
            j++;
            mx = max(mx, p[j]);
            mn = min(mn, p[j]);
        }
        vector<int>sub_p(j - i + 1);
        for(int k = i; k <= j; k++)
            sub_p[k - i] = p[k] - mn;
        vector<long long>answer = solve(sub_p);
        for(long long x : answer)
            cout << x << ' ';
        i = j;
    }
    cout << '\n';
    
    return 0;
}