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.

Example

Input
())(())(())(
7
1 1
2 3
1 2
1 12
8 12
5 11
2 10
Output
0
0
2
10
4
6
6

题意:给定由‘(’和‘)’组成的字符串序列,然后Q个询问,每次回答区间最多有多少个匹配的括号。

思路:分治的思想,当前的括号匹配对于左区间的匹配数+右区间的匹配数+min(左区间的未匹配的左括号,右区间的未匹配的右括号),同时改变当前区间未匹配的括号数。

注意:因为有3个参数需要传递,所以用结构体(因为要修改未匹配括号数)。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
char c[maxn];
struct in
{
int a,b,sum;
in(){ a=; b=; sum=;}
}s[maxn<<];
void read(int &res)
{
char c=getchar();res=; for(;c>''||c<'';c=getchar());
for(;c<=''&&c>='';c=getchar()) res=(res<<)+(res<<)+c-'';
} struct segment_tree
{
void pushup(int Now)
{
int tmp=min(s[Now<<].a,s[Now<<|].b);
s[Now].sum=s[Now<<].sum+s[Now<<|].sum+tmp;
s[Now].a=s[Now<<].a+s[Now<<|].a-tmp;
s[Now].b=s[Now<<].b+s[Now<<|].b-tmp;
}
void build(int Now,int L,int R)
{
if(L==R) {
s[Now].a=(c[L]=='('?:);
s[Now].b=(c[L]==')'?:);
s[Now].sum=; return ;
}
int Mid=(L+R)>>;
build(Now<<,L,Mid);
build(Now<<|,Mid+,R);
pushup(Now);
}
in query(int Now,int L,int R,int l,int r)
{
if(l<=L&&r>=R) return s[Now];
int Mid=(L+R)>>; in w,e,res;
if(l<=Mid) w=query(Now<<,L,Mid,l,r);
if(r>Mid) e=query(Now<<|,Mid+,R,l,r);
res.sum=min(w.a,e.b);
res.a=w.a+e.a-res.sum;
res.b=w.b+e.b-res.sum;
res.sum+=w.sum+e.sum;
return res;
}
}Tree;
int main()
{
int N,Q,x,y;
scanf("%s",c+);
N=strlen(c+);
Tree.build(,,N);
read(Q);
while(Q--){
read(x); read(y);
printf("%d\n",*Tree.query(,,N,x,y).sum);
}
return ;
}

CodeForces-380C:Sereja and Brackets(线段树与括号序列)的更多相关文章

  1. Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并

    题目链接:http://codeforces.com/contest/381/problem/E  E. Sereja and Brackets time limit per test 1 secon ...

  2. CodeForces 380C Sereja and Brackets(扫描线+树状数组)

    [题目链接] http://codeforces.com/problemset/problem/380/C [题目大意] 给出一个括号序列,求区间内左右括号匹配的个数. [题解] 我们发现对于每个右括 ...

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

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

  4. bzoj1095: [ZJOI2007]Hide 捉迷藏 线段树维护括号序列 点分治 链分治

    这题真是十分难写啊 不管是点分治还是括号序列都有一堆细节.. 点分治:时空复杂度$O(n\log^2n)$,常数巨大 主要就是3个堆的初始状态 C堆:每个节点一个,为子树中的点到它父亲的距离的堆. B ...

  5. BZOJ 1095 捉迷藏(线段树维护括号序列)

    对于树的一个括号序列,树上两点的距离就是在括号序列中两点之间的括号匹配完之后的括号数... 由此可以得出线段树的做法.. #include<cstdio> #include<iost ...

  6. BZOJ1095: [ZJOI2007]Hide 捉迷藏【线段树维护括号序列】【思维好题】

    Description 捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩 捉迷藏游戏.他们的家很大且构造很奇特,由N个屋子和N-1条 ...

  7. Codeforces Round #603 (Div. 2) E - Editor(线段树,括号序列)

  8. BZOJ 1095: [ZJOI2007]Hide 捉迷藏(线段树维护括号序列)

    这个嘛= =链剖貌似可行,不过好像代码长度很长,懒得打(其实是自己太弱了QAQ)百度了一下才知道有一种高大上的叫括号序列的东西= = 岛娘真是太厉害了,先丢链接:http://www.shuizilo ...

  9. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

随机推荐

  1. codeforces 1041 e 构造

    Codeforces 1041 E 构造题. 给出一种操作,对于一棵树,去掉它的一条边.那么这颗树被分成两个部分,两个部分的分别的最大值就是这次操作的答案. 现在给出一棵树所有操作的结果,问能不能构造 ...

  2. 测试开发系列之Python开发mock接口(二)

    上一篇咱们已经把开发前的环境准备好了,还需要再做一点准备,你的账户信息是存在哪的呢,当然是存在数据库里的,咱们在去支付,扣钱的时候,肯定是从数据库里面操作的,去更新账户表里面的数据,所以咱们先要把数据 ...

  3. HUNAN 11560 Yangyang loves AC(二分+贪心)

    http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11560&courseid=0 题意:总共有n天,每天 ...

  4. PAT (Advanced Level) 1089. Insert or Merge (25)

    简单题.模拟一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...

  5. raspi串口、python串口模块pyserial

    一.安装 1.下载软件包pyserial-2.7.tar.gz   网址:https://pypi.python.org/pypi/pyserial 2.8uftp上传至/usr/local/src/ ...

  6. TYVJ P1577 泥泞的道路

    题目链接:http://www.tyvj.cn/p/1577# P1577 泥泞的道路 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 公园中有n个景点,编号 ...

  7. luogu P1043 数字游戏

    题目描述 丁丁最近沉迷于一个数字游戏之中.这个游戏看似简单,但丁丁在研究了许多天之后却发觉原来在简单的规则下想要赢得这个游戏并不那么容易.游戏是这样的,在你面前有一圈整数(一共n个),你要按顺序将其分 ...

  8. db2 获取自增主键的方法

    1.用SEQUENCES方式 建表语句 CREATE TABLE TEST1( PKEY INTEGER NOT NULL, NAME VARCHAR(100), SEX VARCHAR(100), ...

  9. mysql delete语句不能用别名

    在mysql数据库里运行delete语句 delete ’; 发现会报错: [Err] - You have an error in your SQL syntax; check the manual ...

  10. Python机器学习-分类

    监督学习下的分类模型,主要运用sklearn实践 kNN分类器 决策树 朴素贝叶斯 实战一:预测股市涨跌 # -*- coding: utf-8 -*- """ Crea ...