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. CString转换成char *字符串问题

    buf = (LPSTR)(LPCTSTR)str;      ==>     buf 显示的是第一个字符 strcpy(pNumber,strNumber);      ==>    e ...

  2. AndroidCityPicker仿IOS选择效果

    近期的一个项目由于android端与IOS端须要同步,所以在城市选择器这里做了一个相似IOS的CityPicker控件,当然由于本人水平问题显示效果比IOS上面还是有一定差距的.OK先让大家看下效果. ...

  3. 2016/06/02 网摘记录 svn 服务器端 客户端 安装使用

    http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2408089.html http://www.cnblogs.com/xiaobaihom ...

  4. HDFS集群安装部署

    准备环境: 三台centos7虚拟机(Node-1,Node-2,Node-3) 配置虚拟机网络,保证三台机器可以互相ping通,并且和宿主机可以互相ping通.如果仅仅是作为虚拟机学习,可以关闭防火 ...

  5. rails使用mysql数据库

    简单步骤 1,安装mysql 安裝 MySQL Ubuntu 上安裝 MySQL 請執行: $ sudo apt-get install mysql-server mysql-common mysql ...

  6. poj 1860 Currency Exchange 解题报告

    题目链接:http://poj.org/problem?id=1860 题目意思:给出 N 种 currency, M种兑换方式,Nick 拥有的的currency 编号S 以及他的具体的curren ...

  7. myeclipse8.6注册码

     loveyLR8ZC-855550-69545856608357821   

  8. Xcode清楚缓存、清理多余证书路径

    Xcode清除缓存.清理多余证书 1.删除Xcode中多余的证书provisioning profile 手动删除: Xcode6 provisioning profile path: ~/Libra ...

  9. 压缩&&解压

    压缩与解压缩: ############################################################# tar xvf wordpress.tar       ## ...

  10. sublime 插件:Emmet

    Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...