Description

Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length n, consisting of characters "(" and ")".

Sereja needs to answer m queries, each of them is described by two integers li, ri(1 ≤ li ≤ ri ≤ n). The answer to the i-th query is the length of the maximum correct bracket subsequence of sequence sli, sli + 1, ..., sri. Help Sereja answer all queries.

You can find the definitions for a subsequence and a correct bracket sequence in the notes.

Input

The first line contains a sequence of characters s1, s2, ..., sn (1 ≤ n ≤ 106) without any spaces. Each character is either a "(" or a ")". The second line contains integer m (1 ≤ m ≤ 105) — the number of queries. Each of the next m lines contains a pair of integers. The i-th line contains integers li, ri (1 ≤ li ≤ ri ≤ n) — the description of the i-th query.

Output

Print the answer to each question on a single line. Print the answers in the order they go in the input.

Input

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

Output

0
0
2
10
4
6
6

Note

A subsequence of length |x| of string s = s1s2... s|s| (where |s| is the length of string s) is string x = sk1sk2... sk|x| (1 ≤ k1 < k2 < ... < 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 «()(())(())».

解题思路:求连续子串中括号匹配的个数。括号匹配问题一般可以用栈来维护。对于每个右括号,其匹配的左括号是固定的,因此我们可以记录每个右括号匹配的左括号位置,对整个区间进行线扫描,同时用树状数组维护+离散化处理即可。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e6+;
const int maxv=1e5+;
char str[maxn];int n,m,pos,tree[maxn],ans[maxv];stack<int> st;
struct node{int l,r,id;}query[maxn];
bool cmp(node a,node b){return a.r<b.r;}///右端点排序
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void print(int x){
if(x<)putchar('-'),x=-x;
if(x>)print(x/);
putchar(x%+'');
}
int lowbit(int x){return x&-x;}
void add(int x,int val){
for(int i=x;i<=n;i+=lowbit(i))tree[i]+=val;
}
int get_sum(int x){
int ans=;
for(int i=x;i>;i-=lowbit(i))ans+=tree[i];
return ans;
}
int main(){
while(~scanf("%s",str)){
while(!st.empty())st.pop();n=strlen(str);
memset(tree,,sizeof(tree)),memset(ans,,sizeof(ans));m=read();
for(int i=;i<m;++i)query[i].l=read(),query[i].r=read(),query[i].id=i;
sort(query,query+m,cmp);pos=;
for(int i=;i<m;++i){
for(int j=pos;j<=query[i].r;++j){
if(str[j-]=='(')st.push(j);///记录左括号的位置(物理位置)
else if(!st.empty())add(st.top(),),st.pop();///单点更新
}
pos=query[i].r+;///从下一个位置开始,避免重叠,O(n)遍历
ans[query[i].id]=get_sum(query[i].r)-get_sum(query[i].l-);
}
for(int i=;i<m;++i)print(ans[i]),puts("");
}
return ;
}

Sereja and Brackets(括号匹配)的更多相关文章

  1. POJ-2955 Brackets(括号匹配问题)

    题目链接:http://poj.org/problem?id=2955 这题要求求出一段括号序列的最大括号匹配数量 规则如下: the empty sequence is a regular brac ...

  2. POJ 2955 Brackets(括号匹配一)

    题目链接:http://poj.org/problem?id=2955 题目大意:给你一串字符串,求最大的括号匹配数. 解题思路: 设dp[i][j]是[i,j]的最大括号匹配对数. 则得到状态转移方 ...

  3. poj 2955 Brackets 括号匹配 区间dp

    题意:最多有多少括号匹配 思路:区间dp,模板dp,区间合并. 对于a[j]来说: 刚開始的时候,转移方程为dp[i][j]=max(dp[i][j-1],dp[i][k-1]+dp[k][j-1]+ ...

  4. POJ - 2955 Brackets括号匹配(区间dp)

    Brackets We give the following inductive definition of a “regular brackets” sequence: the empty sequ ...

  5. poj 2955 Brackets (区间dp 括号匹配)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  6. CSUOJ 1271 Brackets Sequence 括号匹配

    Description ]. Output For each test case, print how many places there are, into which you insert a ' ...

  7. CodeForces-380C:Sereja and Brackets(线段树与括号序列)

    Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length n, consistin ...

  8. POJ 1141 Brackets Sequence(括号匹配二)

    题目链接:http://poj.org/problem?id=1141 题目大意:给你一串字符串,让你补全括号,要求补得括号最少,并输出补全后的结果. 解题思路: 开始想的是利用相邻子区间,即dp[i ...

  9. CF380C. Sereja and Brackets[线段树 区间合并]

    C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...

随机推荐

  1. setUp() and setUpBeforeClass()

    The @BeforeClass and @AfterClass annotated methods will be run exactly once during your test run - a ...

  2. Mac OS用minikube安装单节点kubernetes

    参考 https://kubernetes.io/docs/tasks/tools/install-minikube/ https://github.com/linianhui/code/blob/m ...

  3. Hadoop提供的reduce函数中Iterable 接口只能遍历一次的问题

    今天在写MapReduce中的reduce函数时,碰到个问题,特此记录一下: void reduce(key, Iterable<*>values,...) { for(* v:value ...

  4. CSS3 Selector

    每个前端工程师可能每天都会写一些css,其中选择器是很主要的一部分.但是,大家可能每天写的大多是#id,.class这样的选择器,这并不稀奇,但是如果我们了解并且熟用css3为我们提供的强大并且优雅的 ...

  5. BZOJ2327: [HNOI2011]勾股定理

    BZOJ2327: [HNOI2011]勾股定理 Description 题解Here! 这是一道神题... 我一开始把题目看错了,我以为是在$n$根木棒中选两个$i,j$满足$gcd(i,j)==1 ...

  6. gson如何转化json数组

    String.JsonObject.JavaBean 互相转换 User user = new Gson().fromJson(jsonObject, User.class); User user = ...

  7. Oracle:热备测试

    我们知道Oracle数据库热备有3步: 1. alter tablespace  tbname  begin backup: 2. cp  /×××      to   /×× 3. alter ta ...

  8. STL Algorithms 之 unique

    C++的文档中说,STL中的unique是类似于这样实现的: template <class ForwardIterator> ForwardIterator unique ( Forwa ...

  9. Linux 命令行命令及参数辨异

    1. 软链接与硬链接 ln -s 源文件 目标文件 当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在某个固定的目录,放上该文件,然后在其它的目 ...

  10. MFC之document与view实践总结

    Document/View是MFC的基石,负责程序数据的管理和显示,Doculent和Viewd的关系有一档一视,一档多视和多档多视,下面将分别对实现过程中的重点知识进行总结. 1. 视图的同步更新 ...