E. Sereja and Brackets
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
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 «()(())(())».

题意:给你一个括号序列,q个询问,求区间内括号匹配的数量;

思路:线段数区间合并;

   一个线段树存三个数,l,m,r;

   l表示区间内除去以匹配的左括号的数量;

   r表示区间内除去以匹配的右括号的数量;

   m表示匹配好的左右括号的数量;

   对于左边的区间,和右边的区间,只要将 左边的l和右边的尽量匹配即可;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e6+,M=1e7+,inf=1e9+;
const ll INF=1e18+,mod=1e9+; /// 数组大小
char s[N]; struct ans
{
int x,y,z;
ans(){}
ans(int xx,int yy,int zz)
{
x=xx;y=yy;z=zz;
}
}; struct SGT
{
int LT[N<<],RT[N<<],MT[N<<];
void pushup(int pos)
{
int k=min(LT[pos<<],RT[pos<<|]);
MT[pos]=MT[pos<<]+MT[pos<<|]+*k;
LT[pos]=LT[pos<<]+LT[pos<<|]-k;
RT[pos]=RT[pos<<]+RT[pos<<|]-k;
}
void build(int l,int r,int pos)
{
if(l==r)
{
LT[pos]=;
RT[pos]=;
MT[pos]=;
if(s[l]=='(')
LT[pos]++;
else
RT[pos]++;
return;
}
int mid=(l+r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
pushup(pos);
}
ans query(int L,int R,int l,int r,int pos)
{
if(L==l&&r==R)
return ans(LT[pos],MT[pos],RT[pos]);
int mid=(l+r)>>;
if(R<=mid)
return query(L,R,l,mid,pos<<);
else if(L>mid)
return query(L,R,mid+,r,pos<<|);
else
{
ans a=query(L,mid,l,mid,pos<<);
ans b=query(mid+,R,mid+,r,pos<<|);
int k=min(a.x,b.z);
int m=a.y+b.y+*k;
int l=a.x+b.x-k;
int r=a.z+b.z-k;
return ans(l,m,r);
}
}
};
SGT tree;
int main()
{
scanf("%s",s+);
int x=strlen(s+);
tree.build(,x,);
int q;
scanf("%d",&q);
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",tree.query(l,r,,x,).y);
}
return ;
}
/*
))(()))))())())))))())((()()))))()))))))))))))
9
26 42
21 22
6 22
7 26
43 46
25 27
32 39
22 40
2 45
*/

Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并的更多相关文章

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

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

  2. Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树

    C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...

  3. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  4. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线

    D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...

  5. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

  6. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

  7. Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash

    E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...

  8. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  9. Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)

    题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...

随机推荐

  1. [py][mx]django课程页显示city和机构封面图

    city和课程机构信息展示到前台去 organization/views.py from django.views.generic.base import View from organization ...

  2. Keras 源码分析

    . │ activations.py │ callbacks.py │ constraints.py │ initializations.py │ metrics.py │ models.py │ o ...

  3. iOS 界面启动时,功能新特征显示

    APP新启动的时候,都会有几张新的图片滑动,才能到主的界面.现在,我们新建一个控制器,专门来处理新特性,直接上代码. 第一步:新建一个NewfeatureController // // HWNewf ...

  4. IIS注册WEB服务扩展

    .net framework和iis那个先装的?如果先装IIS,然后再装.net的话,没有问题.但是如果顺序反了的话,需要在命令行里面执行 C:\Windows\Microsoft.NET\Frame ...

  5. JSTL—标签

    什么是JSTL标签? Jsp标准标签库(JSP Standerd Tag Library) JSTL的优点是什么? 1) 提供一组标准的标签 2)可用于编写动态功能 使用JSTL的步骤? 1)引入ja ...

  6. Servlet—文件上传

    什么是Commons? Apache的一个开源子项目,Commons-FileUpload是 Conmmons下子项目. Commons-FileUpload的作用? 1:该组件可以方便地嵌入JSP页 ...

  7. STA分析(一) setup and hold

    timing check可以分为Dynamic Timing Analysis(Post_sim)和Static Timing Analysis STA:可以分析的很全面:仿真速度也很快:可以分析控制 ...

  8. TouchSlide触屏滑动特效插件的使用

    官方连接:http://www.superslide2.com/TouchSlide/ TouchSlide 是纯javascript打造的触屏滑动特效插件,面向手机.平板电脑等移动终端, 能实现触屏 ...

  9. zw版【转发·台湾nvp系列Delphi例程】HALCON SmallestRectangle1

    zw版[转发·台湾nvp系列Delphi例程]HALCON SmallestRectangle1 procedure TForm1.Button1Click(Sender: TObject);var ...

  10. 在notepad++里面使用正则表达式替换掉所有行逗号前面内容

    需求:在notepad++里面使用正则表达式替换掉所有行逗号前面内容,一文本内容如下(只贴一小部分,实际上N多): 级别,層級程序,程式插件,外掛程式鼠标,滑鼠打印,列印打开,開啟博客,部落格联系,聯 ...