给出一个长度为 的字符串 和序列

对于r[0,n1]\forall r\in [0, n-1]ij[lcp(sufi,sufj)r]\sum_{i\ne j} [lcp(suf_i, suf_j)\ge r]maxij,lcp(sufi,sufj)rai×aj\max_{i\neq j,lcp(suf_i,suf_j)\ge r} a_i\times a_j

LOJ 2133

Solution

反着建SAM,在parent树上统计以每个点状态lcp的答案

注意除了记maxmax之外还需要记minmin,因为权值可能为负数

Code

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
141
142
143
144
145
146
147
#include <bits/stdc++.h>

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair <int, int> pii;

template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }
template <typename T> inline T read ()
{
T sum = 0, fl = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

inline void proc_status ()
{
ifstream t ("/proc/self/status");
cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) << endl;
}

const int Maxn = 300000 + 100;
const LL inf = 0x3f3f3f3f3f3f3f3f;

int N, A[Maxn];
char S[Maxn];

namespace SAM
{
int node_cnt = 1, last = 1;

struct info
{
int maxlen, fa, ch[27], cnt;
LL min, max;
} node[Maxn << 1];

vector <int> G[Maxn << 1];

inline int new_node (int pre, int id)
{
node[++node_cnt].maxlen = node[pre].maxlen + 1;
node[node_cnt].cnt = 1, node[node_cnt].min = node[node_cnt].max = A[id];
return node_cnt;
}

inline void extend (int c, int id)
{
int now = new_node (last, id), pre = last; last = now;

for (; pre && !node[pre].ch[c]; pre = node[pre].fa) node[pre].ch[c] = now;

if (!pre) node[now].fa = 1;
else
{
int x = node[pre].ch[c];
if (node[x].maxlen == node[pre].maxlen + 1) node[now].fa = x;
else
{
int y = ++node_cnt;
node[y] = node[x], node[y].maxlen = node[pre].maxlen + 1;
node[y].cnt = 0, node[y].min = inf, node[y].max = -inf;
node[x].fa = node[now].fa = y;

for (; node[pre].ch[c] == x; pre = node[pre].fa) node[pre].ch[c] = y;
}
}
}

LL Ans1[Maxn << 1], Ans2[Maxn << 1];

inline void dfs (int x)
{
LL sum = node[x].cnt, ans1 = 0, ans2 = -inf;
LL min_now = node[x].min, max_now = node[x].max;

for (int i = 0; i < G[x].size(); ++i)
{
int y = G[x][i];
dfs (y);

if (sum) Chkmax (ans2, max((LL)min_now * node[y].min, (LL)max_now * node[y].max));
Chkmin (min_now, node[y].min);
Chkmax (max_now, node[y].max);

ans1 += (LL)node[y].cnt * sum;
sum += node[y].cnt;
}

node[x].cnt = sum, node[x].min = min_now, node[x].max = max_now;
Ans1[node[x].maxlen] += ans1;
Chkmax(Ans2[node[x].maxlen], ans2);
}

inline void build (char *s, int n)
{
node[1].min = inf, node[1].max = -inf;
for (int i = 1; i <= n; ++i) extend (s[i] - 'a', i);
}

inline void solve ()
{
for (int i = 1; i <= node_cnt; ++i) G[node[i].fa].pb (i);
for (int i = 0; i <= N; ++i) Ans2[i] = -inf;
dfs (1);
for (int i = N - 1; i >= 0; --i) Ans1[i] += Ans1[i + 1], Chkmax(Ans2[i], Ans2[i + 1]);
for (int i = 0; i < N; ++i) printf("%lld %lld\n", Ans1[i], Ans2[i] == -inf ? 0 : Ans2[i]);
}
}

inline void Solve ()
{
reverse (S + 1, S + N + 1);
reverse (A + 1, A + N + 1);
SAM :: build (S, N);
SAM :: solve ();
}

inline void Input ()
{
N = read<int>();
scanf("%s", S + 1);
for (int i = 1; i <= N; ++i) A[i] = read<int>();
}

int main()
{

#ifndef ONLINE_JUDGE
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
#endif

Input ();
Solve ();

return 0;
}