显示代码纯文本
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
const int maxn=200010;
int cnt,last;
long long ans;
int fa[maxn],len[maxn];
map<int,int>ch[maxn];
struct SAM{
SAM(){
cnt=0;
last=++cnt;
ans=0;
}
void Insert(int c){
int p=last,np=last=++cnt;
len[np]=len[p]+1;
while(p&&!ch[p][c])ch[p][c]=np,p=fa[p];
if(!p)fa[np]=1;
else{
int q=ch[p][c];
if(len[p]+1==len[q])fa[np]=q;
else{
int nq=++cnt;
len[nq]=len[p]+1;
ch[nq]=ch[q];
fa[nq]=fa[q];
fa[q]=fa[np]=nq;
while(ch[p][c]==q)ch[p][c]=nq,p=fa[p];
}
}
ans+=len[last]-len[fa[last]];
printf("%lld\n",ans);
return;
}
}sam;
int main(){
#ifndef ONLINE_JUDGE
freopen("menci_incantation.in","r",stdin);
freopen("menci_incantation.out","w",stdout);
#endif
int n;
scanf("%d",&n);
for(int i=1,a;i<=n;i++){
scanf("%d",&a);
sam.Insert(a);
}
return 0;
}