Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并
1 second
256 megabytes
standard input
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.
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.
Print the answer to each question on a single line. Print the answers in the order they go in the input.
())(())(())(
7
1 1
2 3
1 2
1 12
8 12
5 11
2 10
0
0
2
10
4
6
6
A 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|).
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,m,r;
l表示区间内除去以匹配的左括号的数量;
r表示区间内除去以匹配的右括号的数量;
m表示匹配好的左右括号的数量;
对于左边的区间,和右边的区间,只要将 左边的l和右边的尽量匹配即可;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e6+,M=1e7+,inf=1e9+;
const ll INF=1e18+,mod=1e9+; /// 数组大小
char s[N]; struct ans
{
int x,y,z;
ans(){}
ans(int xx,int yy,int zz)
{
x=xx;y=yy;z=zz;
}
}; struct SGT
{
int LT[N<<],RT[N<<],MT[N<<];
void pushup(int pos)
{
int k=min(LT[pos<<],RT[pos<<|]);
MT[pos]=MT[pos<<]+MT[pos<<|]+*k;
LT[pos]=LT[pos<<]+LT[pos<<|]-k;
RT[pos]=RT[pos<<]+RT[pos<<|]-k;
}
void build(int l,int r,int pos)
{
if(l==r)
{
LT[pos]=;
RT[pos]=;
MT[pos]=;
if(s[l]=='(')
LT[pos]++;
else
RT[pos]++;
return;
}
int mid=(l+r)>>;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
pushup(pos);
}
ans query(int L,int R,int l,int r,int pos)
{
if(L==l&&r==R)
return ans(LT[pos],MT[pos],RT[pos]);
int mid=(l+r)>>;
if(R<=mid)
return query(L,R,l,mid,pos<<);
else if(L>mid)
return query(L,R,mid+,r,pos<<|);
else
{
ans a=query(L,mid,l,mid,pos<<);
ans b=query(mid+,R,mid+,r,pos<<|);
int k=min(a.x,b.z);
int m=a.y+b.y+*k;
int l=a.x+b.x-k;
int r=a.z+b.z-k;
return ans(l,m,r);
}
}
};
SGT tree;
int main()
{
scanf("%s",s+);
int x=strlen(s+);
tree.build(,x,);
int q;
scanf("%d",&q);
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",tree.query(l,r,,x,).y);
}
return ;
}
/*
))(()))))())())))))())((()()))))()))))))))))))
9
26 42
21 22
6 22
7 26
43 46
25 27
32 39
22 40
2 45
*/
Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并的更多相关文章
- CF380C. Sereja and Brackets[线段树 区间合并]
C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树
C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树
题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线
D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
- Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash
E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)
题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...
随机推荐
- Scala系统学习(二):Scala开发环境安装配置
Scala可以安装在任何基于UNIX/Linux或基于Windows的系统上.在您的机器上开始安装Scala之前,必须在计算机上安装Java 1.8或更高版本. 下面请按照以下步骤安装Scala. 步 ...
- Pandas之Series+DataFrame
Series是带有标签的一维数组,可以保存任何数据类型(整数,字符串,浮点数,python对象) index查看series索引,values查看series值 series相比于ndarray,是一 ...
- iOS 自定义日志输出
在做iOS开发过程中,我们经常需要输出日志来查看某些数据是否打印出来,或者查看查个类是否被调用了. 系统默认的是NSLog(@"xxxx %d",1) ,但如果该APP要发布到商店 ...
- unity3d-小案例之角色简单漫游
准备资源 我这里从网上下载一个角色模型,里面有一组动画.有站立.奔跑.杀怪等 我们来实现角色的前后左后移动,即键盘上的WSDA键,这里因为没有行走的动画.索性就用奔跑代替了!! 暂时先不计较代码冗余的 ...
- input的text输入框设置大一点
<input type="text" style="height:51px;width:449px;font-size:12px;">12可以随意改 ...
- 016-sed
行处理:一次处理一行.正则选定文本 ----->>sed处理格式:一.命令行格式:sed [options] 'command' files(如果没有则是通过管道)1.options: - ...
- Hive 大数据倾斜总结
在做Shuffle阶段的优化过程中,遇 到了数据倾斜的问题,造成了对一些情况下优化效果不明显.主要是因为在Job完成后的所得到的Counters是整个Job的总和,优化是基于这些 Counters得出 ...
- linux常用命令:split 命令
split是linux下常用的分割文件命令.Linux下文件分割可以通过split命令来实现,而用cat进行文件合并.而分割可以指定按行数分割和按大小分割两种模式. 1.命令格式: split [OP ...
- CentOS随笔 - 4.CentOS7安装MySql 5.5.60(下载 tar 方式安装)
前言 转帖请注明出处: http://www.cnblogs.com/Troy-Lv5/ 由于公司也有php+mysql的项目, 所以今天也把Mysql装了一遍. 为了与以前的程序和数据库兼容, 这次 ...
- ACM题目————中位数
题目描述 长为L的升序序列S,S[L / 2]为其中位数. 给出两个等长升序序列S1和S2,求两序列合并并排序后的中位数. 输入 多组数据,每组第一行为n,表示两个等长升序序列的长度. 接下来n行为升 ...