Sereja and Brackets(括号匹配)
Description
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.
Input
())(())(())(
7
1 1
2 3
1 2
1 12
8 12
5 11
2 10
Output
0
0
2
10
4
6
6
Note
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 «()(())(())».
解题思路:求连续子串中括号匹配的个数。括号匹配问题一般可以用栈来维护。对于每个右括号,其匹配的左括号是固定的,因此我们可以记录每个右括号匹配的左括号位置,对整个区间进行线扫描,同时用树状数组维护+离散化处理即可。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e6+;
const int maxv=1e5+;
char str[maxn];int n,m,pos,tree[maxn],ans[maxv];stack<int> st;
struct node{int l,r,id;}query[maxn];
bool cmp(node a,node b){return a.r<b.r;}///右端点排序
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void print(int x){
if(x<)putchar('-'),x=-x;
if(x>)print(x/);
putchar(x%+'');
}
int lowbit(int x){return x&-x;}
void add(int x,int val){
for(int i=x;i<=n;i+=lowbit(i))tree[i]+=val;
}
int get_sum(int x){
int ans=;
for(int i=x;i>;i-=lowbit(i))ans+=tree[i];
return ans;
}
int main(){
while(~scanf("%s",str)){
while(!st.empty())st.pop();n=strlen(str);
memset(tree,,sizeof(tree)),memset(ans,,sizeof(ans));m=read();
for(int i=;i<m;++i)query[i].l=read(),query[i].r=read(),query[i].id=i;
sort(query,query+m,cmp);pos=;
for(int i=;i<m;++i){
for(int j=pos;j<=query[i].r;++j){
if(str[j-]=='(')st.push(j);///记录左括号的位置(物理位置)
else if(!st.empty())add(st.top(),),st.pop();///单点更新
}
pos=query[i].r+;///从下一个位置开始,避免重叠,O(n)遍历
ans[query[i].id]=get_sum(query[i].r)-get_sum(query[i].l-);
}
for(int i=;i<m;++i)print(ans[i]),puts("");
}
return ;
}
Sereja and Brackets(括号匹配)的更多相关文章
- POJ-2955 Brackets(括号匹配问题)
题目链接:http://poj.org/problem?id=2955 这题要求求出一段括号序列的最大括号匹配数量 规则如下: the empty sequence is a regular brac ...
- POJ 2955 Brackets(括号匹配一)
题目链接:http://poj.org/problem?id=2955 题目大意:给你一串字符串,求最大的括号匹配数. 解题思路: 设dp[i][j]是[i,j]的最大括号匹配对数. 则得到状态转移方 ...
- poj 2955 Brackets 括号匹配 区间dp
题意:最多有多少括号匹配 思路:区间dp,模板dp,区间合并. 对于a[j]来说: 刚開始的时候,转移方程为dp[i][j]=max(dp[i][j-1],dp[i][k-1]+dp[k][j-1]+ ...
- POJ - 2955 Brackets括号匹配(区间dp)
Brackets We give the following inductive definition of a “regular brackets” sequence: the empty sequ ...
- poj 2955 Brackets (区间dp 括号匹配)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- CSUOJ 1271 Brackets Sequence 括号匹配
Description ]. Output For each test case, print how many places there are, into which you insert a ' ...
- CodeForces-380C:Sereja and Brackets(线段树与括号序列)
Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length n, consistin ...
- POJ 1141 Brackets Sequence(括号匹配二)
题目链接:http://poj.org/problem?id=1141 题目大意:给你一串字符串,让你补全括号,要求补得括号最少,并输出补全后的结果. 解题思路: 开始想的是利用相邻子区间,即dp[i ...
- CF380C. Sereja and Brackets[线段树 区间合并]
C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...
随机推荐
- spark0.9.1集群模式执行graphx測试程序(LiveJournalPageRank,新增Connected Components)
spark最新版公布了.之前的版本号就已经集成了graphx,这个版本号还改了一些bug. 我做了简单測试,只是网上关于集群模式执行spark资料太少了,仅仅有关于EC2(见參考资料1)的.可是还非常 ...
- STL--map用法
STL--map用法map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力由于这个特性它完成有可能在我们处理 ...
- asp对中文编码及解码,Decode和Encode中文网址处理
<%'-------------------------------------------------------------------------- '================== ...
- SpringMVC实战(注解)
1.前言 前面几篇介绍了SpringMVC中的控制器以及视图之间的映射方式,这篇来解说一下SpringMVC中的注解,通过注解能够非常方便的訪问到控制器中的某个方法. 2.配置文件配置 2.1 注解驱 ...
- ECMAScript学习笔记
1. ECMAScript不存在块级作用域,因此在循环内部定义的变量,在循环外也是可以访问的 eg: var count =10; fpr(var i=0; i<count; i++){ ale ...
- lc.exe 已退出 代码为 -1
地址:http://jingyan.baidu.com/article/91f5db1bd0ace31c7f05e321.html
- iOS中区分照片的来源
原理就是通过枚举出每个assets group,然后取得group property,group property是个整数,对应头文件中的一些枚举值.用这个可以判断照片是从哪来的(相机胶卷.照片流.相 ...
- Java 深拷贝浅拷贝 与 序列化
一.浅拷贝.深拷贝 浅拷贝会对对象中的成员变量进行拷贝:如果是基本类型,拷贝的就是基本类型的值:如果属性是内存地址(引用类型),拷贝的就是内存地址 : 深拷贝,除了基本类型外,引用类型所引用的对象也会 ...
- js split分割字符串成数组
str = "2,2,3,5,6"; //这是一字符串 var strs = new Array(); //定义一数组 strs = str.split("," ...
- html5--6-65 布局4-弹性布局
html5--6-65 布局4-弹性布局 实例 学习要点 掌握传统布局与CSS3新增弹性布局方式的实现和应用 弹性布局(弹性伸缩布局) 事实上它是一种新类型的盒子模型,也有书上称作弹性伸缩盒布局. 比 ...