Sereja and Brackets

题目链接: CodeForces - 380C

Sereja has a bracket sequence s1, s2, ..., *s**n, 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, ..., *s**n* (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

())(())(())(71 12 31 21 128 125 112 10

Output

00210466

Note

A subsequence of length |x| of string s = s1s2... s|s| (where |s| is the length of string s) is string x = sk1sk2... *s**k|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和r,代表一个区间。对于每一个询问,让你输出区间中能选出最长的子序列是合法的括号序列的长度。

思路:

对询问区间进行离线保存,以右端点升序来排序,

然后从坐向右扫描,用stack来维护每一个还没被匹配到的(字符的下标,

当来一个)字符的时候,将其和栈顶的那个(匹配,同时用树状数组在(的下标的数值上+2

然后走到每一个区间的右端点时对区间的答案进行更新。

为什么这样写可以呢?

因为我们可以通过分析发现,字符串中每一个)字符在区间中可以固定匹配到一个(使其区间答案最右。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2) { ans = ans * a % MOD; } a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
struct node {int l, r;int id;
} a[maxn];
char s[maxn];
int n;
int m;
bool cmp(node aa, node bb)
{return aa.r < bb.r;
}
int tree[maxn];
int lowbit(int x)
{return x & (-1 * x);
}
void add(int x, int val)
{while (x <= n) {tree[x] += val;x += lowbit(x);}
}
int ask(int x)
{int res = 0;while (x) {res += tree[x];x -= lowbit(x);}return res;
}
int ans[maxn];
int main()
{//freopen("D:\\code\\text\\input.txt","r",stdin);//freopen("D:\\code\\text\\output.txt","w",stdout);gbtb;cin >> s + 1;n=strlen(s+1);cin >> m;repd(i, 1, m ) {cin >> a[i].l >> a[i].r;a[i].id = i;}sort(a + 1, a + 1 + m, cmp);stack<int> st;while (sz(st)) {st.pop();}int pos = 1;repd(i, 1, m) {
//        chu(pos);repd(j, pos, a[i].r) {if (s[j] == '(') {st.push(j);} else {if (sz(st)) {add(st.top(), 2);st.pop();}}}ans[a[i].id] = ask(a[i].r) - ask(a[i].l - 1);pos = a[i].r + 1;}repd(i,1,m){printf("%d\n",ans[i] );}return 0;
}inline void getInt(int *p)
{char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}} else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}}
}

转载于:https://www.cnblogs.com/qieqiemin/p/11491979.html

Sereja and Brackets CodeForces - 380C (树状数组+离线)相关推荐

  1. CodeForces 314C 树状数组 + dp

    //CodeForces 314C //分析:相当于求给定序列的不降子序列的个数,从一个空序列开始将得到的不降子序列不断的延长是典型的做法,则dp[i]表示以第 i 个元素结尾的序列 //思路:O(n ...

  2. CodeForces - 538F--A Heap of Heaps(树状数组+离线)

    题目链接https://codeforces.com/problemset/problem/538/F Time limit 3000 ms Memory limit 524288 kB Andrew ...

  3. CodeForces 396C 树状数组 + DFS

    本主题开始看到以为段树或树状数组,但是,对于一个节点的有疑问的所有子节点的加权,这一条件被视为树的根,像 然后1号是肯定在第一层中,然后建立一个单向侧倒查,然后记录下来 其中每个节点 层,终于 两个节 ...

  4. hdu 3333 树状数组+离线处理

    思路:既然要求的是不同的元素的和,那么我们可以想办法让每个值在区间中只出现一次,于是想到了离线的算法:将查询按照右端点排序,位置在右端点之前的元素都插入到树状数组中,对于已经出现过的值,我们要先删除( ...

  5. hdu 4417(树状数组+离线算法)

    解题思路:这道题要求某区间内比h小的个数,其实这里可以类似于树状数组求逆序数那样.关键是如何转换成树状数组的模型,这才是本题的难点. 我们首先分析,如果知道h在该区间的哪个位置,那么剩下的就很好做了. ...

  6. SPOJ D-query 树状数组离线 求区间内不同数字的个数

    Given a sequence of n numbers a1, a2, -, an and a number of d-queries. A d-query is a pair (i, j) (1 ...

  7. hdu4417 Super Mario(树状数组+离线区间操作)

    题目链接 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jum ...

  8. 2019 南京 网络赛 B (二维偏序,树状数组离线)

    题意: 给出一N*N的蛇形矩阵,具体位置元素值不给你,自己找规律,然后给你M个 有效位置,P次查询,每次查询一个子矩阵中有效元素的权值和,该权值和等于对于 每个有效元素,模10拆分后相加得到的和.(注 ...

  9. P4113 [HEOI2012]采花 ( 树状数组 + 离线 )

    题目链接:点击进入 题目 思路 跟此题相似:HH的项链 对于若干个询问的区间 [ l , r ] ,如果他们的 r 都相等的话,那么对于数组中出现的同一个数字,因为要求只有每个数出现至少两次才可以计入 ...

最新文章

  1. 真是祸从GPT-2口出,和AI聊会天,把别人隐私都给套出来了
  2. android界面不可见键盘隐藏功能,Android中点击隐藏软键盘最佳方法
  3. 苹果开发账号过期不续费会怎样?
  4. CRM, C4C和SAP Hybris的数据库层设计
  5. 面了三次字节,他的一些感悟
  6. 9、mybatis中动态sql的使用
  7. php 负载监控_php记录服务器负载、内存、cpu状态的代码
  8. php mysql长连接聊天室_PHP之探索MySQL 长连接、连接池
  9. oracle匿名代码块执行insert,MyBatis+Oracle在执行insert时空值报错之从源码寻找解决办法...
  10. 也许,真的有2012
  11. vc++6.0工具栏自绘按钮程序
  12. svpwm的matlab模型,使用simulink进行SVPWM模块的搭建
  13. ChIP-seq数据处理流程(附赠长达5小时的视频指导)
  14. python两个日期计算年龄
  15. 云上城之个服务器维护时间,云上城之歌开服时间表 官方最新开服情况
  16. 设计模式——单例模式(创建型模式)
  17. Unity 之 ShaderGraph 实现旋涡(传送门)效果入门级教程
  18. CSS制作垂直口风琴2
  19. 朔日计算机基础答案,大学计算机基础课程练习系统 使用标准手册.doc
  20. [转]2004手机游戏年终点评

热门文章

  1. 什么是工资单上的variable pay mix?
  2. SAP CRM Fiori busy dialog的工作原理
  3. 一个程序员必须学会的Github使用技巧
  4. SAP CRM 和 Cloud for Customer 的 Document flow API 介绍
  5. 虚拟机Ubuntu开机后提示:无法应用原保存的显示器配置(屏幕显示问题)
  6. 华为的型号命名规则_电力电缆产品的型号命名规则
  7. jvm内存参数配置_“步步精心”-常用JVM配置参数
  8. html %3ca id=%3e,a.markdown
  9. 传递子类 java_Java,将主类传递给子类,错误的编码风格?
  10. oracle序列创建及使用,Oracle创建和使用序列