Codeforces 380C segment Tree bracket matching

Source: Internet
Author: User

Sereja and BracketsTime limit:MS Memory Limit:262144KB 64bit IO Format:%i64d &%i64u SubmitStatus

Description

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 answermqueries, each of them are described by and integers l,? Ri (1?≤? ) Li? ≤? Ri? ≤? n) . The answer to theI-th query is the length of the maximum correct bracket subsequence of sequence sl,? sli? +?1,?...,? Sri . Help Sereja answer all queries.

You can find the definitions-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 integerm(1?≤? ) M. ≤?105) -the number of queries. Each of the nextmLines contains a pair of integers. TheI-th line contains integers l,? Ri (1?≤? ) Li? ≤? Ri? ≤? n) -the Description of theI-th query.

Output

Print the answer to all question on a single line. Print the answers in the order they go in the input.

Sample Input

Input
()) (()) (()) (71 12 31 21 128 125 112 10
Output
00210466

Hint

Asubsequenceof length| x|Of string s? =? s 1s2 ... s | S| (where| S|is the length of strings) is string x? =? s  k1sk2 ... s k| X| (1?≤? ) K1?<? K2?<?...? <? k | x|? ≤?| S|) .

A correct bracket sequence is a bracket sequence so can be transformed to a correct aryphmetic expression by inserting characters "1" and "+" between the characters of the string. For example, bracket sequences "() ()", "(())" is correct (the resulting expressions "(1) + (1)" /c14> ","(+1) "), and" ("and" ("is not.")

For the third query required sequence would be? ()?.

For the fourth query required sequence would be? ()(())(())?.

Given a sequence, there are several pairs of complete parentheses matching codes in this sequence:
#include <cstdio> #include <cstring> #include <algorithm> #define MAXN 1111111using namespace std;    struct node{int l,r,all;    Node () {l=r=all=0;    } Node (int a,int b,int c) {l=a,r=b,all=c; }};    Node Sum[maxn<<2];char str[maxn];void pushup (int rt) {int t=min (SUM[RT&LT;&LT;1].L,SUM[RT&LT;&LT;1|1].R);    sum[rt].all=sum[rt<<1].all+sum[rt<<1|1].all+t;    sum[rt].l=sum[rt<<1].l+sum[rt<<1|1].l-t; Sum[rt].r=sum[rt<<1].r+sum[rt<<1|1].r-t;}        void build (int l,int R,int RT) {if (l==r) {if (str[l]== ' (') sum[rt].l++;        else if (str[l]== ') ') sum[rt].r++;    return;    } int mid= (L+R) >>1;    Build (l,mid,rt<<1);    Build (mid+1,r,rt<<1|1); Pushup (RT);}    Node query (int l,int r,int l,int r,int RT) {if (l>=l&&r>=r) return SUM[RT];    Node Cnt1,cnt2;    int mid= (L+R) >>1; if (l<=mid) cnt1=query (l,mid,l,r,rt<<1);    if (r>mid) cnt2=query (mid+1,r,l,r,rt<<1|1);    int t=min (CNT1.L,CNT2.R); Return Node (cnt1.l+cnt2.l-t,cnt1.r+cnt2.r-t,cnt1.all+cnt2.all+t);}    int main () {int q,l,r;    scanf ("%s", str+1);    scanf ("%d", &q);    int N=strlen (str+1);    Build (1,n,1);        while (q--) {scanf ("%d%d", &l,&r);    printf ("%d\n", 2*query (1,n,l,r,1). All); } return 0;}


Codeforces 380C segment Tree bracket matching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.