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
132
133
134
135
136
137
138
139
140
|
#include <cstdio>
#include <cctype>
#include <algorithm>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int MAXN = 310000;
namespace fast_io{
//...
}using namespace fast_io;
namespace SA{
int s[MAXN],sa[MAXN],ht[MAXN],rk[MAXN],x[MAXN],y[MAXN],cnt[MAXN];
void get_sa(int n,int m){
for(int i = 0;i<m;i++) cnt[i] = 0;
for(int i = 0;i<n;i++) cnt[s[i]]++;
for(int i = 1;i<m;i++) cnt[i] += cnt[i-1];
for(int i = n-1;~i;--i) sa[--cnt[s[i]]] = i;
m = rk[sa[0]] = 0;
for(int i = 1;i<n;i++) rk[sa[i]] = s[sa[i]] != s[sa[i-1]]?++m:m;
for(int j = 1;;j<<=1){
if(++m == n) break;
for(int i = 0;i<j;i++) y[i] = n-j+i;
for(int i = 0,k=j;i<n;i++) if(sa[i] >= j) y[k++] = sa[i]-j;
for(int i = 0;i<n;i++) x[i] = rk[y[i]];
for(int i = 0;i<m;i++) cnt[i] = 0;
for(int i = 0;i<n;i++) cnt[x[i]]++;
for(int i = 1;i<m;i++) cnt[i] += cnt[i-1];
for(int i = n-1;~i;--i) sa[--cnt[x[i]]] = y[i],y[i] = rk[i];
m = rk[sa[0]] = 0;
for(int i = 1;i<n;i++) rk[sa[i]] =(y[sa[i]]!=y[sa[i-1]]||y[sa[i]+j]!=y[sa[i-1]+j])?++m:m;
}
}
template<typename T>
int mapCharToInt(int n,const T *str){
int m = *max_element(str,str+n);
for(int i = 0;i<=m;i++) rk[i] = 0;
for(int i = 0;i<n;i++) rk[(int)(str[i])] = 1;
for(int i = 1;i<=m;i++) rk[i] += rk[i-1];
for(int i = 0;i<n;i++) s[i] = rk[(int)(str[i])]-1;
return rk[m];
}
void getheight(int n){
for(int i = 0,h = ht[0] = 0;i<n;i++){
int j = sa[rk[i]-1];
while(i+h<n&&j+h<n&&s[i+h]==s[j+h]) h++;
if(ht[rk[i]] = h) h--;
}
}
void build(int n,char *str){
int m = mapCharToInt(++n,str);
get_sa(n,m);
getheight(n);
}
}
namespace BCJ{
int f[MAXN],siz[MAXN];
ll maxn[MAXN],minn[MAXN];
void init(int n,ll * val){
for(int i = 0;i<=n;i++){
f[i] = i,siz[i] = 1;
maxn[i] = minn[i] = val[i];
}
}
int find(int x){
return f[x] == x?x:f[x] = find(f[x]);
}
bool same(int x,int y){
return find(x) == find(y);
}
ll unite(int x,int y,ll &ans){
int fx = find(x),fy = find(y);
if(fx == fy) return 0;
ans = max(ans,maxn[fx]*maxn[fy]);
ans = max(ans,minn[fx]*minn[fy]);
f[fy] = fx;
minn[fx] = min(minn[fx],minn[fy]);
maxn[fx] = max(maxn[fx],maxn[fy]);
ll res = 1LL*siz[fx] * siz[fy];
siz[fx] += siz[fy];
return res;
}
}
bool cmp(int a,int b){
return SA::ht[a] > SA::ht[b];
}
char str[MAXN];
int n,m;
ll a[MAXN],ans[MAXN],cnt[MAXN];
void init(){
static ll min1 = inf,min2 = inf,max1 = -inf,max2 = -inf;
read(n);
read(str);
str[n] = 'a'-1;
for(int i = 1;i<=n;i++){
read(a[i]);
if(a[i] < min1) min1=a[i];
else if(a[i] < min2) min2=a[i];
if(a[i] > max1) max1=a[i];
else if(a[i] > max2) max2=a[i];
ans[i] = -1LL*inf*inf;
}
ans[0] = max(max1*max2,min1*min2);
}
void solve(){
static int h[MAXN];
SA::build(n,str);
BCJ::init(n,a);
for(int i = 1;i<=n;i++) h[i] = i;
sort(h+1,h+n+1,cmp);
for(int i = 1;i<=n;i++){
int x = h[i],ht = SA::ht[x];
if(x!=1) cnt[ht] += BCJ::unite(SA::sa[x]+1,SA::sa[x-1]+1,ans[ht]);
}
for(int i = n-2;i>=0;i--){
cnt[i] += cnt[i+1];
ans[i] = max(ans[i],ans[i+1]);
}
cnt[0] = 1LL*n*(n-1)/2;
for(int i = 0;i<n;i++){
if(!cnt[i]) ans[i] = 0;
print(cnt[i]),print(' '),print(ans[i]),print('\n');
}
}
signed main(){
init();
solve();
flush();
return 0;
}
|