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. android DatePicker使用

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  2. android 计时器

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  3. 【面试 spring】【第七篇】spring的问题

    1.spring你熟悉么?两大特色 spring 主要有IOC和AOP两大特色. =========================================================== ...

  4. Python基础语法04-数据结构

    Python Number(数字) Python Number 数据类型用于存储数值. 数据类型是不允许改变的,这就意味着如果改变 Number 数据类型的值,将重新分配内存空间. Python 支持 ...

  5. Linux中修改docker镜像源及安装docker

    1.首先备份系统自带yum源配置文件/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.re ...

  6. eclipse Kepler tomcat内存溢出解决方式

    使用eclipse开发ssh项目,本机8G内存,可是在打开一个表格后再打开一个页面.立即就内存溢出,网上搜到下面解决方式,未解决: 1.改动eclipse.ini參数 -vmargs -Xms1024 ...

  7. 日常方便使用的Python脚本实现

    目录 文件批量重命名 bin文件合并 正文 1.python根据不同条件批量实现文件重命名 因为下载的电视剧名字比较乱,但却按照下载时间顺序依次排列,而手动重命名或者找软件太麻烦,我就自己实现了个: ...

  8. ftk学习记(combox篇)

    [声明:版权全部,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 上一篇文章谈到了多窗体,还是依照约定看一下效果是什么样的. 假设大家细心一点.就会发现窗体中的l ...

  9. Desktop Management Interface & System Management BIOS

    http://en.wikipedia.org/wiki/Desktop_Management_Interface Desktop Management Interface From Wikipedi ...

  10. [Pyhton]weakref 弱引用

    文档中的解释: https://docs.python.org/2/library/weakref.html wiki 中的解释: 在计算机程序设计中,弱引用.与强引用相对.是指不能确保其引用的对象不 ...