hhuoj Mouse and Parenthesis 线段树求局部最小值 TWT Tokyo Olymipic 2COMBO -1 未完待续

来源:互联网 发布:张国荣同性恋 知乎 编辑:程序博客网 时间:2024/06/18 00:38

问题 E: Mouse and Parenthesis

时间限制: 1 Sec  内存限制: 128 MB
提交: 31  解决: 7
[提交][状态][讨论版]

题目描述

Tom has m same balanced parenthesis sequence P=p1 p2…pn of length n.

This day Jerry comes into Tom's room and swaps one pair of parenthesis in every sequence.

Tom and Jerry both like balanced parenthesis sequence, so Jerry wants to know whether each P remains balanced after pai and pbi  swapped. 


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

输入

The first line contains an integers T (T≤20), which indicates the number of test cases. 

For each case:

The first line contains two integers n,m.
The second line contains n characters p1 p2…pn.

The i-th of the last m lines contains 2 integers ai,bi (1≤ai,bi≤n,ai≠bi).


⋅ for 50% data, 1≤n≤50,1≤q≤1000.

⋅ for 100% data, 1≤n≤100000,1≤q≤100000.

输出

For every test case, you should output "Case #x:", where x indicates the case number and counts from 1. Then in ith line output 'Yes' if ith parenthesis sequence is balanced, otherwise 'No'. 

样例输入

24 2(())1 32 32 1()1 2

样例输出

Case #1:NoYesCase #2:No




/* ━━━━━┒ ┓┏┓┏┓┃μ'sic foever!! ┛┗┛┗┛┃\○/ ┓┏┓┏┓┃ / ┛┗┛┗┛┃ノ) ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┃┃┃┃┃┃ ┻┻┻┻┻┻ */#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>using namespace std;const int maxn =1e5;int number[maxn+5];char chr[maxn+5];int a[maxn+5];struct unit{    int l;    int r;    int sum;}save[(maxn+5)*9];void build(int l,int r,int index){    save[index].l=l;    save[index].r=r;    if(l==r){        save[index].sum=a[l];        return;    }    int middle = (l+r)/2;    build(l, middle, 2*index);    build(middle+1, r, 2*index+1);    save[index].sum=min(save[2*index].sum,save[2*index+1].sum);}int getSum(int l,int r,int index){    if(save[index].l==l&&save[index].r==r){        return save[index].sum;    }    int middle = (save[index].l+save[index].r)/2;    if(middle<l){        return getSum(l, r, 2*index+1);    }else if(middle>=r){        return getSum(l,r,2*index);    }else{        return min(getSum(l, middle, 2*index),getSum(middle+1, r, 2*index+1));    }}int main(){    int t;    scanf("%d",&t);    int rnd=1;    while(t--){        int n,m,i;        string in;        scanf("%d%d",&n,&m);        scanf("%s",chr+1);        printf("Case #%d:\n",rnd);        memset(a,0,sizeof(a));        a[0]=0;        for(int j=1;j<=n;j++){            a[j]=a[j-1]+(chr[j]=='('?1:-1);        }        build(1, n, 1);        for(i=0;i<m;i++){            int p1,p2;            scanf("%d%d",&p1,&p2);            if(chr[p1]==chr[p2]){                printf("Yes\n");                continue;            }            int left=min(p1,p2),right=max(p1,p2);            if(chr[left]==')'){                printf("Yes\n");                continue;            }            int ans=getSum(left, right-1, 1);            if(ans>=2){                printf("Yes\n");            }else{                printf("No\n");            }        }        rnd++;    }    return 0;}





0 0
原创粉丝点击