题意:

给出一串括号

给出一些询问,问某个区间[l,r]内的能合法匹配的括号数有多少个

分析:

我们能够实现处理两个数组

sum[i] 1....i中已经能匹配的右括号的数目

left[i] 1....i中还不能匹配的左括号数目

这两个数组能够非常easy的扫描一遍动态维护得出来

我们能够先求前缀和,即

1...m中有多少能匹配的右括号sum[m]

则,我们能够得到sum[r]-sum[l-1],区间[l,r]内能合法匹配的右括号

可是这些右括号的匹配包含了和[1,l-1]中的左括号的匹配

所以我们再减去left[l-1]。可是

这又会多减去了[1,l-1]中和[r+1....]右括号匹配的左括号

所以得加上这部分

这部分是多少?这是这个问题的关键也是这个问题比較难以理解的地方

这部分是min(left[l]...left[r])
为什么?

…...(..(.......
.....)......(............ ..)........)

1 2        L  3     4           R 5       6

红色部分是询问区间L,R

能够看出

我们这里减去了1,2括号

然而括号1是不应该减去的,它和括号6配对

括号2是应该减去的。它和括号3配对

假设我们取L,R上left的最小值,那么能够看到最小值落在括号3和括号4之间,能够避免把括号4算入(它和括号5匹配)

并且也能够把多减掉的括号1给加上

然而这里另一个tricks,假设,类似4号括号的这种括号在L,R区间上第一个位置的时候,这里是没有min的位置的。

推断这样的情况也确实麻烦

我们作例如以下处理

假设 left[l-1]-min(left[l]...left[r])<0则不减。否则减去(由于L,R内能匹配的右括号不超过sum[r]-sum[l-1],后者中的右括号还能和1。L上的左括号匹配)

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#define L(x) x<<1
#define R(x) x<<1|1
using namespace std;
const int NN=1111111;
char str[NN];
char tmp[NN];
int l[NN],r[NN];
struct TREE{
int x;
}tr[NN*6];
void build(int idx,int L,int R){
if(L==R){
tr[idx].x=l[L];
return;
}
int mid=(L+R)/2;
build(L(idx),L,mid);
build(R(idx),mid+1,R);
tr[idx].x=min(tr[L(idx)].x,tr[R(idx)].x);
}
int que(int idx,int ll,int rr,int L,int R){
int mid=(L+R)/2;
if(ll==L && rr==R){
return tr[idx].x;
}
if(rr<=mid){
return que(L(idx),ll,rr,L,mid);
}else if(ll>=mid+1){
return que(R(idx),ll,rr,mid+1,R);
}else{
return min(que(L(idx),ll,mid,L,mid),que(R(idx),mid+1,rr,mid+1,R));
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("G:/in.txt","r",stdin);
//freopen("G:/myout.txt","w",stdout);
#endif
cin>>(str+1);
//cin>>tmp;
int n;cin>>n;
int len=strlen(str+1);
for(int i=1;i<=len;i++){
l[i]=l[i-1];
r[i]=r[i-1];
if(str[i]=='('){
l[i]++;
}else{
if(l[i]){
l[i]--;
r[i]++;
}
}
}
build(1,1,len);
while(n--){
int x,y;cin>>x>>y;
if(x==y){
cout<<0<<endl;
continue;
}
int ans=r[y]-r[x-1];
ans-=max(0,l[x-1]-que(1,x,y,1,len));
cout<<ans*2<<endl;
}
}

CF380C Sereja and Brackets [想法+线段树]的更多相关文章

  1. CF380C. Sereja and Brackets[线段树 区间合并]

    C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...

  2. 【SPOJ61】Brackets(线段树)

    题意:给出一个括号序列,要求维护两种操作: 1.将第x位上的括号取反 2.查询当前整个括号序列是否匹配 n<=3e4 思路:线段树维护区间内没有匹配的左右括号数量 pushup时t[p].r=t ...

  3. CodeForces 380C Sereja and Brackets(扫描线+树状数组)

    [题目链接] http://codeforces.com/problemset/problem/380/C [题目大意] 给出一个括号序列,求区间内左右括号匹配的个数. [题解] 我们发现对于每个右括 ...

  4. 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 ...

  5. Sereja and Brackets CodeForces - 380C (线段树+分治思路)

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

  6. Sereja and Array-数组操作或者线段树或树状数组

    CodeForces - 315B Sereja and Array Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I ...

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

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

  8. POJ 2828 线段树(想法)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 15422   Accepted: 7684 Desc ...

  9. spoj BRCKTS - Brackets 线段树

    题目链接 给一个括号序列, 两种操作. 一种将某个位置的括号变反(左变右, 右变左), 第二种是询问这个括号序列是否合法. 线段树, 我们开两个数组lf, rg. 表示某个区间里面, 右边的左括号个数 ...

随机推荐

  1. 黑马程序员-ReadInt

    判断输入的字符串是否为数字. namespace 读入一个整数 { class Program { static void Main(string[] args) { Console.WriteLin ...

  2. photoshop mac版下载及破解

    1.下载 直接百度photoshop,就可以找到百度的下载源: 2.破解 http://zhidao.baidu.com/question/581955095.html

  3. cocoapods安装失败

    ERROR:  While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the ...

  4. JAVA-4-斐波列

    public class Ch049 { public static void main(String[] args) { // TODO 自动生成的方法存根 int a = 1, b = 1; fo ...

  5. javascript获取地址栏参数/值

    //URL: http://www.example.com/?var1=val1&var2=val2=val3&test=3&test=43&aaa=#2 //wind ...

  6. linux下 oracle常用命令

    打开图形化窗口: 1)Database Configuration Assistant windows    (添加数据库实例) $ dbca 2)Oracle Net Configuration A ...

  7. webAPP前端必备知识

    了解各浏览器内核 Firefox:-moz-box-shadow Safari:-webkit-box-shadow Opera:-o-box-shadow IE:-ms-box-shadow Web ...

  8. 如何写angularJS模块

    angularJS中提供模块的概念,供我们把代码封装在模块单元中,使用模块能给我们带来的好处 保持全局命名空间的清洁 易于在不同应用间复用代码 demo.html <!doctype html& ...

  9. Python 可变对象和不可变对象

    具体可以看这里:http://thomaschen2011.iteye.com/blog/1441254 不可变对象:int,string,float,tuple 可变对象   :list,dicti ...

  10. 从零开始学Sketch——进阶篇-b

    从零开始学Sketch——进阶篇 Sketch是一款矢量绘图应用,而矢量绘图无疑是目前进行网页.图标以及界面设计的最好方式. 在初识了Sketch的界面布局和基础工具之后,我们就可以开始进入高阶的Sk ...