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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <bits/stdc++.h>

#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define st first
#define nd second

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__) << "] "

template <typename T> inline void mini(T &a4, T b4) { a4 = min(a4, b4); }
template <typename T> inline void maxi(T &a4, T b4) { a4 = min(a4, b4); }
 
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vll = vector<ll>;

constexpr int kLeastBits = 9;
constexpr int kBlockSize = 1 << kLeastBits;
constexpr int kBlockMask = kBlockSize - 1;

struct DynamicPrefSum {
	vi in_block;
	vi out_block;
	
	DynamicPrefSum(int n) {
		++n;
		in_block.resize(n + kBlockSize);
		out_block.resize(n / kBlockSize + 1);
	}
	
	void Inc(int loc) {
		for (int i = loc + 1; i & kBlockMask; ++i) {
			++in_block[i];
		}
		const int nblocks = SZ(out_block);
		for (int i = loc / kBlockSize + 1; i < nblocks; ++i) {
			++out_block[i];
		}
	}
	
	int GetPref(int sz) {
		return in_block[sz] + out_block[sz >> kLeastBits];
	}
};

struct ConnectedSol {
	vi a_to_b, b_to_a;
	vi a_to_minb, b_to_mina;
	vector<vi> children;
	vi depth;
	vi parent;
	vi visit_order;
	vi endpoint_to_introduce;
	vi result_endpoint_idx;
	int n;
	
	struct TreeSegment {
		// Smutny, ale potrzebny hack: nie potrzebujemy (a, b) oraz sol
		//   w tym samym czasie, więc każemy im korzystać z tej samej pamięci.
		union {
			struct {
				int a = -1, b = -1;
			};
			ll sol;
		};
		
		friend ostream &operator<<(ostream &os, const TreeSegment &ts) {
			os << "Segment(a=" << ts.a << ", b=" << ts.b << ": sol=" <<
				ts.sol << ")";
			return os;
		}
	};
	
	void DfsChildren(int v) {
		for (int s : children[v]) {
			depth[s] = depth[v] + 1;
			parent[s] = v;
			DfsChildren(s);
		}
	}
	
	void MakeTree() {
		children.resize(2 * n);
		children[0].push_back(n);
		for (int i = 1; i < n; ++i) {
			children[b_to_mina[i]].push_back(n + i);
			children[n + a_to_minb[i]].push_back(i);
		}
		depth.resize(2 * n);
		parent.resize(2 * n, -1);
		endpoint_to_introduce.resize(2 * n, -1);
		DfsChildren(0);
		debug() << imie(depth);
		
		for (int i = 0; i < n; ++i) {
			const int j = n + a_to_b[i];
			const int da = depth[i];
			const int db = depth[j];
			assert(abs(da - db) == 1);
			if (da < db) {
				endpoint_to_introduce[i] = j;
			} else {
				endpoint_to_introduce[j] = i;
			}
		}
		
		visit_order.resize(2 * n);
		iota(ALL(visit_order), 0);
		sort(ALL(visit_order), [&](int lhs, int rhs) {
			return make_pair(depth[lhs], lhs) > make_pair(depth[rhs], rhs);
		});
		debug() << imie(visit_order);
	}
	
	vector<TreeSegment> tree_segments;
	vi tree_segments_pnt;
	
	void CreateTreeSegments() {
		vector<int> endpt_to_id(2 * n, -1);
		vector<int> endpts_to_clear;
		vector<vi> endpts_by_hi(2 * n);
		result_endpoint_idx.resize(n, -1);
		
		// Trochę hack. Chcę uniknąć problemów w sytuacji, w której liczba
		//   odcinków na vectorze przekroczy 2^25 (prawdopodobnie to sie nie stanie,
		//   ale nigdy nie można być pewnym).
		// Już prawie na pewno ta liczba nie powinna przekroczyć 44'444'444, prawda?
		// Sam umiem wygenerować max. ok. 24,5 mln.
		if (n > 100'000) {
			tree_segments.reserve(44'444'444);
			tree_segments_pnt.reserve(44'444'444);
		}
		
		auto AddLowEndpoint = [&](int hi_endpt, int lo_endpt) {
			if (endpt_to_id[lo_endpt] == -1) {
				const int new_id = SZ(tree_segments);
				debug() << "new segment" << imie(new_id) << imie(hi_endpt) << imie(lo_endpt);
				tree_segments.emplace_back();
				tree_segments_pnt.push_back(-1);
				tie(tree_segments[new_id].a, tree_segments[new_id].b) = minmax(hi_endpt, lo_endpt);
				
				endpt_to_id[lo_endpt] = new_id;
				endpts_to_clear.push_back(lo_endpt);
				endpts_by_hi[hi_endpt].push_back(new_id);
			}
			return endpt_to_id[lo_endpt];
		};
		
		auto ClearEndpoints = [&]() {
			for (int v : endpts_to_clear) {
				endpt_to_id[v] = -1;
			}
			endpts_to_clear.clear();
		};
		
		for (int hi_endpt : visit_order) {
			if (endpoint_to_introduce[hi_endpt] != -1) {
				const int lo_endpt = endpoint_to_introduce[hi_endpt];
				const int a = min(hi_endpt, lo_endpt);
				const int seg_idx = AddLowEndpoint(hi_endpt, endpoint_to_introduce[hi_endpt]);
				result_endpoint_idx[a] = seg_idx;
			}
			for (int hi_child : children[hi_endpt]) {
				for (int seg_child : endpts_by_hi[hi_child]) {
					const int lo_child =
						tree_segments[seg_child].a ^ tree_segments[seg_child].b ^ hi_child;
					const int lo_endpt = parent[lo_child];
					const int seg_idx = AddLowEndpoint(hi_endpt, lo_endpt);
					tree_segments_pnt[seg_child] = seg_idx;
				}
				endpts_by_hi[hi_child].clear();
				endpts_by_hi[hi_child].shrink_to_fit();
			}
			ClearEndpoints();
		}

		//~ cerr << SZ(tree_segments) << " " << tree_segments.capacity() << " " << sizeof(TreeSegment) << "\n";
		debug() << imie(tree_segments);
	}
	
	void ComputeMiniSols() {
		vector<vi> segments_by_a(n);
		for (int i = 0; i < SZ(tree_segments); ++i) {
			segments_by_a[tree_segments[i].a].push_back(i);
		}
		
		DynamicPrefSum pref_sum(n);
		for (int a = 0; a < n; ++a) {
			for (int seg_idx : segments_by_a[a]) {
				const int b = tree_segments[seg_idx].b - n;
				const int mini_sol = pref_sum.GetPref(b);
				tree_segments[seg_idx].sol = mini_sol;
				debug() << imie(a) << imie(b) << imie(mini_sol);
			}
			pref_sum.Inc(a_to_b[a]);
		}
	}
	
	void PropagateSols() {
		const int S = SZ(tree_segments);
		for (int i = S - 1; i >= 0; --i) {
			auto &seg = tree_segments[i];
			const int pnt = tree_segments_pnt[i];
			if (pnt != -1) {
				seg.sol += tree_segments[pnt].sol;
			}
			debug() << imie(seg);
		}
	}
	
	vll GatherAnswers() {
		vll ans(n, -1);
		for (int a = 0; a < n; ++a) {
			const int seg_idx = result_endpoint_idx[a];
			ans[a] = tree_segments[seg_idx].sol;
		}
		return ans;
	}
	
	vll Solve(const vi &elems) {
		debug() << imie(elems);
		n = SZ(elems);
		a_to_b.resize(n);
		b_to_a.resize(n);
		for (int i = 0; i < n; ++i) {
			a_to_b[i] = elems[i];
			b_to_a[elems[i]] = i;
		}
		
		a_to_minb.resize(n);
		b_to_mina.resize(n);
		for (int i = n - 1; i >= 0; --i) {
			a_to_minb[i] = a_to_b[i];
			if (i < n - 1) { mini(a_to_minb[i], a_to_minb[i + 1]); }
			
			b_to_mina[i] = b_to_a[i];
			if (i < n - 1) { mini(b_to_mina[i], b_to_mina[i + 1]); }
		}
		
		debug() << imie(a_to_minb);
		debug() << imie(b_to_mina);
		MakeTree();
		CreateTreeSegments();
		ComputeMiniSols();
		PropagateSols();
		return GatherAnswers();
	}
};

vll SolveSegment(vi elems) {
	debug() << "subroutine" << imie(elems);
	const int n = SZ(elems);
	vll answers(n, n - 1);
	vll ans_left = ConnectedSol{}.Solve(elems);
	reverse(ALL(elems));
	for (int &x : elems) {
		x = (n - 1) - x;
	}
	vll ans_right = ConnectedSol{}.Solve(elems);
	reverse(ALL(ans_right));
	
	for (int i = 0; i < n; ++i) {
		answers[i] += ans_left[i] + ans_right[i];
	}
	return answers;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(11);
	cerr << fixed << setprecision(6);
	
	int n;
	cin >> n;
	
	vi elems(n);
	for (int &x : elems) { cin >> x; --x; }
	
	vi splits{0};
	int max_pref = -1;
	for (int i = 0; i < n; ++i) {
		max_pref = max(max_pref, elems[i]);
		if (max_pref == i) {
			splits.push_back(i + 1);
		}
	}
	
	vll answers(n);
	for (int i = 1; i < SZ(splits); ++i) {
		const int L = splits[i - 1];
		const int R = splits[i];
		vi subsegment(elems.begin() + L, elems.begin() + R);
		for (int &x : subsegment) { x -= L; }
		vll sub_answers = SolveSegment(subsegment);
		for (int j = L; j < R; ++j) {
			answers[j] += sub_answers[j - L];
		}
	}
	
	for (ll ans : answers) {
		cout << ans << " ";
	}
	cout << "\n";
}