# BZOJ3879: SvT

## 3879: SvT

Time Limit: 30 Sec  Memory Limit: 512 MB
Submit: 149  Solved: 57
[Submit][Status][Discuss]

## Description

(我并不想告诉你题目名字是什么鬼)

7 3

popoqqq

1 4

2 3 5

4 1 2 5 6

## Sample Output

0

0

2

Hint

#include<cstdio>
#include<cctype>
#include<queue>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
typedef long long ll;
const int maxn=1000010;
int first[maxn],next[maxn],To[maxn],ToT,e;
To[++e]=v;next[e]=first[u];first[u]=e;
}
int anc[maxn][20],L[maxn],R[maxn],dep[maxn];
void dfs(int x) {
L[x]=++ToT;dep[x]=dep[anc[x][0]]+1;
rep(i,1,19) anc[x][i]=anc[anc[x][i-1]][i-1];
ren anc[To[i]][0]=x,dfs(To[i]);
R[x]=ToT;
}
int lca(int x,int y) {
if(dep[x]<dep[y]) swap(x,y);
dwn(i,19,0) if(1<<i<=dep[x]-dep[y]) x=anc[x][i];
dwn(i,19,0) if(anc[x][i]!=anc[y][i]) x=anc[x][i],y=anc[y][i];
return x==y?x:anc[x][0];
}
int n,m,to[maxn][26],pos[maxn],fa[maxn],l[maxn],cnt=1,last=1;
void extend(int c,int x) {
int p=last,q,np,nq;l[last=np=++cnt]=l[p]+1;pos[x]=np;
for(;!to[p][c];p=fa[p]) to[p][c]=np;
if(!p) fa[np]=1;
else {
q=to[p][c];
if(l[p]+1==l[q]) fa[np]=q;
else {
l[nq=++cnt]=l[p]+1;
memcpy(to[nq],to[q],sizeof(to[q]));
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;to[p][c]==q;p=fa[p]) to[p][c]=nq;
}
}
}
char s[maxn];
int A[maxn*3],S[maxn];
int cmp(int x,int y) {return L[x]<L[y];}
struct Solver {
int first[maxn],next[maxn],to[maxn],is[maxn],e;
to[++e]=v;next[e]=first[u];first[u]=e;
}
ll f[maxn],ans;
ll solve(int x) {
ren {
solve(to[i]);
ans+=f[x]*f[to[i]]*l[x];
f[x]+=f[to[i]];
}
}
}T;
int main() {
dwn(i,n,1) extend(s[i]-'a',i);
dfs(1);
while(m--) {
sort(A+1,A+k+1,cmp);
rep(i,2,k) A[i+k-1]=lca(A[i-1],A[i]);
sort(A+1,A+cnt+1,cmp);k=0;
rep(i,1,cnt) if(A[i]!=A[i-1]) A[++k]=A[i];
T.e=T.ans=0;rep(i,1,k) T.first[A[i]]=0;
rep(i,1,k) {
while(top&&(L[A[i]]<L[S[top]]||L[A[i]]>R[S[top]])) top--;
}