#P4094. [HEOI2016/TJOI2016] 字符串

    ID: 3029 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2016线段树二分各省省选河北O2优化RMQ主席树后缀数组,SA天津

[HEOI2016/TJOI2016] 字符串

Description

On Jiayuan’s birthday, her friends bought her a birthday gift from an online store. The gift is placed in a magic box. On the outside of the box there is a string ss of length nn, and mm questions. Jiayuan must answer these mm questions correctly to open the box and get the gift, get promoted and receive a raise, become the CEO, marry a rich and handsome man, and reach the pinnacle of life.

Each question has four parameters a,b,c,da,b,c,d. Among all substrings of s[a..b]s[a..b], what is the maximum possible length of the longest common prefix with s[c..d]s[c..d]? Jiayuan is not good at such problems, so she asks you for help. How would you help her?

Input Format

The first line contains two positive integers n,mn,m, denoting the length of the string and the number of queries.

The next line contains a string of length nn. Then there are mm lines, each containing four integers a,b,c,da,b,c,d, representing a query asking for the maximum length of the longest common prefix between any substring of s[a..b]s[a..b] and s[c..d]s[c..d].

Output Format

For each query, output the answer.

5 5
aaaaa
1 1 1 5
1 5 1 1
2 3 2 3
2 4 2 3
2 3 2 4
1
1
2
2
2

Hint

For 10%10\% of the testdata, 1n,m3001 \le n,m \le 300.

For 40%40\% of the testdata, 1n,m3,0001 \le n,m \le 3{,}000, and the string contains only a, b.

For 100%100\% of the testdata, 1n,m100,0001 \le n,m \le 100{,}000, the string contains only lowercase English letters, aba \le b, cdc \le d, and 1a,b,c,dn1 \le a,b,c,d \le n.

Translated by ChatGPT 5