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
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(1);
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
//ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=201000,M=5100000;
array<int,3> seg[N];
int fl[N],fr[N],nn,n;
int sufl[N],sufr[N],pl[N],pr[N];
vector<PII> que[N];
ll ans[N],tmpr[M],tmpr2[M];
bool inter(int x,int y) {
	return seg[x][1]>=seg[y][0]&&seg[y][1]>=seg[x][0];
}

template<class T>
struct BIT {
	T c[N];
	int size;
	void init(int s) {
		size = s;
		for (int i = 1; i <= s; i++) c[i] = 0;
	}
	T query(int x) { // 1 ... x
		assert(x <= size);
		T s = 0;
		for (; x; x -= x & (-x)) {
			s += c[x];
		}
		return s;
	}

	void modify(int x, T s) { // a[x] += s
		assert(x != 0);
		for (; x <= size; x += x & (-x)) {
			c[x] += s;
		}
	} 
};

BIT<int> c;

void solve2(int n) {
	rep(i,1,n+1) {
		pr[seg[i][0]]=seg[i][1];
		pl[seg[i][1]]=seg[i][0];
	}
	sufr[n+1]=n+1;
	sufl[n+1]=n+1;
	per(i,1,n+1) {
		sufr[i]=min(sufr[i+1],pr[i]);
		sufl[i]=min(sufl[i+1],pl[i]);
	}
	map<PII,int> tmp;
	int id=0;

	rep(i,1,n+1) que[i].clear();
	rep(i,1,n+1) {
		int pL=seg[i][0],qL=seg[i][1];
		while (1) {
			if (!tmp.count(mp(pL,qL))) {
				tmp[mp(pL,qL)]=++id;
				//printf("?? %d %d %d\n",pL,qL,id);
				que[pL].pb(mp(qL,id));
			} else break;
			int cpL=sufl[qL];
			int cqL=sufr[pL];
			pL=cpL; qL=cqL;
		}
	}
	assert(id<=5000000);
	c.init(n);
	rep(i,1,n+1) {
		for (auto [r,id]:que[i]) tmpr[id]=c.query(r-1);
		c.modify(pr[i],1);
	}
	fprintf(stderr,"%d\n",id);
	rep(i,1,id+1) tmpr2[i]=-1;
	rep(i,1,n+1) {
		int pL=seg[i][0],qL=seg[i][1];
		function<ll(int,int)> gao=[&](int pL,int qL) {
			int id=tmp[mp(pL,qL)];
			if (tmpr2[id]!=-1) return tmpr2[id];
			else {
				tmpr2[id]=tmpr[id];
				if (pL==1&&qL==1) return tmpr2[id];
				return tmpr2[id]+=gao(sufl[qL],sufr[pL]);
			}
		};
		ans[seg[i][2]]+=gao(pL,qL);
	}
}

void solve(int L,int R) {
	n=R-L+1;
	rep(i,1,n+1) {
		seg[i]={fl[L+i-1]-L+1,fr[L+i-1]-L+1,L+i-1};
		ans[L+i-1]+=n-1;
	}
	solve2(n);
	rep(i,1,n+1) {
		seg[i][0]=n+1-seg[i][0];
		seg[i][1]=n+1-seg[i][1];
	}
	solve2(n);
}

int main() {
	scanf("%d",&nn);
	int pre=0,mx=0;
	for (int i=1;i<=nn;i++) {
		fl[i]=i;
		scanf("%d",&fr[i]);
		mx=max(mx,fr[i]);
		if (mx==i) {
			solve(pre+1,i);
			pre=i;
		}
	}
	rep(i,1,nn+1) printf("%lld%c",ans[i]," \n"[i==nn]);
}