C. Sereja and Brackets----线段树

C. 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

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|).

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


题目链接:http://codeforces.com/contest/380/problem/C


大早上的硬是怼了一个线段树。。

题目的意思是说给你一段括号序列,然后给你查询区间,问你在此区间内括号正确匹配的长度。

一开始没想出来,后来一百度,看见这个题目的标题是用线段树,顿悟。

线段树的节点存三个数值,一个是sum,代表该区间内的正确的括号序列的长度,一个是左括号数量,一个是右括号数量,合并时,sum+=新的括号匹配的长度,也就是左区间的左括号和右区间的右括号数量的最小值。

好久没有敲线段树了,有些手生了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=1000000+10;
int sum[maxn<<2],ll[maxn<<2],rr[maxn<<2];
char s[maxn];
void PushUp(int rt){
    int tmp=min(ll[rt<<1],rr[rt<<1|1]);
    sum[rt]=sum[rt<<1]+sum[rt<<1|1]+tmp*2;
    ll[rt]=ll[rt<<1]+ll[rt<<1|1]-tmp;
    rr[rt]=rr[rt<<1]+rr[rt<<1|1]-tmp;
}
void build(int l,int r,int rt){
    if(l==r){
        ll[rt]=rr[rt]=sum[rt]=0;
        if(s[l-1]=='(')
            ll[rt]=1;
        else
            rr[rt]=1;
        return ;
    }
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    PushUp(rt);
}
int Query(int L,int R,int l,int r,int rt,int &lls,int &rrs){
    if(l>=L&&r<=R){
        lls=ll[rt];
        rrs=rr[rt];
        return sum[rt];
    }
    int m=(l+r)>>1;
    if(m>=R)
        return Query(L,R,l,m,rt<<1,lls,rrs);
    else if(m<L)
        return Query(L,R,m+1,r,rt<<1|1,lls,rrs);
    else{
        int sum1,sum2,ll2,rr2,tmp;
        sum1=Query(L,R,l,m,rt<<1,lls,rrs);
        sum2=Query(L,R,m+1,r,rt<<1|1,ll2,rr2);
        tmp=min(lls,rr2);
        lls=lls+ll2-tmp;
        rrs=rrs+rr2-tmp;
        return sum1+sum2+tmp*2;
    }
}
int main(){
    int n;
    scanf("%s",s);
    n=strlen(s);
    build(1,n,1);
    int m,l,r,ll,rr;
    scanf("%d",&m);
    while(m--){
        scanf("%d%d",&l,&r);
        int ans=Query(l,r,1,n,1,ll,rr);
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值