题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1809

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').

Input

The input contains at most 30 sets. For each set:
The first line contains two integers n,q (2≤n≤10 5,1≤q≤10 5).
The second line contains n characters p 1 p 2…p n.
The i-th of the last q lines contains 2 integers a i,b i (1≤a i,b i≤n,a i≠b i).

Output

For each question, output " Yes" if P remains balanced, or " No" otherwise.

Sample Input

4 2
(())
1 3
2 3
2 1
()
1 2

Sample Output

No
Yes
No

题意:

现在给出长度为n的一串圆括号串,保证是平衡的括号串;

再给出q个查询,每个查询有两个值a,b,代表询问交换P[a]和P[b],会不会使得括号串变得不平衡。

题解:

首先,我们定义一个preSum数组,代表了这括号序列的前缀和,遇到一个'('就加上1,遇到一个')'就减去1;

这样一来,该括号序列为平衡序列 $\Leftrightarrow$ preSum[n]==0,且对于$\forall$i=1~n都有preSum[i]≥0;

那么我们有以下三种情况:

  1. P[a]==P[b],这样情况显然交换一下和原来没有区别,显然是保持平衡的;
  2. P[a]=')'且P[b]='(',这种情况下,preSum[1]~preSum[a-1]不会有任何变动,preSum[a]~preSum[b-1]都会$+=2$,preSum[b]~preSum[n]也不会有任何变动,这样一来,交换后产生的新的括号序列,依然满足preSum[n]==0,且对于$\forall$i=1~n都有preSum[i]≥0,那么显然该括号序列还是平衡序列;
  3. P[a]='('且P[b]=')',这种情况下,preSum[1]~preSum[a-1]不会有任何变动,preSum[a]~preSum[b-1]都会$-=2$,preSum[b]~preSum[n]不会有任何变动,那么显然关键就在preSum[a]~preSum[b-1]上了,我们知道一旦有一个preSum[i]<0,这个括号序列就不平衡了,所以我们必须保证preSum[a]~preSum[b-1]都大于等于2才行。

所以,我们可以使用线段树或者RMQ维护preSum[]数组的区间最小值,对每次查询只要查询出[a,b]区间内preSum[i]的最小值有没有比2大就可以了。

AC代码:

①线段树:

#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
typedef long long LL; const int maxn=1e5+;
const int INF=0x3f3f3f3f; int n,q;
char str[maxn];
int preSum[maxn]; struct Node{
int l,r;
int val;
}node[*maxn];
void pushup(int root)
{
node[root].val=min(node[root*].val,node[root*+].val);
}
void build(int root,int l,int r)
{
node[root].l=l; node[root].r=r;
if(l==r) node[root].val=preSum[l];
else
{
int mid=l+(r-l)/;
build(root*,l,mid);
build(root*+,mid+,r);
pushup(root);
}
}
int query(int root,int st,int ed)
{
if(ed<node[root].l || node[root].r<st) return INF;
if(st<=node[root].l && node[root].r<=ed) return node[root].val;
else return min(query(root*,st,ed),query(root*+,st,ed));
} int main()
{
while(scanf("%d%d",&n,&q)!=EOF)
{
scanf("%s",str+); preSum[]=;
for(int i=;i<=n;i++) preSum[i]=preSum[i-]+(str[i]=='('?:-); build(,,n);
for(int i=,a,b;i<=q;i++)
{
scanf("%d%d",&a,&b);
if(a>b) swap(a,b); if( str[a]==str[b] || (str[a]==')' && str[b]=='(') ) printf("Yes\n");
else if(query(,a,b-)>=) printf("Yes\n");
else printf("No\n");
}
}
}

②RMQ:

#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL; const int maxn=1e5+;
const int INF=0x3f3f3f3f; int n,q;
char str[maxn];
int preSum[maxn]; struct _RMQ{
int Mnum[maxn][]; //int(log(maxn)/log(2.0))
void init(int num[])
{
for(int i=;i<=n;i++) Mnum[i][]=num[i];
int j_max=(log(n)/log());
for(int j=;j<=j_max;j++)
{
for(int i=;i<=n;i++)
{
if(i+(<<j)- <= n)
Mnum[i][j]=min(Mnum[i][j-],Mnum[i+(<<(j-))][j-]);
}
}
}
int query(int l,int r)
{
int k=log(r-l+)/log();
return min(Mnum[l][k],Mnum[r-(<<k)+][k]);
}
}RMQ; int main()
{
while(scanf("%d%d",&n,&q)!=EOF)
{
scanf("%s",str+); preSum[]=;
for(int i=;i<=n;i++) preSum[i]=preSum[i-]+(str[i]=='('?:-); RMQ.init(preSum);
for(int i=,a,b;i<=q;i++)
{
scanf("%d%d",&a,&b);
if(a>b) swap(a,b); if( str[a]==str[b] || (str[a]==')' && str[b]=='(') ) printf("Yes\n");
else if(RMQ.query(a,b-)>=) printf("Yes\n");
else printf("No\n");
}
}
}

CSU 1809 - Parenthesis - [前缀和+维护区间最小值][线段树/RMQ]的更多相关文章

  1. 区间最小值 线段树 (2015年 JXNU_ACS 算法组暑假第一次周赛)

    区间最小值 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submiss ...

  2. dutacm.club_1094_等差区间_(线段树)(RMQ算法)

    1094: 等差区间 Time Limit:5000/3000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/Others)Total ...

  3. tyvj 1038 忠诚 区间最小值 线段树或者rmq

    P1038 忠诚 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 老管家是一个聪明能干的人.他为财主工作了整整10年,财主为了让自已账目更加清楚.要求管家每天 ...

  4. CSU 1809 Parenthesis(线段树+前缀和)

    Parenthesis Problem Description: Bobo has a balanced parenthesis sequence P=p1 p2-pn of length n and ...

  5. hdu 5700区间交(线段树)

    区间交 Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...

  6. Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树

    https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...

  7. 【题解】P1712 [NOI2016]区间(贪心+线段树)

    [题解]P1712 [NOI2016]区间(贪心+线段树) 一个observe是,对于一个合法的方案,将其线段长度按照从大到小排序后,他极差的来源是第一个和最后一个.或者说,读入的线段按照长度分类后, ...

  8. 【BZOJ4653】【NOI2016】区间(线段树)

    [BZOJ4653][NOI2016]区间(线段树) 题面 BZOJ 题解 \(NOI\)良心送分题?? 既然是最大长度减去最小长度 莫名想到那道反复减边求最小生成树 从而求出最小的比值 所以这题的套 ...

  9. BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针

    BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间, ...

随机推荐

  1. 【代码审计】iCMS_v7.0.7 admincp.app.php页面存在SQL注入漏洞分析

      0x00 环境准备 iCMS官网:https://www.icmsdev.com 网站源码版本:iCMS-v7.0.7 程序源码下载:https://www.icmsdev.com/downloa ...

  2. RF使用ie浏览器访问页面,浏览器启动只显示This is the initial start page for the WebDriver server,页面访问失败

    问题描述:启动ie浏览器后,页面显示如下: 问题定位: 1.IE页面缩放没有设置成100% 2.ie浏览器的安全模式设置是否都将“启动保护模式”勾选上 3.iedriver驱动版本号是否和seleni ...

  3. Qt打包部署程序自动查找依赖DLL工具windeployqt

    qt编译好一个exe程序之后,部署到一台没有开发环境的机器上,需要一起拷贝其依赖的dll文件.这时需要一个windeployqt工具来帮忙,因为手动拷贝的话容易遗漏. https://blog.csd ...

  4. Android studio导入eclipse工程时出现中文全部乱码问题

    File->settings->Copyright的下面File Encoding -> 改成gbk这样修改就OK了

  5. 64位Oracle 11g 使用PL/SQL

    Oracle 11g和PL/SQL安装完后,发现打开PL/SQL并不能连接Oracle数据库! [第一回合]完败! 先是在网上找解决方法,说是需要使用Net Configuration Assista ...

  6. 在 Ubuntu 13.10 安装 PyCharm 3.0.1 & Oracle JDK

    由于授权问题,在较新的Linux发行版本中都不再包含Oracle Java,取而代之的是OpenJDK.Ubuntu也是如此. OpenJDK能满足大部分的应用程序运行条件,但PyCharm无法在Op ...

  7. Android开发训练之第五章第五节——Resolving Cloud Save Conflicts

    Resolving Cloud Save Conflicts IN THIS DOCUMENT Get Notified of Conflicts Handle the Simple Cases De ...

  8. Delphi之Code Explorer

    Code Explorer(代码浏览器)是Delphi IDE的特性之一,它大受用户的欢迎.正如其名所表示,Code Explorer用于快速浏览源代码单元.Code Explorer通常位于Code ...

  9. Esper学习之八:EPL语法(四)

    关于EPL,已经写了三篇了,预估计了一下,除了今天这篇,后面还有5篇左右.大家可别嫌多,官方的文档对EPL的讲解有将近140页,我已经尽量将废话都干掉了,再配合我附上的例子,看我的10篇文章比那140 ...

  10. 【Spring Boot&&Spring Cloud系列】Spring Boot中使用数据库之MySql

    对于传统关系型数据库来说,Spring Boot使用JPA(Java Persistence API)资源库提供持久化的标准规范,即将Java的普通对象通过对象关系映射(ORM)持久化到数据库中. 项 ...