CSU 1809 Parenthesis (线段树)【2016年湖南省第十二届大学生计算机程序设计竞赛 - G】

题目点我点我点我


1809: Parenthesis

Time Limit: 5 Sec   Memory Limit: 128 MB
Submit: 513   Solved: 111
[ Submit][ Status][ Web Board]

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

Source




题目大意:给了一个平衡的括号序列s(平衡是指括号匹配正常) 现在q次询问,每次输入两个数a、b  问将s[a]  s[b]交换后是否任然平衡(即是否正常匹配),平衡则输出“Yes”  否则输出“No”;


解题思路:括号匹配问题的经典做法就是把序列转化成前缀和,‘(’就等于前一个+1,‘)’就等于后一个-1,所以正常的序列的前缀和一定是>=0的.由于一开始序列是平衡的,那么,只有当s[l]=='(' && s[r]==')'时(l<=r)才有可能不平衡,并且只会对前缀和val[i]产生影响的区间为val[l]~val[r-1],所以就可以用线段树来求val[l]~val[r-1]的最小值,如果val[l]~val[r-1]的前缀和小于2的话就是No,因为s[l]=='(' && s[r]==')'就换后,val[l]~val[r-1]就会减少2.



/* ***********************************************
┆  ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆  ┃ 勒 ┃  ┆      
┆  ┃ 戈 ┗━━━┓ ┆
┆  ┃ 壁     ┣┓┆
┆  ┃ 的草泥马  ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <bitset>
using namespace std;

#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
#define pb push_back
#define mp make_pair
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define all(a) a.begin(), a.end()
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")

const int N = 1e5 + 5;

#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r

int sum[N*3],val[N];
char s[N];

void push_up(int i)
{
    sum[i] = min(sum[i<<1],sum[i<<1|1]);
}

void build(int i,int l,int r)
{
    if(l==r)
    {
        sum[i] = val[l];
        return;
    }
    int mid = (l+r) >> 1;
    build(lson);
    build(rson);
    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) >> 1;
    if(L<=mid)ans = min(ans,querry(lson,L,R));
    if(R>mid) ans = min(ans,querry(rson,L,R));
    return ans;
}


int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,m;
	while(~scanf("%d%d",&n,&m))  
    {
        scanf("%s",s+1);
        for(int i=1;i<=n;i++)
        {
            if(s[i]=='(')val[i] = val[i-1] + 1;
            else val[i] = val[i-1] - 1;
        }
        build(1,1,n);
        while(m--)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            if(l>r)swap(l,r);
            if(s[l]=='(' && s[r]==')')
            {
                if(querry(1,1,n,l,r-1)<2)
                    printf("No\n");
                else printf("Yes\n");
            }
            else printf("Yes\n");
        }
    }


	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值