#x1008. CF1043G Speckled Band

CF1043G Speckled Band

Speckled Band

题面翻译

给你一个长为 nnn2×105n\leq 2\times 10^5)的小写字母字符串 ss,再给 qqq2×105q\leq 2\times 10^5)个询问,每次给一个区间 [l,r][l,r],你需要把 s[l,r]s[l, r] 这个子串划分成若干段,使得至少有两个段是相同的,你需要构造一种分段方式使得划分出的段中本质不同的段最少,你只要求这个最小值。

题目描述

Ildar took a band (a thin strip of cloth) and colored it. Formally, the band has n n cells, each of them is colored into one of 26 26 colors, so we can denote each color with one of the lowercase letters of English alphabet.

Ildar decided to take some segment of the band [l,r] [l, r] ( 1lrn 1 \le l \le r \le n ) he likes and cut it from the band. So he will create a new band that can be represented as a string t=slsl+1sr t = s_l s_{l+1} \ldots s_r .

After that Ildar will play the following game: he cuts the band t t into some new bands and counts the number of different bands among them. Formally, Ildar chooses 1kt 1 \le k \le |t| indexes 1i1<i2<<ik=t 1 \le i_1 < i_2 < \ldots < i_k = |t| and cuts t t to k k bands-strings $ t_1 t_2 \ldots t_{i_1}, t_{i_1 + 1} \ldots t_{i_2}, \ldots, {t_{i_{k-1} + 1}} \ldots t_{i_k} $ and counts the number of different bands among them. He wants to know the minimal possible number of different bands he can get under the constraint that at least one band repeats at least two times. The result of the game is this number. If it is impossible to cut t t in such a way, the result of the game is -1.

Unfortunately Ildar hasn't yet decided which segment he likes, but he has q q segments-candidates [l1,r1] [l_1, r_1] , [l2,r2] [l_2, r_2] , ..., [lq,rq] [l_q, r_q] . Your task is to calculate the result of the game for each of them.

输入格式

The first line contains one integer n n ( 1n200000 1 \le n \le 200\,000 ) — the length of the band Ildar has.

The second line contains a string s s consisting of n n lowercase English letters — the band Ildar has.

The third line contains a single integer q q ( 1q200000 1 \le q \le 200\,000 ) — the number of segments Ildar has chosen as candidates.

Each of the next q q lines contains two integer integers li l_i and ri r_i ( 1lirin 1 \le l_i \le r_i \le n ) denoting the ends of the i i -th segment.

输出格式

Output q q lines, where the i i -th of them should contain the result of the game on the segment [li,ri] [l_i, r_i] .

样例 #1

样例输入 #1

9
abcabcdce
7
1 6
4 7
5 9
6 9
1 9
3 6
4 4

样例输出 #1

1
-1
4
3
2
2
-1

提示

Consider the first example.

If Ildar chooses the segment [1,6] [1, 6] , he cuts a string t=abcabc t = abcabc . If he cuts t t into two bands abc abc and abc abc , the band abc abc repeats two times and the number of different tapes is 1 1 . So, the result of this game is 1 1 .

If Ildar chooses the segment [4,7] [4, 7] , he cuts a string t=abcd t = abcd . It is impossible to cut this band in such a way that there is at least one band repeating at least two times. So, the result of this game is 1 -1 .

If Ildar chooses the segment [3,6] [3, 6] , he cuts a string t=cabc t = cabc . If he cuts t t into three bands c c , ab ab and c c , the band c c repeats two times and the number of different bands is 2 2 . So, the result of this game is 2 2 .