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
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x)
#else
#define debug(...) {}
#endif

struct PersistentRangeSum {
	struct node {
		int lf, rg, sum;
		node() {}
		node(int a, int b, int c) : lf(a), rg(b), sum(c) {}
	};
	int com;
	vector<node> st;
	vector<int> days;
	PersistentRangeSum(int n) {
		com = 1;
		while (com < n)
			com <<= 1;
		st.emplace_back(0, 0, 0);
		st.emplace_back(0, 0, 0);
		days.emplace_back(1);
	}
	void update(int v) {
		st[v].sum = st[st[v].lf].sum + st[st[v].rg].sum;
	}
	int ins(int v, int l, int r, int p, int w) {
		if (l == r) {
			st.emplace_back(st[v]);
			v = ssize(st) - 1;
			st[v].sum = w;
			return v;
		}
		int mid = (l + r) / 2;
		st.emplace_back(st[v]);
		v = ssize(st) - 1;
		if (p <= mid) {
			if (!st[v].lf) {
				st[v].lf = ssize(st);
				st.emplace_back(0, 0, 0);
			}
			st[v].lf = ins(st[v].lf, l, mid, p, w);
		}
		else {
			if (!st[v].rg) {
				st[v].rg = ssize(st);
				st.emplace_back(0, 0, 0);
			}
			st[v].rg = ins(st[v].rg, mid + 1, r, p, w);
		}
		update(v);
		return v;
	}
	void ustaw(int x, int w) {
		days.emplace_back(ins(days.back(), 0, com - 1, x, w));
	}
	int zap(int v, int l, int r, int ll, int rr) {
		if (l > rr || ll > r)
			return 0;
		if (ll <= l && r <= rr)
			return st[v].sum;
		return zap(st[v].lf, l, (l + r) / 2, ll, rr) + zap(st[v].rg, (l + r) / 2 + 1, r, ll, rr);
	}
	int query(int day, int l, int r) {
		++day;
		assert(day < ssize(days));
		return zap(days[day], 0, com - 1, l, r);
	}
};

const int INF = int(1e9);

int main() {
	cin.tie(0)->sync_with_stdio(0);
	int n;
	cin >> n;
	vector p(n, 0), q(n, 0);
	REP (i, n) {
		cin >> p[i];
		--p[i];
		q[p[i]] = i;
	}

	PersistentRangeSum cnt(n);
	REP (i, n) {
		cnt.ustaw(p[i], 1);
	}
	auto count_direct = [&](int a, int b) {
		return cnt.query(a - 1, b + 1, n - 1) + cnt.query(n - 1, 0, b - 1) -
			   cnt.query(a, 0, b - 1);
	};

	auto count_between = [&](int a, int b, int l, int r) {
		return cnt.query(b, l, r) - cnt.query(a - 1, l, r);
	};

	vector<int> mnbt(n), mxbt(n), mntp(n), mxtp(n);
	REP (i, n) {
		mnbt[i] = p[i];
		mxbt[i] = p[i];
		mntp[i] = q[i];
		mxtp[i] = q[i];
	}
	FOR (i, 1, n - 1) {
		mxbt[i] = max(mxbt[i], mxbt[i - 1]);
		mxtp[i] = max(mxtp[i], mxtp[i - 1]);
	}
	for (int i = n - 2; i >= 0; --i) {
		mnbt[i] = min(mnbt[i], mnbt[i + 1]);
		mntp[i] = min(mntp[i], mntp[i + 1]);
	}
	
	auto BotMin = [&](int x) {
		if (x < 0 || n <= x)
			return INF;
		return mnbt[x];
	};

	auto BotMax = [&](int x) {
		if (x < 0 || n <= x)
			return -INF;
		return mxbt[x];
	};

	auto TopMin = [&](int x) {
		if (x < 0 || n <= x)
			return INF;
		return mntp[x];
	};

	auto TopMax = [&](int x) {
		if (x < 0 || n <= x)
			return -INF;
		return mxtp[x];
	};

	auto cut_left = [&](int a, int b) {
		return pair(min(a, TopMin(b + 1)), min(b, BotMin(a + 1)));
	};

	auto cut_right = [&](int a, int b) {
		return pair(max(a, TopMax(b - 1)), max(b, BotMax(a - 1)));
	};

	map<pair<int, int>, pair<LL, int>> mp_left, mp_right;
	function<pair<LL, int>(int, int)> count_left = [&](int a, int b) {
		if (mp_left.count(pair(a, b)))
			return mp_left[pair(a, b)];
		int dir = count_direct(a, b);
		auto [x, y] = cut_left(a, b);
		if (x == a && y == b) {
			mp_left[pair(a, b)] = pair(dir, dir);
			return mp_left[pair(a, b)];
		}
		auto [score, ile] = count_left(x, y);
		int mid = count_between(x + 1, a - 1, y + 1, b - 1);
		mp_left[pair(a, b)] = pair(score + dir + ile + mid * 2, dir + ile + mid);
		return mp_left[pair(a, b)];
	};

	function<pair<LL, int>(int, int)> count_right = [&](int a, int b) {
		if (mp_right.count(pair(a, b)))
			return mp_right[pair(a, b)];
		int dir = count_direct(a, b);
		auto [x, y] = cut_right(a, b);
		if (x == a && y == b) {
			mp_right[pair(a, b)] = pair(dir, dir);
			return mp_right[pair(a, b)];
		}
		auto [score, ile] = count_right(x, y);
		int mid = count_between(a + 1, x - 1, b + 1, y - 1);
		mp_right[pair(a, b)] = pair(score + dir + ile + mid * 2, dir + ile + mid);
		return mp_right[pair(a, b)];
	};

	REP (i, n) {
		int dir = count_direct(i, p[i]);
		auto [xl, yl] = cut_left(i, p[i]);
		auto [score_lf, ile_lf] = count_left(xl, yl);
		int midl = count_between(xl + 1, i - 1, yl + 1, p[i] - 1);
		auto [xr, yr] = cut_right(i, p[i]);
		auto [score_rg, ile_rg] = count_right(xr, yr);
		int midr = count_between(i + 1, xr - 1, p[i] + 1, yr - 1);
		if (i)
			cout << ' ';
		cout << dir + score_lf + ile_lf + score_rg + ile_rg + (midl + midr) * 2;
	}
	cout << '\n';
}