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. (46)C#注册表及读写

    启动注册表:regedit 结构: 注册表一共有7个配置单元用regedit只能看到5个 HKEY_CLASSES_ROOT 包含系统上文件类型的细节(.txt,.doc)等.以及使用那些应用程序可以 ...

  2. P1536 村村通 洛谷

    https://www.luogu.org/problem/show?pid=1536 题目描述 某市调查城镇交通状况,得到现有城镇道路统计表.表中列出了每条道路直接连通的城镇.市政府“村村通工程”的 ...

  3. linux下查看cpu使用情况

    1.在终端输入top 2.ubuntu系统自带有system monitor 3.sudo apt-get install sysstat 然后在终端输入:mpstat

  4. 《深入理解mybatis原理》 MyBatis的一级缓存实现详解 及使用注意事项

    MyBatis是一个简单,小巧但功能非常强大的ORM开源框架,它的功能强大也体现在它的缓存机制上.MyBatis提供了一级缓存.二级缓存 这两个缓存机制,能够很好地处理和维护缓存,以提高系统的性能.本 ...

  5. Spring的IoC容器概述

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/ioc-containers.html: IoC容器 Spring容器是Spring框架的核心.容器 ...

  6. 如何删除xcode启动主页面项目列表

    Open Xcode, leave the splash screen up and choose "File", "Open Recent Projects" ...

  7. sshd登录攻击

    先说简单的防范措施: 1.密码足够复杂 密码的长度大于8位.有数字.大小写字母.特殊字符组合. 2.nmap 扫描 为了避免被扫描到, #看到端口是81 ssh root@192.168.1.63 玩 ...

  8. SQL数据库 更改数据类型

    向表中添加数据 alter table 表名 add 列名 类型 更改表中列的数据类型 alter table 表名 alter column 列名 类型 删除表中的指定列 alter table 表 ...

  9. 5. TCP客户/服务器程序示例

    signal 信号是一种软件中断,异步发生,在进程运行的时候随时可能发生.信号可以: 由一个进程发给另一个进程,或发给自身 由内核发给某个进程 信号的action: signal handler,在信 ...

  10. CString转换成char *字符串问题

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