Codeforces 380C. Sereja and Brackets(模拟啊)

来源:互联网 发布:乐高编程机器人 编辑:程序博客网 时间:2024/05/16 08:07

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


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.

Sample test(s)
input
())(())(())(71 12 31 21 128 125 112 10
output
00210466
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 «()(())(())».


题意:

给出一个含‘ ( ’ ,‘ ) ' 的字符串,

然后给出一个询问区间,去这个区间里完整匹配的()的数目!一个()的答案为2!

PS:

我们可以用栈把每个’ ( ‘的下标存起来!然后再扫一遍’ )‘!用树状数组维护一下即可!

代码如下:

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <iostream>#include <stack>using namespace std;#define maxn 1000017#define maxm 100017using namespace std;struct node{    int l, r;    int id;} q[maxm];char s[maxn];int c[maxn];int ans[maxm];stack<int> st;int len;int Lowbit(int x)  // 2^k{    return x&(-x);}void update(int i, int x)//i点增量为x{    while(i <= len)    {        c[i] += x;        i += Lowbit(i);    }}int sum(int x)//区间求和 [1,x]{    int ans = 0;    while(x)    {        ans += c[x];        x -= Lowbit(x);    }    return ans;}bool cmp(node a, node b){    return a.r < b.r;}int main(){    int m;    while(~scanf("%s",s))    {        int pos = 1;        memset(c, 0, sizeof(c));        while(!st.empty())        {            st.pop();        }        len = strlen(s);        scanf("%d",&m);        for(int i = 0; i < m; i++)        {            scanf("%d%d",&q[i].l,&q[i].r);            q[i].id = i;        }        sort(q, q+m, cmp);        for(int i = 0; i < m; i++)        {            for(int j = pos; j <= q[i].r; j++)            {                if(s[j-1] == '(')                {                    st.push(j);                }                else if(s[j-1] == ')')                {                    if(!st.empty())                    {                        int tt = st.top();                        update(tt, 2);                        st.pop();                    }                }            }            pos = q[i].r+1;            ans[q[i].id] = sum(q[i].r) - sum(q[i].l-1);        }        for (int i = 0; i < m; i++)        {            printf("%d\n", ans[i]);        }    }    return 0;}/*())(())(())(71 11 22 32 105 111 128 12*/


1 0
原创粉丝点击