luogu#P8304. [CoE R4 D] 01 串

    ID: 7142 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>贪心线段树洛谷原创O2优化前缀和洛谷月赛

[CoE R4 D] 01 串

Problem Description

Define a good 0101 string S\mathcal{S} that satisfies the following conditions:

  • S\mathcal{S} is non-empty.

  • For any prefix S[1p](p[1,S])\mathcal {S} [1\dots p] (p \in [1,|\mathcal S|]), the number of 00 is not greater than the number of 11.

  • For any suffix $\mathcal S[p\dots |\mathcal{S}|] (p \in [1,|\mathcal S|])$, the number of 00 is not greater than the number of 11.

Now you are given a 0101 string T\mathcal{T} of length nn. There are qq queries. In each query, you are given a pair l,rl, r. Find the length of the longest good 0101 subsequence in T[lr]\mathcal{T}[l\dots r]. If there is no good 0101 subsequence, output 1-1.

Note: A subsequence is a new sequence formed by removing some elements without changing the relative order of the remaining elements.

Input Format

The first line contains two integers n,qn, q, representing the length of the 0101 string and the number of queries.

The second line contains a 0101 string of length nn, representing T\mathcal{T}.

The next qq lines each contain two integers l,rl, r, representing one query.

Output Format

Output qq lines. Each line contains one integer, representing the answer to each query in order.

10 4
0100101011
1 1
1 5
2 9
1 10
-1
3
7
8

Hint

Sample Explanation

In the first query, the queried string is 00. No subsequence is good, so the answer is 1-1.

In the second query, the queried string is 0100101001. The subsequence 101101 is good and is the longest, so the answer is 33.

In the third query, the queried string is 1001010110010101. The subsequence 10101011010101 is good and is the longest, so the answer is 77.

In the fourth query, the queried string is 01001010110100101011. The subsequence 1010101110101011 is good and is the longest, so the answer is 88.


Constraints

This problem uses bundled testdata.

Subtask Score nn \le qq \le
11 1010
22 2020 20002000
33 3030 8×1048\times 10^4
44 1010 10510^5 11
55 3030 5×1055\times 10^5

For 100%100\% of the testdata, 1lrn5×1051 \leq l \leq r \leq n \leq 5 \times 10^5, and 1q5×1051 \leq q \leq 5 \times 10^5.

Translated by ChatGPT 5