CF380C.Sereja and Brackets

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Sereja has a bracket sequence s1,s2,...,sns_{1},s_{2},...,s_{n} , or, in other words, a string ss of length nn , consisting of characters "(" and ")".

Sereja needs to answer mm queries, each of them is described by two integers li,ril_{i},r_{i} (1<=li<=ri<=n)(1<=l_{i}<=r_{i}<=n) . The answer to the ii -th query is the length of the maximum correct bracket subsequence of sequence sli,sli+1,...,sris_{li},s_{li}+1,...,s_{ri} . Help Sereja answer all queries.

You can find the definitions for a subsequence and a correct bracket sequence in the notes.
Sereja 有一个括号序列s1,s2,...,sns_{1},s_{2},...,s_{n} ,或者换句话说,一个长度为 n 的字符串 s ,由字符 "(" 和 ")" 组成。

Sereja 需要回答 m 个查询,每个查询由两个整数 li, ri 描述。 li,ril_{i},r_{i} (1<=li<=ri<=n)(1<=l_{i}<=r_{i}<=n) .第 i 个查询的答案是序列 sli,sli+1,...,sris_{li},s_{li}+1,...,s_{ri} 的最大正确括号子序列的长度。帮助 Sereja 回答所有问题。

您可以在注释中找到子序列和正确括号序列的定义。

输入格式

The first line contains a sequence of characters s1,s2,...,sns_{1},s_{2},...,s_{n} (1<=n<=106)(1<=n<=10^{6}) without any spaces. Each character is either a "(" or a ")". The second line contains integer mm (1<=m<=105)(1<=m<=10^{5}) — the number of queries. Each of the next mm lines contains a pair of integers. The ii -th line contains integers li,ril_{i},r_{i} (1<=li<=ri<=n)(1<=l_{i}<=r_{i}<=n) — the description of the ii -th query.
输入
第一行包含字符 s1,s2,...,sns_{1},s_{2},...,s_{n} (1<=n<=106)(1<=n<=10^{6})序列,不含空格。每个字符都是"("或")"。第二行包含整数 mm (1<=m<=105)(1<=m<=10^{5}) - 查询次数。接下来的每行 m 都包含一对整数。第 i 行包含整数 li,ril_{i},r_{i} (1<=li<=ri<=n)(1<=l_{i}<=r_{i}<=n) - i 次查询的描述。

输出格式

Print the answer to each question on a single line. Print the answers in the order they go in the input.
输出
将每个问题的答案打印在一行上。按输入的顺序打印答案。

输入输出样例

  • 输入#1

    ())(())(())(
    7
    1 1
    2 3
    1 2
    1 12
    8 12
    5 11
    2 10
    

    输出#1

    0
    0
    2
    10
    4
    6
    6
    

说明/提示

A subsequence of length x|x| of string s=s1s2... sss=s_{1}s_{2}...\ s_{|s|} (where s|s| is the length of string ss ) is string x=sk1sk2... skxx=s_{k1}s_{k2}...\ s_{k|x|} (1<=k1<k2<...<kx<=s)(1<=k_{1}<k_{2}<...<k_{|x|}<=|s|) .

A correct bracket sequence is a bracket sequence that can be transformed into a correct aryphmetic expression by inserting characters "1" and "+" between the characters of the string. For example, bracket sequences "()()", "(())" are correct (the resulting expressions "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not.

For the third query required sequence will be «()».

For the fourth query required sequence will be «()(())(())».

字符串s=s1s2... sss=s_{1}s_{2}...\ s_{|s|} (其中 |s| 是字符串 s 的长度)的长度为 |x| 的 子序列是字符串x=sk1sk2... skxx=s_{k1}s_{k2}...\ s_{k|x|} (1<=k1<k2<...<kx<=s)(1<=k_{1}<k_{2}<...<k_{|x|}<=|s|)
正确的括号序列是指可以通过在字符串的字符之间插入字符"1"和"+"来转换为正确的重音表达式的括号序列。例如,括号序列"()()", "(())" 是正确的(得到的表达式为"(1)+(1)"、"((1+1)+1)"),而")("和"("则不正确。

第三个查询所需的序列将是"()"。

第四个查询的必填序列是"()()()()) "。

首页