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 l**i, r**i(1 ≤ l**i ≤ r**i ≤ 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 l**i, r**i (1 ≤ l**i ≤ r**i ≤ 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 = s**k1s**k2... 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,代表一个区间。对于每一个询问,让你输出区间中能选出最长的子序列是合法的括号序列的长度。

思路:

线段树+分治的思想来解决此问题。

我们线段树每一个区间维护以下信息:

1、区间中能选出最长的子序列是合法的括号序列的个数 num。

2、 区间中多余的'(' 字符的个数 a

3、区间中多余的')' 字符的个数 b

那么对于区间合并时,

num=左儿子的num+右儿子的num+min(左儿子的a,右儿子的b)

a=左儿子的a+右儿子的a - min(左儿子的a,右儿子的b)

b=左儿子的b+右儿子的b - min(左儿子的a,右儿子的b)

最后输出时,注意num个括号个数,*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 num;
int a;// (
int b;// )
} segmeng_tree[maxn << 2];
char s[maxn];
int n;
int m;
void pushup(int rt)
{
int x = min(segmeng_tree[rt << 1].a, segmeng_tree[rt << 1 | 1].b);
segmeng_tree[rt].num = x + segmeng_tree[rt << 1].num + segmeng_tree[rt << 1 | 1].num;
segmeng_tree[rt].a = segmeng_tree[rt << 1].a + segmeng_tree[rt << 1 | 1].a - x;
segmeng_tree[rt].b = segmeng_tree[rt << 1].b + segmeng_tree[rt << 1 | 1].b - x;
}
void build(int rt, int l, int r)
{
segmeng_tree[rt].l = l;
segmeng_tree[rt].r = r;
if (l == r) {
segmeng_tree[rt].a = s[l] == '(';
segmeng_tree[rt].b = s[l] == ')';
segmeng_tree[rt].num = 0;
} else {
int mid = (l + r) >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
pushup(rt);
}
} node ask(int rt, int l, int r)
{
if (segmeng_tree[rt].l >= l && segmeng_tree[rt].r <= r) {
return segmeng_tree[rt];
}
int mid = (segmeng_tree[rt].l + segmeng_tree[rt].r) >> 1;
if (r <= mid) {
return ask(rt << 1, l, r);
} else if (l > mid) {
return ask(rt << 1 | 1, l, r);
} else {
node res1 = ask(rt << 1, l, r);
node res2 = ask(rt << 1 | 1, l, r);
node res = res1;
int x = min(res1.a, res2.b);
res.num += x;
res.b += res2.b;
res.a += res2.a;
res.num += res2.num;
res.b -= x;
res.a -= x;
return res;
}
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
scanf("%s", s + 1);
n = strlen(s + 1);
build(1, 1, n);
scanf("%d", &m);
while (m--) {
int l, r;
scanf("%d %d", &l, &r);
printf("%d\n", ask(1, l, r).num * 2);
}
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';
}
}
}

Sereja and Brackets CodeForces - 380C (线段树+分治思路)的更多相关文章

  1. Sereja and Brackets CodeForces - 380C (树状数组+离线)

    Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...

  2. Codeforces 938G 线段树分治 线性基 可撤销并查集

    Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...

  3. Sonya and Bitwise OR CodeForces - 1004F (线段树,分治)

    大意: 给定序列$a$, 给定整数$x$. 两种操作(1)单点修改 (2)给定区间$[l,r]$,求有多少子区间满足位或和不少于$x$. 假设不带修改. 固定右端点, 合法区间关于左端点单调的. 可以 ...

  4. Codeforces 1140F 线段树 分治 并查集

    题意及思路:https://blog.csdn.net/u013534123/article/details/89010251 之前cf有一个和这个相似的题,不过那个题只有合并操作,没有删除操作,直接 ...

  5. loj#2312. 「HAOI2017」八纵八横(线性基 线段树分治)

    题意 题目链接 Sol 线性基+线段树分治板子题.. 调起来有点自闭.. #include<bits/stdc++.h> #define fi first #define se secon ...

  6. BZOJ.4184.shallot(线段树分治 线性基)

    BZOJ 裸的线段树分治+线性基,就是跑的巨慢_(:з」∠)_ . 不知道他们都写的什么=-= //41652kb 11920ms #include <map> #include < ...

  7. BZOJ.4137.[FJOI2015]火星商店问题(线段树分治 可持久化Trie)

    BZOJ 洛谷 一直觉得自己非常zz呢.现在看来是真的=-= 注意题意描述有点问题,可以看BZOJ/洛谷讨论. 每个询问有两个限制区间,一是时间限制\([t-d+1,t]\),二是物品限制\([L,R ...

  8. 洛谷.3733.[HAOI2017]八纵八横(线性基 线段树分治 bitset)

    LOJ 洛谷 最基本的思路同BZOJ2115 Xor,将图中所有环的异或和插入线性基,求一下线性基中数的异或最大值. 用bitset优化一下,暴力的复杂度是\(O(\frac{qmL^2}{w})\) ...

  9. bzoj4025二分图(线段树分治 并查集)

    /* 思维难度几乎没有, 就是线段树分治check二分图 判断是否为二分图可以通过维护lct看看是否链接出奇环 然后发现不用lct, 并查集维护奇偶性即可 但是复杂度明明一样哈 */ #include ...

随机推荐

  1. sql server 备份语句

    1.BACKUP DATABASE your_database TO DISK = 'diff.bak'with DIFFERENTIAL #差异备份,仅备份数据2.BACKUP DATABASE y ...

  2. 【POJ - 1950】Dessert(dfs)

    -->Dessert Descriptions: 给你一个数N(3<=N<=15);每个数之间有三种运算符“‘+’,‘-’,‘.’”.输出和值等于零的所有的运算情况及次数num,如果 ...

  3. 产品之我见(2)-求职APP

    在求职过程中使用了BOSS直聘.拉钩.领英这三款软件,下面来讲述一下作为求职者身份的使用感受以及遇到的问题.            一.BOSS直聘 手机端:打开首先是标语“互联网招聘神器”的页面   ...

  4. SpringCloud学习(五)路由网关(zuul)(Finchley版本)

    在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费.负载均衡.断路器.智能路由.配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统.一个简单的微服务系统如下图: ...

  5. python之迭代器、可迭代对象、生成器、生成器对象、枚举类型

    迭代器 # 迭代器:循环反馈的容器(集合类型)# -- 不同于索引取值,但也可以循环的从容器对象中从前往后逐个返回内部的值​# 优点:不依赖索引,完成取值# 缺点:不能计算长度,不能指定位取值(只能从 ...

  6. 人渣给我笔记本网络适配器里加的的“bluetooth设备(个人局域网)”,卸载不掉

  7. 正则re模块--入门

    本文来源:https://www.cnblogs.com/dyfblog/p/5880728.html 对字符串操作 1.应用: 当我们爬取的东西在js文件中,比如我爬今日头条美女的图片时,它的图片u ...

  8. 查看Oracle索引是否被使用或者有效

    第一步: explain plan for select * from T_USER where OPEN_ID='12345'; 第二步: select * from table(dbms_xpla ...

  9. POJ - 3687 Labeling Balls (拓扑)

    (点击此处查看原题) 题意 此处有n盏灯,编号为1~n,每盏灯的亮度都是唯一的,且在1~n范围之间,现已知m对灯之间的关系:a b ,说明灯a的亮度比灯b小,求出每盏灯的亮度,要求字典序最小(编号小的 ...

  10. Yii2.0 queue

    https://www.yiichina.com/tutorial/1635 https://my.oschina.net/gcdong/blog/3031113 https://www.yii-ch ...