\(\color{#0066ff}{ 题目描述 }\)

\(lcp(i,j)\)表示i这个后缀和j这个后缀的最长公共前缀长度

给定一个字符串,每次询问的时候给出两个正整数集合\(A\)\(B\),求

\(\sum_{i \in A,j \in B}\)\(lcp(i,j)\) 的值

\(\color{#0066ff}{输入格式}\)

第一行两个整数n,m,字符串长度和询问个数

接下来一行为长度为n的字符串

接下来每一组询问,第一行两个整数,为A,B集合大小

接下来两行分别为A,B的元素

\(\color{#0066ff}{输出格式}\)

对于每个询问输出ans

\(\color{#0066ff}{输入样例}\)

7 4
abacaba
2 2
1 2
1 2
3 1
1 2 3
7
1 7
1
1 2 3 4 5 6 7
2 2
1 5
1 5

\(\color{#0066ff}{输出样例}\)

13
2
12
16

\(\color{#0066ff}{数据范围与提示}\)

\(1 \le n, q \le 2 \cdot 10^5, \sum\limits_{i = 1}^{i = q}{k_i} \le 2 \cdot 10^5 ,\sum\limits_{i = 1}^{i = q}{l_i} \le 2 \cdot 10^5\)

\(\color{#0066ff}{ 题解 }\)

首先跑一遍SA

然后问题转为,给你一个序列h

每次询问给你两个集合, 问两个集合的元素任意出一个组成区间的min之和

暴力显然是倍增rmq然后\(O(N^2)\)枚举的

不难发现,对于确定的一个 \(b_j\),随着 \(a_i\) 的增大,\(lcp(b_j,\ a_i)\) 是单调不递增的。

因此,我们枚举 \(a_i\),分为 2 种情况。\(\forall\ b_j \in (a_{i-1},\ a_i]\),它们在之前没有计算过贡献,这些可以rmqO(1)求。\(\forall\ b_j \leqslant a_{i-1}\),它们在 \(a_{i-1}\) 的时候就已经有贡献了,让这些贡献都与$a_{i-1}到a_i 的min取min, 即可

这东西就可以用权值线段树维护了

同理,可以求出 \(b_j \geqslant a_i\),把重复的 \(b_j= a_i\) 的贡献减掉就行了。

#include <bits/stdc++.h>

typedef long long LL;

LL in() {
    LL x = 0, f = 1; char ch;
    while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
    for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
    return x * f;
}

const int maxn = 2e5 + 100;
const int inf = 0x7fffffff;

int n, m, T;
int H[maxn], c[maxn], x[maxn], y[maxn], sa[maxn], rk[maxn];
LL l[maxn], r[maxn];
char s[maxn];
int fuck1[maxn], fuck2[maxn], lg[maxn];
int f[maxn][25];

struct Tree {
protected:
    struct node {
        node *ch[2];
        int siz, tag, l, r;
        LL val;
        node(int siz = 0, int tag = 0, int l = 0, int r = 0, LL val = 0): siz(siz), tag(tag), l(l), r(r), val(val) {}
        void upd() { 
            siz = ch[0]->siz + ch[1]->siz;
            val = ch[0]->val + ch[1]->val;
        }
        void clr() { siz = val = 0, tag = 1; }
        void dwn() {
            if(!tag) return;
            ch[0]->clr(), ch[1]->clr();
            tag = 0;
        }
    }*root;
    void build(node *&o, int l, int r) {
        o = new node(0, 0, l, r);
        if(l == r) return;
        int mid = (l + r) >> 1;
        build(o->ch[0], l, mid);
        build(o->ch[1], mid + 1, r);
    }
    void modify(node *o, int p, int x) {
        if(!p) return;
        if(o->r < p || o->l > p) return;
        if(o->l == o->r) return(void)(o->siz += x, o->val += 1LL * p * x);
        o->dwn();
        modify(o->ch[0], p, x);
        modify(o->ch[1], p, x);
        o->upd();
    }
    LL query(node *o, int l, int r) {
        if(o->r < l || o->l > r || l > r) return 0;
        if(l <= o->l && o->r <= r) {
            LL tmp = o->siz;
            o->clr();
            return tmp;
        }
        o->dwn();
        LL ans = query(o->ch[0], l, r) + query(o->ch[1], l, r);
        return o->upd(), ans;
    }
public:
    void build(int len) { build(root, 1, len); }
    void lazy(int p, int x) { modify(root, p, x); }
    LL query(int l, int r) { return query(root, l, r); }
    void clr() { root->val = 0, root->siz = 0, root->tag = 1; }
    LL getans() { return root->val; }
}t;
void predoit() {
    m = 122;
    for(int i = 1; i <= n; i++) c[x[i] = s[i]]++;
    for(int i = 1; i <= m; i++) c[i] += c[i - 1];
    for(int i = n; i >= 1; i--) sa[c[x[i]]--] = i;
    for(int k = 1; k <= n; k <<= 1) {
        int num = 0;
        for(int i = n - k + 1; i <= n; i++) y[++num] = i;
        for(int i = 1; i <= n; i++) if(sa[i] > k) y[++num] = sa[i] - k;
        for(int i = 1; i <= m; i++) c[i] = 0;
        for(int i = 1; i <= n; i++) c[x[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], y[i] = 0;
        std::swap(x ,y);
        x[sa[1]] = 1, num = 1;
        for(int i = 2; i <= n; i++) 
            x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k])? num : ++num;
        if(num == n) break;
        m = num;
    }
    for(int i = 1; i <= n; i++) rk[i] = x[i];
    int h = 0;
    for(int i = 1; i <= n; i++) {
        if(rk[i] == 1) continue;
        if(h) h--;
        int j = sa[rk[i] - 1];
        while(j + h <= n && s[i + h] == s[j + h]) h++;
        H[rk[i]] = h;
    }
}

void work_contribute() {
    lg[0] = -1;
    for(int i = 1; i <= n; i++) f[i][0] = H[i], lg[i] = lg[i >> 1] + 1;
    for(int j = 1; j <= 20; j++)
        for(int i = 1; i + (1 << j) - 1 <= n; i++) 
            f[i][j] = std::min(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
}
int getans(int l, int r) {
    if(l > r) std::swap(l, r);
    else if(l == r) return n - sa[l] + 1;
    else l++;
    int len = lg[r - l + 1];
    return std::min(f[l][len], f[r - (1 << len) + 1][len]);
}


void query() {
    LL ans = 0;
    int nnn = in(), mmm = in();
    for(int i = 1; i <= nnn; i++) fuck1[i] = rk[in()];
    for(int i = 1; i <= mmm; i++) fuck2[i] = rk[in()];
    std::sort(fuck1 + 1, fuck1 + nnn + 1);
    std::sort(fuck2 + 1, fuck2 + mmm + 1);
    for(int i = 1, j = 1; i <= nnn; i++) {
        if(i == 1) t.clr();
        else {
            int lcp = getans(fuck1[i - 1], fuck1[i]), tmp = 0;
            tmp = t.query(lcp + 1, n);
            if(lcp) t.lazy(lcp, tmp);
        }
        while(j <= mmm && fuck2[j] <= fuck1[i]) t.lazy(getans(fuck2[j], fuck1[i]), 1), j++;
        ans += t.getans();
    }
    for(int i = 1, j = 1; i <= mmm; i++) {
        if(i == 1) t.clr();
        else {
            int lcp = getans(fuck2[i - 1], fuck2[i]), tmp = 0;
            tmp = t.query(lcp + 1, n);
            if(lcp) t.lazy(lcp, tmp);
        }
        while(j <= nnn && fuck1[j] < fuck2[i]) t.lazy(getans(fuck1[j], fuck2[i]), 1), j++;
        while(j <= nnn && fuck1[j] <= fuck2[i]) t.lazy(getans(fuck1[j], fuck2[i]), 1), ans -= getans(fuck1[j], fuck2[i]), j++; 
        ans += t.getans();
    }	
    printf("%lld\n", ans);
}
                
int main() {

    n = in(), T = in();
    t.build(n);
    scanf("%s", s + 1);
    predoit();
    work_contribute();
    while(T --> 0) query();
    return 0;
}
----olinr