首页 技术 正文
技术 2022年11月21日
0 收藏 702 点赞 4,921 浏览 2295 个字

1809: Parenthesis

Submit Page     Summary    Time Limit: 5 Sec     Memory Limit: 128 Mb     Submitted: 1500     Solved: 398


Description

Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q questions.The i-th question is whether P remains balanced after pai and pbi  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≤105,1≤q≤105).The second line contains n characters p1 p2…pn.The i-th of the last q lines contains 2 integers ai,bi (1≤ai,bi≤n,ai≠bi).

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

Hint

Source

湖南省第十二届大学生计算机程序设计竞赛

题目大意:

给你一个长度为N的括号匹配串,然后有q个查询,表示交换两个位子上的括号之后,询问你字符串还是否构成括号匹配。

思路:

首先预处理出一个前缀和sum【i】,设定(表示1,)表示-1.那么我们开始分类讨论:

①a【x】==a【y】,那么结果一定是Yes.

②a【x】==‘)’&&a【y】==‘(’,那么结果也一定是Yes.因为如果左边的右括号放置在了右边,可以和放置在左边的这个左括号进行匹配,原串保证是匹配的,所以这样交换的结果也一定是Yes.

③a【x】==‘(’&&a【y】==‘)’,那么考虑对前缀和的影响:

撤销x位子上的左括号:从x————–>n 前缀和全部减去1

撤销y位子上的右括号:从y————–>n 前缀和全部加上1

放置x位子上那个右括号:从x———–>n 前缀和全部减去1

放置y位子上那个左括号:从y———–>n 前缀和全部加上1

显然,如果有某个前缀和的位子上出现了负数,那么此时这个字符串一定是构不成括号匹配的,所以我们只要判定影响到的部分是否会出现负数即可。

显然,从y———->n的部分都加上了2,从x———->y-1的部分都减去了1.

那么我们只要查询之前前缀和中区间【x,y-1】中的最小值是否大于等于2即可,如果是,结果就是Yes.否则就是No.

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 100050
#define inf_int 2e9
int sum[maxn*],val[maxn];
char s[maxn];
void push_up(int i){
sum[i] = min(sum[i<<],sum[i<<|]);
}
void build(int i,int l,int r){
if(l==r){
sum[i] = val[l];
return;
}
int mid = (l+r) >> ;
build(i<<,l,mid);
build(i<<|,mid+,r);//i<<1|1 把i左移1位,然后和1按位或
push_up(i);
}
int querry(int i,int l,int r,int L,int R){
if(L<=l && R>=r)
return sum[i];
int ans = inf_int;
int mid = (l+r) >> ;
if(L<=mid)
ans = min(ans,querry(i<<,l,mid,L,R));
if(R>mid)
ans = min(ans,querry(i<<|,mid+,r,L,R));
return ans;
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)) {
scanf("%s",s+);
memset(val,,sizeof(val));
for(int i=;i<=n;i++){
if(s[i]=='(') val[i] = val[i-] + ;
else val[i] = val[i-] - ;
}
build(,,n);
while(m--){
int l,r;
scanf("%d%d",&l,&r);
if(l>r)
swap(l,r);
if(s[l]=='(' && s[r]==')'){
if(querry(,,n,l,r-)<)
printf("No\n");
else printf("Yes\n");
}
else printf("Yes\n");
}
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,036
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,522
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,370
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,151
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,784
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,866