#P8006. String Rearrangement in Phantom

String Rearrangement in Phantom

Description

Given a string SS, each query provides two substrings S[l1,r1]S[l_1, r_1] and S[l2,r2]S[l_2, r_2]. You need to count how many ways to split S[l1,r1]S[l_1, r_1] such that S[l1,r1]=A+B+CS[l_1, r_1] = A + B + C and C+BR+A=S[l2,r2]C + B^R + A = S[l_2, r_2]. Here, BRB^R means the reverse of string BB, ++ means string concatenation, and S[l,r]S[l, r] means taking the ll-th to the rr-th characters of SS. Strings AA, BB, and CC can be empty.

Input Format

The first line contains two positive integers nn and mm, representing the length of SS and the number of queries.

The second line contains a string of length nn consisting of lowercase letters, representing SS.

The next mm lines each contain four positive integers l1,r1,l2,r2l_1, r_1, l_2, r_2, representing the query intervals. It is guaranteed that r1l1=r2l2r_1 - l_1 = r_2 - l_2.

Output Format

Output mm lines, each containing one positive integer, representing the answer.

10 4
aabbaabbaa
1 6 5 10
3 6 5 8
1 3 5 7
1 10 1 10
11
9
2
17

Hint

This problem uses bundled testdata.

Subtask ID Score Special Constraints
00 1010 1n,m1001\le n,m\le 100
11 1n,m5001\le n,m\le 500
22 2020 1n,m30001\le n,m\le 3000
33 3030 1n,m5×1041\le n,m\le 5\times 10^4
44 1n2×1051\le n\le 2\times 10^51m1051\le m\le 10^5

For all testdata, it is guaranteed that 1n2×1051\le n\le 2\times 10^5, 1m1051\le m\le 10^5, 1l1r1n1\le l_1\le r_1\le n, 1l2r2n1\le l_2\le r_2\le n, and r1l1=r2l2r_1 - l_1 = r_2 - l_2.

For Subtask ii, the time limit is i+1i + 1 seconds.

Translated by ChatGPT 5