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. [django]Django model中数据批量导入bulk_create()

    参考: https://www.cnblogs.com/ccorz/p/Django-model-zhong-shu-ju-pi-liang-dao-rubulkcreat.html import o ...

  2. C# 多线程,new ThreadStart(方法)中的方法如果有参数,该怎么写

    using System; using System.Threading; public class Work { public static void Main() { // Start a thr ...

  3. PageRank算法与TextRank算法详解

    PageRank算法: 该算法本质上属于有向带权图. 对于某个互联网网页A来说,该网页PageRank的计算基于以下两个基本假设: 数量假设:在Web图模型中,如果一个页面节点接收到的其他网页指向的入 ...

  4. Advapi32.dll--介绍

    https://blog.csdn.net/zhoujielun123456/article/details/50338147 使用方法详见:OpsTotalService

  5. Unity3D关于VR的Demo(一)

    https://blog.csdn.net/qq_15807167/article/details/52048998?locationNum=8&fps=1 阅读数:9716 最近有点忙,只有 ...

  6. bzoj1704 / P2882 [USACO07MAR]面对正确的方式Face The Right Way

    P2882 [USACO07MAR]面对正确的方式Face The Right Way $n<=5000$?枚举翻转长度,顺序模拟就ok了 对于每次翻转,我们可以利用差分的思想,再搞搞前缀和. ...

  7. Lombok让pojo变得更优雅

    Lombok 采取注解的形式,标记在pojo上面,在编译后,自动生成相应的方法,像get.set.构造方法等都可以注解一键生成. 引入jar包: <dependency> <grou ...

  8. Linux系统下(x64)安装jdk 1.6(jdk-6u45-linux-x64.bin)

    Linux系统下(x64)安装jdk 1.6(jdk-6u45-linux-x64.bin) 一,查看是否安装jdk: # rpm -qa | grep jdk 或者 #rpm -q jdk 或者 # ...

  9. C++设计模式 之 “组件协作”模式:Template Method、Strategy、Observer

    “组件协作”模式: #现代软件专业分工之后的第一个结果是“框架与应用程序的划分”,“组件协作”模式通过晚期绑定,来实现框架与应用程序之间的松耦合,是二者之间协作时常用的模式. #典型模式: Templ ...

  10. 20145118 《Java程序设计》 实验报告三

    实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程 2 ...