CSU 1809 Parenthesis 【前缀和+RMQ】

6 篇文章 0 订阅
2 篇文章 0 订阅

Description

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

Hint


/*
    题意:给你一个长度为n的括号平衡串和q次询问,每次询问给你a,b,
          表示第a个位置和第b个位置的字符交换,如果交换后仍为括号平衡串,
          输出Yes,否则输出No
    类型:前缀和+RMQ(线段树也行)
    分析:1.当左边的字符与右边的字符一样时,交换一定为括号平衡串
    
          2.当左边的字符为')'时,交换一定为括号平衡串.因为一开始是平衡串,
          如果左边的字符')'与右边的'('交换,那么此时交换的两个必能匹配为一对,
          如果左边的字符')'与右边的')'交换,那么此时交换的两个字符相同,满足1.
          
          3.当左边的字符为'('时,满不满足和右边位置左括号未匹配的个数有关.
          建立前缀和数组a,表示当前位置左括号未匹配的个数,
          当遇到左括号时前缀和+1,右括号时前缀和-1,那么由个例推出规律
          ( ( ( ) ) )
          1 2 3 2 1 0
          当左边的'('与右边的交换能满足仍为括号平衡串的必须是在区间[a,b-1]内
          的最小值>=2
          所以问题转化为求区间[a,b-1]内的前缀和数组最小值是否满足>=2
          可以用基于ST的RMQ算法,也可以用线段树求区间最小值
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
int a[100005];
char zi[100000+100];
int Min[100005][32];
int n;
void rmq_init(){
    for(int i=1;i<=n;i++){
        Min[i][0]=a[i];
    }
    for(int j=1;(1<<j)<=n;j++){
        for(int i=1;i+(1<<j)-1<=n;i++){
            Min[i][j]=min(Min[i][j-1],Min[i+(1<<(j-1))][j-1]);
        }
    }
}
int rmq(int l,int r){
    int k=0;
    while((1<<(k+1))<=r-l+1)k++;
    return min(Min[l][k],Min[r-(1<<k)+1][k]);
}

int main()
{
    int m;
    while(scanf("%d%d",&n,&m)!=EOF){
        scanf("%s",&zi[1]);
        a[0]=0;
        for(int i=1;i<=n;i++){
            if(zi[i]=='(')a[i]=a[i-1]+1;
            else a[i]=a[i-1]-1;
        }
        rmq_init();
        for(int i=1;i<=m;i++){
            int b,c;
            scanf("%d%d",&b,&c);
            if(b>c)swap(b,c);
            if(zi[b]==zi[c]||(zi[b]==')'&&zi[c]=='(')){
                printf("Yes\n");
                continue;
            }
            if(rmq(b,c-1)>=2)printf("Yes\n");
            else printf("No\n");
        }
    }
}












  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值