#BS0000200. Dãy con 11/22 (11/22 Subsequence)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Dãy con 11/22 (11/22 Subsequence)
11/22 Subsequence
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
A string has characters from 1, 2, /, indexed from to . A valid string consists of ones, one slash and twos in that order, where . For each query , delete any characters of the inclusive substring without reordering the survivors. Find the maximum valid length, or if impossible.
Input
The first line contains integers , the string length and query count. The second line contains . Each of the next lines contains integers .
Output
Print lines, one answer per query in input order.
Subtasks
- Subtask 1 (20 points): ; ; uses only
1,2,/; . - Subtask 2 (30 points): ; ; uses only
1,2,/; . - Subtask 3 (50 points): ; ; uses only
1,2,/; .
Examples
Input
12 5
111/212/1122
1 7
9 12
3 6
4 10
1 12
Output
5
0
3
1
7
Explanation
For the five intervals, the maximum numbers of matched ones and twos around a slash are respectively 2, nonexistent, 1, 0 and 3. In the first interval the slash at 4 has three ones before it and two twos after it. The second interval has no slash. In [3,6] only one one precedes the slash. In [4,10] the first slash has no preceding one and the second has no following two. In [1,12] the first slash supports three pairs and the second supports only two. Hence the five lengths are 5, 0, 3, 1 and 7.