# [BZOJ4199][UOJ#131]【NOI2015】品酒大会

n<=3*10^5
|val| <=10^9

## Solution

i和j这两个位置的字符r相似其实就是它们开头的后缀的最长公共前缀大于等于r

## Code

```#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define imax(a,b) ((a>b)?a:b)
#define N 300005
#define LL long long
using namespace std;
char st[N];
int s2[N],SA[N],rank[N],r1[N],ct[N],n,height[N],d[N],f[N];
LL a[N],ans[N],cnt[N],sz[N],mx[N],mi[N];
void prp()
{
int m=0;
memset(ct,0,sizeof(ct));
fo(i,1,n) ct[rank[i]=st[i]-'a'+1]++,m=imax(m,rank[i]);
fo(i,1,m) ct[i]+=ct[i-1];
fod(i,n,1) SA[ct[rank[i]]--]=i;
for(int j=1,k=0;k<n;j<<=1,m=k)
{
memset(ct,0,sizeof(ct));
int p=0;
fo(i,n-j+1,n) s2[++p]=i;
fo(i,1,n) if(SA[i]>j) s2[++p]=SA[i]-j;
fo(i,1,n) ct[rank[s2[i]]]++;
fo(i,1,m) ct[i]+=ct[i-1];
fod(i,n,1) SA[ct[rank[s2[i]]]--]=s2[i];
r1[SA[1]]=k=1;
fo(i,2,n) r1[SA[i]]=(rank[SA[i]]==rank[SA[i-1]]&&rank[SA[i]+j]==rank[SA[i-1]+j])?k:++k;
fo(i,1,n) rank[i]=r1[i];
}
}

void geth()
{
height[1]=0;
fo(i,1,n)
{
if(rank[i]==1) continue;
int p=imax(0,height[rank[i-1]]-1);
while(st[i+p]==st[SA[rank[i]-1]+p]) p++;
height[rank[i]]=p;
}
}
bool cmp(int x,int y)
{
return height[x]>height[y];
}
int getf(int k)
{
if(f[k]==0||f[k]==k) return k;
return f[k]=getf(f[k]);
}
int main()
{
cin>>n;
scanf("\n%s",st+1);
fo(i,1,n) scanf("%lld",&a[i]);
prp();
geth();
fo(i,1,n) d[i]=i,sz[i]=1,mx[i]=mi[i]=a[SA[i]];
sort(d+1,d+1+n,cmp);
fo(i,0,n) ans[i]=-7000000000000000000;
fo(i,1,n)
{
int w=height[d[i]];
if(w!=height[d[i-1]]) cnt[w]+=cnt[height[d[i-1]]],ans[w]=max(ans[w],ans[height[d[i-1]]]);
if(d[i]==1) continue;
int fx=getf(d[i]),fy=getf(d[i]-1);
cnt[w]+=sz[fx]*sz[fy];
ans[w]=max(ans[w],max(mx[fx]*mx[fy],mi[fx]*mi[fy]));
f[fx]=fy,sz[fy]+=sz[fx],mx[fy]=max(mx[fy],mx[fx]),mi[fy]=min(mi[fy],mi[fx]);
}
fod(i,n-1,0)
{
ans[i]=max(ans[i],ans[i+1]);
if(cnt[i]==0) cnt[i]=cnt[i+1];
}
fo(i,0,n-1)
{
if(ans[i]==-7000000000000000000) ans[i]=0;
printf("%lld %lld\n",cnt[i],ans[i]);
}
}```