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. 快速掌握RabbitMQ(四)——两种消费模式和QOS的C#实现

    本篇介绍一下RabbitMQ中的消费模式,在前边的所有栗子中我们采用的消费者都是EventingBasicConsumer,其实RabbitMQ中还有其他两种消费模式:BasicGet和QueueBa ...

  2. ELK之Elasticsearch、logstash部署及配置

    ElasticSearch是一个搜索引擎,用来搜索.分析.存储日志; Logstash用来采集日志,把日志解析为json格式交给ElasticSearch; Kibana是一个数据可视化组件,把处理后 ...

  3. Java并发编程,Condition的await和signal等待通知机制

    Condition简介 Object类是Java中所有类的父类, 在线程间实现通信的往往会应用到Object的几个方法: wait(),wait(long timeout),wait(long tim ...

  4. Java全局变量不加修饰符时的访问权限范围

    如上图所示.

  5. 关于ios异步加载图片的几个开源项目

    一.HjCache  原文:http://www.markj.net/hjcache-iphone-image-cache/ 获取 HJCache: HJCache is up on github h ...

  6. ubuntu 添加和删除用户

    Without a home directory sudo useradd myuser With home directory sudo useradd -m myuser Then set the ...

  7. C#中Queue&lt;T&gt;类的使用以及部分方法的源代码分析

    Queue<T>类 表示对象的先进先出集合. 队列在按接收顺序存储消息方面很实用,以便于进行顺序处理. 存储在 Queue,<T> 中的对象在一端插入,从还有一端移除. Que ...

  8. AnimalWindow使用,实现界面动态消失

    http://m.blog.csdn.net/blog/shufac/24932279 http://blog.sina.com.cn/s/blog_455245fc01000a42.html Ani ...

  9. 简谈Java的join()方法(转)

    join()是Thread类的一个方法.根据jdk文档的定义: public final void join()throws InterruptedException: Waits for this ...

  10. 模拟 nbut1225 NEW RDSP MODE I

    传送门:点击打开链接 题意:输入n.m,x.刚開始有一个1~n的排列.然后定义了一种操作.是将数组中的偶数位数字选出来,依照顺序放到数组最前面,奇数位依照顺序放到偶数位的后面,进行m次这种操作.输出之 ...