#abc381e. E - 11/22 Subsequence

E - 11/22 Subsequence

当前没有测试数据。

Score : 500500 points

问题陈述

这个问题中11/22字符串的定义与问题A和C中的定义相同。

如果一个字符串TT满足以下所有条件,则称其为11/22字符串

  • T|T|是奇数。这里,T|T|表示TT的长度。
  • 第1个到第(T+121)(\frac{|T|+1}{2} - 1)个字符都是1
  • (T+12)(\frac{|T|+1}{2})个字符是/
  • (T+12+1)(\frac{|T|+1}{2} + 1)个到第T|T|个字符都是2

例如,11/22111/222/是11/22字符串,但11221/2211/222222/11//2/2/211不是。

给定一个由12/组成的长度为NN的字符串SS,处理QQ个查询。

每个查询提供两个整数LLRR。设TT是从SS的第LL个到第RR个字符的**(连续的)子串。找出TT中一个子序列(不一定是连续的)**的最大长度,该子序列是一个11/22字符串。如果不存在这样的子序列,则打印0

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

The definition of an 11/22 string in this problem is the same as in Problems A and C.

A string TT is called an 11/22 string when it satisfies all of the following conditions:

  • T|T| is odd. Here, T|T| denotes the length of TT.
  • The 11-st through (T+121)(\frac{|T|+1}{2} - 1)-th characters are all 1.
  • The (T+12)(\frac{|T|+1}{2})-th character is /.
  • The (T+12+1)(\frac{|T|+1}{2} + 1)-th through T|T|-th characters are all 2.

For example, 11/22, 111/222, and / are 11/22 strings, but 1122, 1/22, 11/2222, 22/11, and //2/2/211 are not.

Given a string SS of length NN consisting of 1, 2, and /, process QQ queries.

Each query provides two integers LL and RR. Let TT be the (contiguous) substring of SS from the LL-th through RR-th character. Find the maximum length of a subsequence (not necessarily contiguous) of TT that is an 11/22 string. If no such subsequence exists, print 0.

Constraints

  • 1N1051 \leq N \leq 10^5
  • 1Q1051 \leq Q \leq 10^5
  • SS is a string of length NN consisting of 1, 2, and /.
  • 1LRN1 \leq L \leq R \leq N
  • NN, QQ, LL, and RR are integers.

Input

The input is given from Standard Input in the following format. Here, queryi\mathrm{query}_i denotes the ii-th query.

NN QQ

SS

query1\mathrm{query}_1

query2\mathrm{query}_2

\vdots

queryQ\mathrm{query}_Q

Each query is given in the following format:

LL RR

Output

Print QQ lines. The ii-th line should contain the answer to the ii-th query.

Sample Input 1

12 5
111/212/1122
1 7
9 12
3 6
4 10
1 12

Sample Output 1

5
0
3
1
7

For the first query, the substring from the 11-st to 77-th character of SS is 111/212. This string contains 11/22 as a subsequence, which is the longest subsequence that is an 11/22 string. Therefore, the answer is 55.
For the second query, the substring from the 99-th to 1212-th character of SS is 1122. This string does not contain any subsequence that is an 11/22 string, so the answer is 00.