D. Non-Palindromic Substring

onlyblues發表於2024-04-04

D. Non-Palindromic Substring

A string $t$ is said to be $k$-good if there exists at least one substring$^\dagger$ of length $k$ which is not a palindrome$^\ddagger$. Let $f(t)$ denote the sum of all values of $k$ such that the string $t$ is $k$-good.

You are given a string $s$ of length $n$. You will have to answer $q$ of the following queries:

  • Given $l$ and $r$ ($l < r$), find the value of $f(s_ls_{l + 1}\ldots s_r)$.

$^\dagger$ A substring of a string $z$ is a contiguous segment of characters from $z$. For example, "$\mathtt{defor}$", "$\mathtt{code}$" and "$\mathtt{o}$" are all substrings of "$\mathtt{codeforces}$" while "$\mathtt{codes}$" and "$\mathtt{aaa}$" are not.

$^\ddagger$ A palindrome is a string that reads the same backwards as forwards. For example, the strings "$\texttt{z}$", "$\texttt{aa}$" and "$\texttt{tacocat}$" are palindromes while "$\texttt{codeforces}$" and "$\texttt{ab}$" are not.

Input

Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers $n$ and $q$ ($2 \le n \le 2 \cdot 10^5, 1 \le q \le 2 \cdot 10^5$), the size of the string and the number of queries respectively.

The second line of each test case contains the string $s$. It is guaranteed the string $s$ only contains lowercase English characters.

The next $q$ lines each contain two integers, $l$ and $r$ ($1 \le l < r \le n$).

It is guaranteed the sum of $n$ and the sum of $q$ both do not exceed $2 \cdot 10^5$.

Output

For each query, output $f(s_ls_{l + 1}\ldots s_r)$.

Example

input

5
4 4
aaab
1 4
1 3
3 4
2 4
3 2
abc
1 3
1 2
5 4
pqpcc
1 5
4 5
1 3
2 4
2 1
aa
1 2
12 1
steponnopets
1 12

output

9
0
2
5
5
2
14
0
2
5
0
65

Note

In the first query of the first test case, the string is $\mathtt{aaab}$. $\mathtt{aaab}$, $\mathtt{aab}$ and $\mathtt{ab}$ are all substrings that are not palindromes, and they have lengths $4$, $3$ and $2$ respectively. Thus, the string is $2$-good, $3$-good and $4$-good. Hence, $f(\mathtt{aaab}) = 2 + 3 + 4 = 9$.

In the second query of the first test case, the string is $\mathtt{aaa}$. There are no non-palindromic substrings. Hence, $f(\mathtt{aaa}) = 0$.

In the first query of the second test case, the string is $\mathtt{abc}$. $\mathtt{ab}$, $\mathtt{bc}$ and $\mathtt{abc}$ are all substrings that are not palindromes, and they have lengths $2$, $2$ and $3$ respectively. Thus, the string is $2$-good and $3$-good. Hence, $f(\mathtt{abc}) = 2 + 3 = 5$. Note that even though there are $2$ non-palindromic substrings of length $2$, we count it only once.

解題思路

  為了方便規定字串的下標從 $0$ 開始,詢問的 $l$ 和 $r$ 也相應的對映到 $0 \sim n-1$ 範圍內。

  對於詢問 $l$ 和 $r$,子串的長度為 $\text{len} = r-l+1$。先考慮 $k \in [2, len-1]$ 的情況。命題存在長度為 $k$ 的非迴文串,對應的反命題就是不存在長度為 $k$ 的非迴文串,等價於所有長度為 $k$ 的子串都是迴文串。如果 $k$ 能作為答案,意味著原命題要成立,反命題不成立;否則反命題成立。所以我們現在要驗證反命題是否成立(反命題會比原命題好驗證)。

  如果所有長度為 $k$ 的子串都是迴文串,如果 $k$ 是奇數,可以推出 $s_{l} = s_{l+2} = s_{l+4} = \cdots$,$s_{l+1} = s_{l+3} = s_{l+5} = \cdots$,即下標奇偶性相同的字元都相同。此時反命題成立,所有奇數的 $k$ 都無法作為答案。如果 $k$ 是偶數,可以推出 $s_{l} = s_{l+1} = \cdots = s_{r}$,即所有字元都相同,此時反命題成立,所有偶數 $k$ 都無法作為答案。

  要判斷上述兩種情況是否成立,只需預處理出 $f(i)$ 表示左邊所有下標中第一個與 $s_i$ 不同的下標,$g(i)$ 表示左邊所有與 $i$ 奇偶性相同的下標中第一個與 $s_i$ 不同的下標。如果 $f(r) < l$ 說明第二種情況成立,否則假設不超過 $\text{len}-1$ 的最大偶數為 $t = \text{len} - 1 - (\text{len} - 1 \bmod 2)$,答案加上 $2 + 4 + \cdots + t = \frac{t}{2} \left( \frac{t}{2}+1 \right)$。如果 $g(r) < l$ 且 $g(r-1) < l$ 說明第一種情況成立,否則假設不超過 $\text{len}-1$ 的最大奇數為 $t = \text{len} - 1 - (\text{len} \bmod 2)$,答案加上 $3 + 5 + \cdots + t = \left( \frac{t+1}{2} \right)^2-1$。

  剩下就是 $k=1$ 和 $k=\text{len}$ 的情況。顯然 $k=1$ 不可能作為答案,因為長度為 $1$ 的串必定是迴文串。而長度為 $\text{len}$ 的整個串則需要單獨判斷是否為迴文串,可以用 Manacher 演算法(字串雜湊貌似會被卡自然溢位,或者用雙雜湊也可以)。

  AC 程式碼如下,時間複雜度為 $O(n+m)$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 2e5 + 5, M = N * 2;

int n, m;
char s[N], t[M];
int d[M];
int f[N], g[N];

void manacher() {
    int m = 0;
    t[m++] = '#';
    for (int i = 0; i < n; i++) {
        t[m++] = s[i];
        t[m++] = '#';
    }
    for (int i = 0, j = 0; i < m; i++) {
        if (i < j + d[j]) d[i] = min(d[2 * j - i], j + d[j] - i);
        else d[i] = 1;
        while (i - d[i] >= 0 && i + d[i] < m && t[i - d[i]] == t[i + d[i]]) {
            d[i]++;
        }
        if (i + d[i] > j + d[j]) j = i;
    }
}

void solve() {
    scanf("%d %d %s", &n, &m, s);
    for (int i = 0; i < n; i++) {
        if (i - 1 >= 0 && s[i] == s[i - 1]) f[i] = f[i - 1];
        else f[i] = i - 1;
        if (i - 2 >= 0 && s[i] == s[i - 2]) g[i] = g[i - 2];
        else g[i] = i - 2;
    }
    manacher();
    while (m--) {
        int l, r;
        scanf("%d %d", &l, &r);
        l--, r--;
        LL ret = 0;
        if (f[r] >= l) {
            LL t = r - l - (r - l) % 2;
            ret += t * (t + 2) / 4;
        }
        if (g[r] >= l || g[r - 1] >= l) {
            LL t = r - l - (r - l + 1) % 2;
            ret += (t + 1 >> 1) * (t + 1 >> 1) - 1;
        }
        if (d[l + r + 1] <= r - l + 1) ret += r - l + 1;
        printf("%lld\n", ret);
    }
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

參考資料

  Codeforces Round #934 (Div1, Div2) Editorial:https://codeforces.com/blog/entry/127195