# BZOJ3879: SvT【后缀数组+单调栈】

## Description

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

7 3

popoqqq

1 4

2 3 5

4 1 2 5 6

## Sample Output

0

0

2

### Hint

#include<bits/stdc++.h>

using namespace std;

typedef pair<int, int> pi;
typedef long long ll;
const int N = 5e5 + 10;
const int M = 3e6 + 10;
const int LOG = 20;
const ll Mod = 23333333333333333;

struct Suffix_Array {
int s[N], n, m;
int c[N], x[N], y[N];
int height[N], sa[N], rank[N];
int st[N][LOG], Log[N];
ll sum[N];

void init(int len, char *c) {
n = len, m = 0;
for (int i = 1; i <= len; i++) {
s[i] = c[i];
m = max(m, s[i]);
}
}

for (int i = 1; i <= m; i++) c[i] = 0;
for (int i = 1; i <= n; i++) c[x[y[i]]]++;
for (int i = 1; i <= m; i++) c[i] += c[i - 1];
for (int i = n; i >= 1; i--) sa[c[x[y[i]]]--] = y[i];
}

void buildsa() {
for (int i = 1; i <= n; i++) x[i] = s[i], y[i] = i;
int now;
for (int k = 1; k <= n; k <<= 1) {
now = 0;
for (int i = n - k + 1; i <= n; i++) y[++now] = i;
for (int i = 1; i <= n; i++) if (sa[i] > k) y[++now] = sa[i] - k;
y[sa[1]] = now = 1;
for (int i = 2; i <= n; i++) y[sa[i]] = (x[sa[i]] == x[sa[i - 1]] && x[sa[i] + k] == x[sa[i - 1] + k]) ? now : ++now;
swap(x, y);
if (now == n) break;
m = now;
}
}

void buildrank() {
for (int i = 1; i <= n; i++) rank[sa[i]] = i;
}

void buildsum() {
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + n - sa[i] + 1 - height[i];
}

void buildheight() {
for (int i = 1; i <= n; i++) if (rank[i] != 1) {
int k = max(height[rank[i - 1]] - 1, 0); // 里面是 rank[i - 1]
for (; s[i + k] == s[sa[rank[i] - 1] + k]; k++);
height[rank[i]] = k; // height 里面是 rank
}
}

void buildst() {
Log[1] = 0;
for (int i = 2; i < N; i++) Log[i] = Log[i >> 1] + 1;
for (int i = 1; i <= n; i++) st[i][0] = height[i];
for (int j = 1; j < LOG; j++) {
for (int i = 1; i + (1 << (j - 1)) <= n; i++) {
st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
}

int queryst(int l, int r) {
if (l == r) return n - sa[l] + 1;
if (l > r) swap(l, r);
++l; //***
int k = Log[r - l + 1];
return min(st[l][k], st[r - (1 << k) + 1][k]);
}

int querylcp(int la, int lb) {
return queryst(rank[la], rank[lb]);
}

void build(int len, char *c) {
init(len, c);
buildsa();
buildrank();
buildheight();
buildsum();
buildst();
}
} Sa;

int n, q, m;
char c[N];
ll ans, sum;
pi p[M];

struct Node {
int num, pos;
ll val;
};
stack<Node> Q;

ll add(ll a, ll b) {
return (a += b) >= Mod ? a - Mod : a;
}

int main() {
#ifdef dream_maker
freopen("input.txt", "r", stdin);
#endif
scanf("%d %d", &n, &q);
scanf("%s", c + 1);
Sa.build(n, c);
while (q--) {
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d", &p[i].second);
p[i].first = Sa.rank[p[i].second];
}
sort(p + 1, p + m + 1);
m = unique(p + 1, p + m + 1) - p - 1;
ans = sum = 0;
while (Q.size()) Q.pop();
Q.push((Node) {1, p[1].second, 0});
for (int i = 2; i <= m; i++) {
int curnum = 1, len = Sa.querylcp(Q.top().pos, p[i].second);
while (Q.size() && Q.top().val >= len) {
curnum += Q.top().num;
sum -= Q.top().val * Q.top().num;
Q.pop();
}
Q.push((Node) {curnum, p[i].second, len});
sum += len * curnum;