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
#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
typedef pair<int,int>P;
typedef long long ll;
const int N=200005;
int n,i,j,a[N],f[N],pool[N];ll ans[N];
int F(int x){return f[x]==x?x:f[x]=F(f[x]);}
inline void merge(int x,int y){if(F(x)!=F(y))f[f[x]]=f[y];}
inline bool cmp(int x,int y){return f[x]==f[y]?x<y:f[x]<f[y];}
namespace Inner{
const int M=N*20;
int m,i,a[N],b[N];ll ans[N];
int maxx[N],maxy[N],minx[N],miny[N];
int root[N],l[M],r[M],val[M],tot;
map<P,ll>TF,TG;
inline void umin(int&a,int b){a>b?(a=b):0;}
inline void umax(int&a,int b){a<b?(a=b):0;}
int ins(int x,int a,int b,int c){
  int y=++tot;
  val[y]=val[x]+1;
  if(a==b)return y;
  int mid=(a+b)>>1;
  if(c<=mid){
    l[y]=ins(l[x],a,mid,c);
    r[y]=r[x];
  }else{
    l[y]=l[x];
    r[y]=ins(r[x],mid+1,b,c);
  }
  return y;
}
int ask(int x,int a,int b,int c,int d){
  if(!x)return 0;
  if(c<=a&&b<=d)return val[x];
  int mid=(a+b)>>1,t=0;
  if(c<=mid)t=ask(l[x],a,mid,c,d);
  if(d>mid)t+=ask(r[x],mid+1,b,c,d);
  return t;
}
ll F(int x,int y){
  //(>=x,>=y)
  if(x<1)x=1;
  if(y<1)y=1;
  if(x>m||y>m)return 0;
  int all=m-y+1-ask(root[x-1],1,m,y,m);
  if(!all)return 0;
  int nx=max(x,maxx[y-1]),ny=max(y,maxy[x-1]);
  if(nx==x&&ny==y)return all;
  ll&ret=TF[P(x,y)];
  if(ret)return ret;
  return ret=all+F(nx,ny);
}
ll G(int x,int y){
  //(<=x,<=y)
  if(x>m)x=m;
  if(y>m)y=m;
  if(x<1||y<1)return 0;
  int all=ask(root[x],1,m,1,y);
  if(!all)return 0;
  int nx=min(x,minx[y+1]),ny=min(y,miny[x+1]);
  if(nx==x&&ny==y)return all;
  ll&ret=TG[P(x,y)];
  if(ret)return ret;
  return ret=all+G(nx,ny);
}
inline void solve(){
  for(i=0;i<=m+1;i++){
    maxx[i]=maxy[i]=-M;
    minx[i]=miny[i]=M;
  }
  for(i=1;i<=m;i++)b[i]=a[i];
  sort(b+1,b+m+1);
  for(i=1;i<=m;i++){
    a[i]=lower_bound(b+1,b+m+1,a[i])-b;
    umin(minx[a[i]],i);
    umax(maxx[a[i]],i);
    umin(miny[i],a[i]);
    umax(maxy[i],a[i]);
  }
  for(i=1;i<=m;i++){
    umax(maxx[i],maxx[i-1]);//y<=i maxx
    umax(maxy[i],maxy[i-1]);//x<=i maxy
  }
  for(i=m;i;i--){
    umin(minx[i],minx[i+1]);//y>=i minx
    umin(miny[i],miny[i+1]);//x>=i miny
  }
  //for(i=1;i<=m;i++)printf(">>%d %d\n",i,a[i]);
  for(i=1;i<=m;i++)root[i]=ins(root[i-1],1,m,a[i]);
  for(i=1;i<=m;i++)ans[i]=m-1+F(i+1,a[i]+1)+G(i-1,a[i]-1);
  for(i=0;i<=tot;i++)l[i]=r[i]=val[i]=0;
  tot=0;
  TF.clear();
  TG.clear();
}
}
inline void gao(int l,int r){
  int m=Inner::m=r-l+1;
  for(int i=1;i<=m;i++){
    //printf("%d%c",pool[l+i-1],i<m?' ':'\n');
    Inner::a[i]=a[pool[l+i-1]];
  }
  Inner::solve();
  for(int i=1;i<=m;i++)ans[pool[l+i-1]]=Inner::ans[i];
}
int main(){
  scanf("%d",&n);
  for(i=1;i<=n;i++)scanf("%d",&a[i]),f[i]=i;
  priority_queue<P>q;
  for(i=1;i<=n;i++){
    P mx(a[i],i);
    while(!q.empty()){
      P t=q.top();
      if(t.first<a[i])break;
      mx=max(mx,t);
      merge(i,t.second);
      q.pop();
    }
    q.push(mx);
  }
  for(i=1;i<=n;i++)F(i),pool[i]=i;
  sort(pool+1,pool+n+1,cmp);
  for(i=1;i<=n;i=j){
    for(j=i;j<=n&&f[pool[i]]==f[pool[j]];j++);
    gao(i,j-1);
  }
  for(i=1;i<=n;i++)printf("%lld ",ans[i]);
}