1
2
3
4
5
6
7
8
9
10
11
12
13
|
signed main()
{
scanf("%s",str+1);
n=strlen(str+1);ans=1ll*(n+1)*n*(n-1)/2;
R(i,1,n) s[i]=str[i]-'a'+2;
get_SA(s,sa,rk,n),get_ht(n,ht);ht[n+1]=-1;
R(i,1,n) l[i]=r[i]=i;
R(i,1,n) while(ht[l[i]-1]>ht[i]) l[i]=l[l[i]-1];
L(i,1,n) while(ht[r[i]+1]>=ht[i]) r[i]=r[r[i]+1];
R(i,1,n) ans-=2ll*ht[i]*(i-l[i]+1)*(r[i]-i+1);
//L(i,1,n) printf("%lld %lld %lld %lld\n",i,i-l[i]+1,r[i]-i+1,ht[i]);
printf("%lld\n",ans);
}
|