PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000200. Dãy con 11/22 (11/22 Subsequence)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Dãy con 11/22 (11/22 Subsequence)

11/22 Subsequence

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

A string SS has NN characters from 1, 2, /, indexed from 11 to NN. A valid string consists of tt ones, one slash and tt twos in that order, where t≥0t\ge 0. For each query (L,R)(L,R), delete any characters of the inclusive substring without reordering the survivors. Find the maximum valid length, or 00 if impossible.

Input

The first line contains integers N,QN,Q, the string length and query count. The second line contains SS. Each of the next QQ lines contains integers L,RL,R.

Output

Print QQ lines, one answer per query in input order.

Subtasks

  • Subtask 1 (20 points): 1≤N,Q≤1001\le N,Q\le 100; ∣S∣=N|S|=N; SS uses only 1, 2, /; 1≤L≤R≤N1\le L\le R\le N.
  • Subtask 2 (30 points): 1≤N,Q≤20001\le N,Q\le 2000; ∣S∣=N|S|=N; SS uses only 1, 2, /; 1≤L≤R≤N1\le L\le R\le N.
  • Subtask 3 (50 points): 1≤N,Q≤1000001\le N,Q\le 100000; ∣S∣=N|S|=N; SS uses only 1, 2, /; 1≤L≤R≤N1\le L\le R\le N.

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.