CodeForces-380C:Sereja and Brackets(线段树与括号序列)
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.
Example
())(())(())(
7
1 1
2 3
1 2
1 12
8 12
5 11
2 10
0
0
2
10
4
6
6
题意:给定由‘(’和‘)’组成的字符串序列,然后Q个询问,每次回答区间最多有多少个匹配的括号。
思路:分治的思想,当前的括号匹配对于左区间的匹配数+右区间的匹配数+min(左区间的未匹配的左括号,右区间的未匹配的右括号),同时改变当前区间未匹配的括号数。
注意:因为有3个参数需要传递,所以用结构体(因为要修改未匹配括号数)。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
char c[maxn];
struct in
{
int a,b,sum;
in(){ a=; b=; sum=;}
}s[maxn<<];
void read(int &res)
{
char c=getchar();res=; for(;c>''||c<'';c=getchar());
for(;c<=''&&c>='';c=getchar()) res=(res<<)+(res<<)+c-'';
} struct segment_tree
{
void pushup(int Now)
{
int tmp=min(s[Now<<].a,s[Now<<|].b);
s[Now].sum=s[Now<<].sum+s[Now<<|].sum+tmp;
s[Now].a=s[Now<<].a+s[Now<<|].a-tmp;
s[Now].b=s[Now<<].b+s[Now<<|].b-tmp;
}
void build(int Now,int L,int R)
{
if(L==R) {
s[Now].a=(c[L]=='('?:);
s[Now].b=(c[L]==')'?:);
s[Now].sum=; return ;
}
int Mid=(L+R)>>;
build(Now<<,L,Mid);
build(Now<<|,Mid+,R);
pushup(Now);
}
in query(int Now,int L,int R,int l,int r)
{
if(l<=L&&r>=R) return s[Now];
int Mid=(L+R)>>; in w,e,res;
if(l<=Mid) w=query(Now<<,L,Mid,l,r);
if(r>Mid) e=query(Now<<|,Mid+,R,l,r);
res.sum=min(w.a,e.b);
res.a=w.a+e.a-res.sum;
res.b=w.b+e.b-res.sum;
res.sum+=w.sum+e.sum;
return res;
}
}Tree;
int main()
{
int N,Q,x,y;
scanf("%s",c+);
N=strlen(c+);
Tree.build(,,N);
read(Q);
while(Q--){
read(x); read(y);
printf("%d\n",*Tree.query(,,N,x,y).sum);
}
return ;
}
CodeForces-380C:Sereja and Brackets(线段树与括号序列)的更多相关文章
- Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并
题目链接:http://codeforces.com/contest/381/problem/E E. Sereja and Brackets time limit per test 1 secon ...
- CodeForces 380C Sereja and Brackets(扫描线+树状数组)
[题目链接] http://codeforces.com/problemset/problem/380/C [题目大意] 给出一个括号序列,求区间内左右括号匹配的个数. [题解] 我们发现对于每个右括 ...
- CF380C. Sereja and Brackets[线段树 区间合并]
C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...
- bzoj1095: [ZJOI2007]Hide 捉迷藏 线段树维护括号序列 点分治 链分治
这题真是十分难写啊 不管是点分治还是括号序列都有一堆细节.. 点分治:时空复杂度$O(n\log^2n)$,常数巨大 主要就是3个堆的初始状态 C堆:每个节点一个,为子树中的点到它父亲的距离的堆. B ...
- BZOJ 1095 捉迷藏(线段树维护括号序列)
对于树的一个括号序列,树上两点的距离就是在括号序列中两点之间的括号匹配完之后的括号数... 由此可以得出线段树的做法.. #include<cstdio> #include<iost ...
- BZOJ1095: [ZJOI2007]Hide 捉迷藏【线段树维护括号序列】【思维好题】
Description 捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩 捉迷藏游戏.他们的家很大且构造很奇特,由N个屋子和N-1条 ...
- Codeforces Round #603 (Div. 2) E - Editor(线段树,括号序列)
- BZOJ 1095: [ZJOI2007]Hide 捉迷藏(线段树维护括号序列)
这个嘛= =链剖貌似可行,不过好像代码长度很长,懒得打(其实是自己太弱了QAQ)百度了一下才知道有一种高大上的叫括号序列的东西= = 岛娘真是太厉害了,先丢链接:http://www.shuizilo ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
随机推荐
- vba比较日期大小,定义日期;vba让excel保存
Private Sub CommandButton1_Click()Dim i, j As IntegerDim a As Datea = #10/1/2013#j = 2i = 2' If Wor ...
- DBCP,C3P0与Tomcat jdbc pool 连接池的比较
hibernate开发组推荐使用c3p0; spring开发组推荐使用dbcp(dbcp连接池有weblogic连接池同样的问题,就是强行关闭连接或数据库重启后,无法reconnect,告诉连接被重置 ...
- PAT (Advanced Level) 1089. Insert or Merge (25)
简单题.模拟一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...
- sql 添加自定义排序
Mysql : SELECT (@i:=@i+1) AS ind ,字段 FROM 表名 别名, (SELECT @i:=0) t WHERE `IsDeleted` = 0; Oracle: 本就有 ...
- 【面试 hibernate】【第二篇】hibernate相关问题
1.hibernate工作原理[说一下你怎么理解的hibernate] hibernate是一个ORM对象关系映射的持久层框架,是对JDBC的轻量级封装. [可以不记,hibernate核心接口] 1 ...
- linux 源码编译安装apache
cc1 是c语言的编译器.
- java学习笔记(四)面向对象
一.形參长度可变的方法 当传入被调用的函数參数数量不确定时,在方法最后一个形參的类型后加上三个点号(...),表明该形參能够接受多个參数值.多个參数值被当做数组传入,这些參数必须为指定的类型. pac ...
- Solidworks如何绘制标准螺纹线
1 绘制螺旋线,螺距为0.5mm,圈数为15,起始角度为0°. 2
- SolidEdge如何自动标注尺寸
1 工具-尺寸-关系助手(必须在编辑草图轮廓状态下,如果你的草图不可编辑,则没有这些选项) 2 框选要自动标注尺寸的东西,这些东西立即变为黄色,然后打对勾 3 选择横纵坐标尺寸原点(其实就是为 ...
- Classification and logistic regression
logistic 回归 1.问题: 在上面讨论回归问题时.讨论的结果都是连续类型.但假设要求做分类呢?即讨论结果为离散型的值. 2.解答: 假设: 当中: g(z)的图形例如以下: 由此可知:当hθ( ...