您的位置:首页 > 其它

CSU 1809 Parenthesis

2017-06-25 14:27 351 查看

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
题目大意:给你一个长度为N的字符串,包括两种字符'('和')',有m次操作,每次给两个位置a和b,让你把a和b位置上的东西互换,看换完之后是否依旧匹配
题解:把'('当成1,')'当成-1,求前缀数组,然后用线段树维护一下a到b-1区间是否满足条件就好啦,用ST表也能做,时间有点紧,等有空再写
AC代码:
#include<bits/stdc++.h>using namespace std;const int N = 200000;int a[120000];int tree[N * 3];void update(int rt, int l, int r, int pos, int val){if(l == r){tree[rt] = val;return;}int mid = (l + r) >> 1;if(pos <= mid){update(rt << 1, l, mid, pos, val);}if(pos > mid){update(rt << 1|1, mid + 1, r, pos, val);}tree[rt] = min(tree[rt << 1], tree[rt << 1 | 1]);return;}int query(int rt, int l, int r, int L, int R){if(L <= l && R >= r){return tree[rt];}int mid = (l + r) >> 1, res = 0x3f3f3f3f;if(mid >= L){res = min(res, query(rt << 1, l, mid, L, R));}if(mid < R){res = min(res, query(rt << 1|1, mid + 1, r, L, R));}return res;}int32_t main(){int n, m;while(scanf("%d%d", &n, &m) != EOF){memset(a, 0, sizeof(a));memset(tree, 0, sizeof(tree));int cnt = 1;char temp[200000];scanf("%s", temp);for(int i = 0; i < n; i++){if(temp[i] == '('){a[cnt] = a[cnt - 1] + 1;update(1, 1, n, cnt, a[cnt]);cnt++;}else{a[cnt] = a[cnt - 1] - 1;update(1, 1, n, cnt, a[cnt]);cnt++;}}while(m--){int l, r;scanf("%d%d", &l, &r);if(l>r) swap(l,r);if(temp[l-1] == temp[r-1] || (temp[l-1] == ')' && temp[r-1] == '(')){printf("Yes\n");continue;}if(tree[1] < 0){printf("No\n");continue;}int ans = query(1, 1, n, l, r - 1);if(ans >= 2){printf("Yes\n");}else{printf("No\n");}}}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: