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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
	*this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
// debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; }

bool FAKE = false;
// bool FAKE = true;

struct Node {
	int sum = 0;
	int left, right;
};

const int BASE = 256 * 1024;
vector<Node> tree[2*BASE];

void recClear(int id) {
	if ((int) tree[id].size() == 1) {
		return;
	}
	tree[id] = {Node{0,0,0}};
	if (id < BASE) {
		recClear(2*id);
		recClear(2*id+1);
	}
}

void init() {
	static bool started = false;
	if (!started) {
		started = true;
		for (int i = 1; i < 2 * BASE; i++) {
			tree[i] = {Node{0,0,0}};
		}
	}
	else {
		recClear(1);
	}
}

void add(int i) {
	int id = 1;
	for (int k = 17; k >= 0; k--) {
		const Node& last = tree[id].back();
		if (i & (1 << k)) {
			// go right
			tree[id].push_back(Node{last.sum, last.left, last.right + 1});
			id = 2 * id + 1;
		}
		else {
			// go left
			tree[id].push_back(Node{last.sum + 1, last.left + 1, last.right});
			id = 2 * id;
		}
	}
}
int getSumBefore(int tajm, int i) {
	int id = 1;
	int sum = 0;
	for (int k = 17; k >= 1; k--) { // no leaf
		if (i & (1 << k)) {
			// go right
			sum += tree[id][tajm].sum; // it's actually sum from left child
			tajm = tree[id][tajm].right;
			id = 2 * id + 1;
		}
		else {
			// go left
			tajm = tree[id][tajm].left;
			id = 2 * id;
		}
	}
	return sum;
}

const int NAX = 2e5 + 5;
const int A = 7600; // 30 moves on average
const int B = 7000;
long long c2[A][2*B+1];
int who[NAX];

int bad() {
	return B + 2 + rand() % (B / 20);
}

bool done(int x, int y) {
	return abs(x-y) <= B && who[x] && c2[who[x]][x-y+B];
}

vector<long long> solve(int n, vector<int> p, bool firstIteration) {
	if (!firstIteration) {
		for (int i = 0; i < A; i++) {
			for (int j = 0; j <= 2 * B; j++) {
				c2[i][j] = 0;
			}
		}
	}
	
	// mapka.clear();
	
	vector<int> inv(n+1);
	for (int i = 1; i <= n; i++) {
		inv[p[i]] = i;
	}
	vector<vector<int>> sufMin(2, vector<int>(n+2, n+1));
	
	for (int i = n; i >= 1; --i) {
		sufMin[0][i] = min(p[i], sufMin[0][i+1]);
		sufMin[1][i] = min(inv[i], sufMin[1][i+1]);
	}
	
	vector<long long> ans(n+1);
	
	int start = 1;
	for (int end = 1; end <= n; end++) {
		if (sufMin[0][end+1] == end+1 && sufMin[1][end+1] == end+1) {
			// block end
			
			init();
			
			for (int i = start; i <= end; i++) {
				add(p[i]);
				if (firstIteration) {
					ans[i] += end - start;
				}
				int x = i;
				int y = p[i];
				if (FAKE) {
					if (rand() % 2) {
						y = min(n, x + bad());
					}
					else {
						y = max(1, x - bad());
					}
				}
				// debug() << "start" imie(x) imie(y);
				
				vector<pair<int,int>> path;
				
				long long soFar = 0;
				while(true) {
					if (done(x, y)) {
						soFar = c2[who[x]][x-y+B];
						break;
					}
					path.emplace_back(x, y);
					
					// ans[i] += getSumBefore(x-start, y+1);
					
					// for (int j = start; j < x; j++) {
						// if (p[j] < y) {
							// ans[i]++;
						// }
					// }
					
					int x2 = sufMin[1][y];
					int y2 = sufMin[0][x];
					if (FAKE) {
						x2 = max(1, min(x, y) - 1);
						y2 = max(1, x2 - bad());
						if (rand() % 2) {
							swap(x2, y2);
						}
						if (x2 == 1 || y2 == 1) {
							break;
						}
					}
					// debug() << imie(x2) imie(y2);
					if (x == x2 && y == y2) {
						assert(x2 == y2);
						break;
					}
					// for (int j = x2; j < x; j++) {
						// if (y2 <= p[j] && p[j] < y) {
							// ans[i] += turn;
						// }
					// }
					x = x2;
					y = y2;
				}
				reverse(path.begin(), path.end());
				debug() << imie(path);
				for (const pair<int,int>& edge : path) {
					int x = edge.first;
					int y = edge.second;
					soFar += getSumBefore(x-start, y+1);
					if (who[x] && abs(x-y) <= B) {
						c2[who[x]][x-y+B] = soFar;
					}
					// mapka[{x,y}] = soFar;
				}
				ans[i] += soFar;
			}
			start = end + 1;
		}
	}
	// debug() << imie(ans);
	
	return ans;
}

int main() {
	int n;
	scanf("%d", &n);
	
	vector<int> chooseA(n);
	for (int i = 0; i < n; i++) {
		chooseA[i] = i + 1;
	}
	random_shuffle(chooseA.begin(), chooseA.end());
	int nxt = 1;
	for (int i = 0; i < min(A - 3, n); i++) {
		who[chooseA[i]] = nxt++;
	}
	
	vector<int> p(n+1);
	for (int i = 1; i <= n; i++) {
		if (FAKE) p[i] = i;
		else scanf("%d", &p[i]);
	}
	if (FAKE) {
		random_shuffle(p.begin() + 1, p.begin() + n + 1);
	}
	vector<long long> answer[2];
	for (int rep = 0; rep < 2; rep++) {
		answer[rep] = solve(n, p, rep==0);
		reverse(p.begin() + 1, p.begin() + n + 1);
		for (int i = 1; i <= n; i++) {
			p[i] = n + 1 - p[i];
		}
	}
	for (int i = 1; i <= n; i++) {
		printf("%lld ", answer[0][i] + answer[1][n+1-i]);
	}
	puts("");
}