CF380C Sereja and Brackets [想法+线段树]
题意:
给出一串括号
给出一些询问,问某个区间[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 [想法+线段树]的更多相关文章
- CF380C. Sereja and Brackets[线段树 区间合并]
C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 【SPOJ61】Brackets(线段树)
题意:给出一个括号序列,要求维护两种操作: 1.将第x位上的括号取反 2.查询当前整个括号序列是否匹配 n<=3e4 思路:线段树维护区间内没有匹配的左右括号数量 pushup时t[p].r=t ...
- CodeForces 380C Sereja and Brackets(扫描线+树状数组)
[题目链接] http://codeforces.com/problemset/problem/380/C [题目大意] 给出一个括号序列,求区间内左右括号匹配的个数. [题解] 我们发现对于每个右括 ...
- 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 ...
- Sereja and Brackets CodeForces - 380C (线段树+分治思路)
Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...
- Sereja and Array-数组操作或者线段树或树状数组
CodeForces - 315B Sereja and Array Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I ...
- Sereja and Brackets CodeForces - 380C (树状数组+离线)
Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...
- POJ 2828 线段树(想法)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 15422 Accepted: 7684 Desc ...
- spoj BRCKTS - Brackets 线段树
题目链接 给一个括号序列, 两种操作. 一种将某个位置的括号变反(左变右, 右变左), 第二种是询问这个括号序列是否合法. 线段树, 我们开两个数组lf, rg. 表示某个区间里面, 右边的左括号个数 ...
随机推荐
- 图解MonoForAndroid开发环境搭建
电脑系统:windows 8.1 企业版 预装VS:2010旗舰版+2013 with update2旗舰版 ==================================== 1.1 安装ja ...
- repeater控件 + marquee标签 实现文字滚动显示
各种信息网站.BBS等网站上的公告信息模块的实现 拖出一个repeater控件绑定数据库中要显示的信息 在repeater的 <ItemTemplate> ... </ItemTem ...
- 为什么z-index不起作用
感觉很简单的东西 在用的时候 可能会遇到这样活那样的问题 这就是要注意细节:参考地址:http://www.ourjour.com/136/ 设置z-index 不起作用,可能是这三个原因: 1.父标 ...
- java获取对象属性类型、属性名称、属性值 【转】
/** * 根据属性名获取属性值 * */ private Object getFieldValueByName(String fieldName, Object o) { try { String ...
- 【转】iOS申请发布证书-图文详解
摘要 发布产品到App Store所需证书,2013年5月26日测试 IOS 发布证书 distribution 打包程序 真机调试证书 本文讲述发布证书的申请,申请真机调试证书请参考:http:// ...
- iOS App上传中遇到的问题
1. 今天打包上传文件时出现“Missing iOS Distribution signing identity for XXXX” 导致问题的原因是:下边这个证书过期了 以下是苹果官方给出的回应: ...
- UICollectionView出现the behavior of the UICollectionViewFlowLayout is not defined because:
2015-01-28 21:55:17.790 Demo[636:9351] the behavior of the UICollectionViewFlowLayout is notdefined ...
- Swift - 10 - assert(断言)
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
- 测试最新的log4cplus1.1.2版
#include "stdafx.h" #include <sstream> class AB{ public: void do_test() { ...
- C#三种方式实现序列化(转)
序列化和反序列化我们可能经常会听到,其实通俗一点的解释,序列化就是把一个对象保存到一个文件或数据库字段中去,反序列化就是在适当的时候把这个文件再转化成原来的对象使用. 序列化和反序列化最主要的作用有: ...