Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of length n and q questions.
The i-th question is whether P remains balanced after p ai and p bi  swapped. Note that questions are individual so that they have no affect on others.
Parenthesis sequence S is balanced if and only if:
1. S is empty;
2. or there exists balanced parenthesis sequence A,B such that S=AB;
3. or there exists balanced parenthesis sequence S' such that S=(S').

题意就是给出一个正确配对的括号序列,问交换两个括号以后是否依旧正确配对,一般括号配对都可以使用遇到左括号加一,遇到右括号减一的方法,中间过程中不能出现负值,否则配对失败,这道题也可以这样做,先求出前缀和,如:

编号: 1    2   3  4   5   6    7   8

         (    (    (    )    )    )    (    )

前缀: 1   2    3  2   1   0     1  0

有两种仍然可以配对的交换:1.当交换的两个括号相同时,2.ai是右括号,bi是左括号时,根据示例可以看出;

唯一一种可能发生不配对的交换:ai是左括号,bi是右括号;当有右括号加入ai位置时,从ai位置到bi-1位置的前缀和全部都要减2,所以ai到bi-1区间内最小值至少为2,这样就变成了查询区间最小值问题了,可以用线段树,也可以RMQ(代码待补);因为刚刚学习线段树,又刚好听说ZKW线段树代码比较精简,所以临时现去学习了一下,结果发现好多给的示例代码都是错的。。。这个版本可能也有错,但是AC了就好。。。哈哈哈哈哈哈哈哈哈嗝。。。。

zkw线段树:

#include<iostream>
#include<cstring>
#include<cstdio> using namespace std; const int N = + ;
const int INF = (<<);
int sum[N],T[N<<],cnt,M;
char ch[N]; void Build(int n){
int i;
for(M=;M<=n+;M*=);
for(i=+M;i<=n+M;i++) T[i] = sum[cnt++];
for(i=M-;i;i--) T[i]=min(T[i<<],T[i<<|]);
} void Update(int n,int V){
for(T[n+=M]=V,n/=;n;n/=)
T[n]=min(T[n<<],T[n<<|]);
} int Query(int s,int t){
int minc=INF;
for(s=s+M-,t=t+M+;s^t^;s/=,t/=){
if(~s&) minc=min(minc,T[s^]);
if(t&) minc=min(minc,T[t^]);
}
return minc;
}
int main(){
int n,q,a,b;
while(scanf("%d %d",&n,&q)==){
scanf("%s",ch+);
sum[] = ;
for(int i=;i<=n;i++){
if(ch[i]=='(') sum[i]=sum[i-]+;
else sum[i]=sum[i-]-;
}
cnt = ;
Build(n);
for(int i=;i<q;i++){
scanf("%d %d",&a,&b);
if(a > b) swap(a,b);
if(ch[a]==ch[b] || (ch[a]==')'&&ch[b]=='(')){ printf("Yes\n"); continue; }
int ret = Query(a,b-);
printf("%s\n",ret>=?"Yes":"No");
}
}
return ;
}


RMQ:

#include<iostream>
#include<cstring>
#include<cstdio> using namespace std; const int N = + ;
const int INF = (<<);
int sum[N],dp[N][],cnt,n;
char ch[N];
void RMQ_Init(){
memset(dp,,sizeof(dp));
for(int i=;i<n+;i++) dp[i][] = sum[i];
for(int j=;(<<j)<=n+;j++)
for(int i=;i+(<<j)- < n+;i++)
dp[i][j] = min(dp[i][j-],dp[i+(<<(j-))][j-]);
}
int RMQ(int L,int R){
int k = ;
while((<<(k+))<=R-L+) k++;
return min(dp[L][k],dp[R-(<<k)+][k]);
}
int main(){
int q,a,b;
while(scanf("%d %d",&n,&q)==){
scanf("%s",ch+);
sum[] = ;
for(int i=;i<=n;i++){
if(ch[i]=='(') sum[i]=sum[i-]+;
else sum[i]=sum[i-]-;
}
cnt = ;
RMQ_Init();
for(int i=;i<q;i++){
scanf("%d %d",&a,&b);
if(a > b) swap(a,b);
if(ch[a]==ch[b] || (ch[a]==')'&&ch[b]=='(')){ printf("Yes\n"); continue; }
int ret = RMQ(a,b-);
printf("%s\n",ret>=?"Yes":"No");
}
}
return ;
}
 


V-Parenthesis 前缀+ZKW线段树或RMQ的更多相关文章

  1. Parenthesis(前缀和+线段树)

    1809: Parenthesis Time Limit: 5 Sec     Memory Limit: 128 Mb     Submitted: 2291     Solved: 622 Des ...

  2. zkw线段树详解

    转载自:http://blog.csdn.net/qq_18455665/article/details/50989113 前言 首先说说出处: 清华大学 张昆玮(zkw) - ppt <统计的 ...

  3. HDU 4366 Successor(树链剖分+zkw线段树+扫描线)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4366 [题目大意] 有一个公司,每个员工都有一个上司,所有的人呈树状关系,现在给出每个人的忠诚值和 ...

  4. [SinGuLaRiTy] ZKW线段树

    [SinGuLaRiTy-1007] Copyrights (c) SinGuLaRiTy 2017. All Rights Reserved. 关于ZKW线段树 Zkw线段树是清华大学张昆玮发明非递 ...

  5. 线段树(单标记+离散化+扫描线+双标记)+zkw线段树+权值线段树+主席树及一些例题

    “队列进出图上的方向 线段树区间修改求出总量 可持久留下的迹象 我们 俯身欣赏” ----<膜你抄>     线段树很早就会写了,但一直没有总结,所以偶尔重写又会懵逼,所以还是要总结一下. ...

  6. ZKW线段树入门

    Part 1 来说说它的构造 线段树的堆式储存 我们来转成二进制看看 小学生问题:找规律 规律是很显然的 一个节点的父节点是这个数左移1,这个位运算就是低位舍弃,所有数字左移一位 一个节点的子节点是这 ...

  7. BZOJ3173 TJOI2013最长上升子序列(Treap+ZKW线段树)

    传送门 Description 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? Input ...

  8. 数据结构3——浅谈zkw线段树

    线段树是所有数据结构中,最常用的之一.线段树的功能多样,既可以代替树状数组完成"区间和"查询,也可以完成一些所谓"动态RMQ"(可修改的区间最值问题)的操作.其 ...

  9. 有趣的 zkw 线段树(超全详解)

    zkw segment-tree 真是太棒了(真的重口味)!写篇博客纪念入门 emmm...首先我们来介绍一下 zkw 线段树这个东西(俗称 "重口味" ,与 KMP 类似,咳咳. ...

随机推荐

  1. java定义时间

    import java.text.SimpleDateFormat; import java.util.Date; SimpleDateFormat format=new SimpleDateForm ...

  2. linux运维、架构之路-实时同步方案

    一.inotify+rsync实时同步 1.介绍         inotify-tools是一种强大的.细粒度的.异步的文件系统事件监控机制,可以用来监控文件系统的事件.inotify-tools是 ...

  3. spring中试用junit4测试

    一:加入jar包 <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId& ...

  4. B/S实现大视频上传

    在公司做B/S 开发与维护三年啦, 对B/S架构的了解也是只知大概,对于这种基础知识还是很有必要理一理哒.趁空去网上查阅了资料,顺便整理一份笔记供以后查询. 一. B/S的概念 B/S(Brower/ ...

  5. web移动端适配方案

    web移动端常用解决方案: 一.通过js+rem,这里有一个解决方案(http://imochen.github.io/hotcss/) 1.1.rem兼容性(https://caniuse.com) ...

  6. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  7. AutoCAD2008换硬盘后重新激活

    1.打开“C:\Documents and Settings\All Users\Application Data\Autodesk\Software Licenses”,删除文件夹里面的所有*.da ...

  8. RedisTemplate访问Redis数据结构(四)——Set

    Redis的Set是string类型的无序集合.集合成员是唯一的,这就意味着集合中不能出现重复的数据,Redis 中 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1). SetOper ...

  9. WWDC2014代码和视频下载

    WWDC2014 sample code 地址 http://pan.baidu.com/s/1qWGznnY WWDC2014 videos 地址 https://github.com/liubin ...

  10. Java数据结构与算法(1):线性表

    线性表是一种简单的数据类型,它是具有相同类型的n个数据元素组成的有限序列.形如如A0,A1,...,An-1.大小为0的表为空表,称Ai后继Ai-1,并称Ai-1前驱Ai. printList打印出表 ...