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
#include <chrono>
#include <cassert>
#include <string>
#include <array>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cmath>
#include <complex>
#include <numeric>
#include <random>
#include <ios>
#include <iostream>
#include <bitset>
using namespace std; 

#define fwd(i, a, b) for (int i = (a); i < (b); ++ i)
#define rep(i, n) for (int i = 0; i < (n); ++ i)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)x.size())
#define st first
#define nd second
#define pii pair<int, int>
#define pli pair<long long, int>
#define vi vector<int>
#define fastio ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define int long long

#ifdef LOCAL
template<typename Stream, typename T1, typename T2>
Stream& operator << (Stream& out, pair<T1, T2> a) {return out << "(" << a.st << ", " << a.nd << ")";}
 
template<typename Stream, typename T>
Stream &operator << (Stream& out, T v) {
	out << "{";
	int i = 0;
	for (auto x : v)
		out << x << ((++ i) != sz(v) ? ", " : "");
	return out << "}";
}

template<typename... Args>
void dump(Args... x) {((cerr << x << ", "), ...) << '\n';}
 
#define debug(x...) cerr << "[" #x "]: ", dump(x)
#else
#define debug(...) 0
#endif

const int maxN = 200 * 1000;

long long ans[maxN];

vector<pii> qrs[maxN];

map<pii, int> mp;

vector<pli> dp;
vi see;
vi pdp;

vi top, rig;

int memo (int x, int y) {
	if (mp.find(make_pair(x, y)) == end(mp)) {
		int id = sz(dp);
		qrs[x].push_back(make_pair(y, id));
		see.push_back(0);
		pdp.push_back(-1);
		mp[make_pair(x, y)] = id;
		dp.push_back(make_pair(0, 0));
		int nx = min(x, top[y]);
		int ny = min(y, rig[x]);
		if (!(nx == x && ny == y))
			pdp[id] = memo(nx, ny);
	}
	return mp[make_pair(x, y)];
}

int fen[maxN + 1];

void upd(int p) {
	++ p;
	for (; p <= maxN; p = (p | (p - 1)) + 1)
		++ fen[p];
}

int ask(int p) {
	int odp = 0;
	++ p;
	for (; p; p = (p & (p - 1)))
		odp += fen[p];
	return odp;
}

void solve(vector<pii> p, int n, bool secondPhase) {
	if (secondPhase) {
		rep(i, n)
			qrs[i] = vector<pii>();
		dp = vector<pli>();
		pdp = vi();
		see = vi();
		fill(fen, fen + maxN + 1, 0);
		mp = map<pii, int>();
	}
	top = vi(n);
	rig = vi(n);
	rep(i, n)
		top[p[i].nd] = p[i].st;
	rep(i, n)
		rig[p[i].st] = p[i].nd;
	for (int i = n - 2; i >= 0; -- i) {
		top[i] = min(top[i], top[i + 1]);
		rig[i] = min(rig[i], rig[i + 1]);
	}
	vi mdp;
	for (auto [x, y] : p)
		mdp.push_back(memo(x, y));
	vi mxq(n, 0);
	rep(i, n) {
		sort(all(qrs[i]));
		for (auto [y, id] : qrs[i]) {
			see[id] = ask(y - 1);
			if (pdp[id] != -1) {
				dp[id].st = dp[pdp[id]].st + dp[pdp[id]].nd + see[id] - see[pdp[id]];
				dp[id].nd = dp[pdp[id]].nd + see[id] - see[pdp[id]];
			}
		}
		mxq[i] = ask(n - 1);
		int id = i;
		if (secondPhase)
			id = n - 1 - i;
		ans[id] += mxq[i];
		if (mdp[i] != -1) {
			ans[id] -= see[mdp[i]];
			ans[id] += dp[mdp[i]].st + dp[mdp[i]].nd;
		}
		upd(p[i].nd);
	}
}

int32_t main() {
	fastio;
	int n;
	cin >> n;
	vector<pii> p(n);
	rep(i, n) {
		p[i].st = i;
		cin >> p[i].nd;
		-- p[i].nd;
	}
	solve(p, n, false);
	rep(i, n)
		p[i] = make_pair(n - 1 - p[i].st, n - 1 - p[i].nd);
	reverse(all(p));
	solve(p, n, true);
	rep(i, n)
		cout << ans[i] << ' ';
	cout << '\n';
	return 0;
}