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. Golang 现有的哲学中,要求你尽量手工处理所有的错误返回

    更优雅的 Golang 错误处理 - Go语言中文网 - Golang中文社区 https://studygolang.com/articles/9407

  2. 正则_action

    http://wiki.ubuntu.org.cn/index.php?title=Python%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%93% ...

  3. ubuntu 本地和服务器scp文件传输

    安装 SSH(Secure Shell) 服务以提供远程管理服务 sudo apt-get install ssh SSH 远程登入 Ubuntu 机 ssh username@192.168.0.1 ...

  4. javascript event事件兼容性处理

    ie 6-8支持event事件,ff浏览器不支持 获取鼠标点击位置的坐标 document.onclick = function(){ alert(event.clientX +"-&quo ...

  5. ajax访问json文件缓存问题

    ajax访问json文件,json文件改动,访问的时候也不能及时看到改动后的内容. 这是因为浏览器缓存的原因. 在这时候就需要清除浏览器的缓存或者加上一个标记,让ajax访问文件的时候知道这是一个新的 ...

  6. mysql优化-------Myisam与innodb引擎,索引文件的区别

    Myisam与innodb引擎,索引文件的区别: innodb的次索引指向对主键的引用. myisam的次索引和主索引都指向物理行. myisam一行一行的插入,会产生一行一行的文件,磁盘上有数据文件 ...

  7. HDU3294 Girls' research —— Manacher算法 输出解

    题目链接:https://vjudge.net/problem/HDU-3294 Girls' research Time Limit: 3000/1000 MS (Java/Others)    M ...

  8. string interpolation in sql server

    https://sqlserver.dev129.com/2018/01/29/string-interpolation-in-t-sql/ Most programming languages ha ...

  9. poj 1195 Mobile phones 解题报告

    题目链接:http://poj.org/problem?id=1195 题目意思:有一部 mobie phone 基站,它的面积被分成一个个小正方形(1 * 1 的大小),所有的小正方形的面积构成了一 ...

  10. I.MX6 sdio 设备注册及识别

    /************************************************************************* * I.MX6 sdio 设备注册及识别 * 说明 ...