第十二届湖南省赛G - Parenthesis (树状数组维护)
作者:互联网
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).
OutputFor 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 个询问,每一个询问Q,有一个L和R,如果字符串中的L和R位置的两个字符交换后,括号字符串仍然合法的话,那么输出Yes,否则输出No。
思路:
可以用树状数组维护一下,
我们定义如下,如果字符是'(' 我们定他的权值为-1,')' 定义权值为 +1,
我们容易知道,一个合法的字符串的总权值和是0.,并且一个合法的括号串,不存在任意一个位置i,1~i到sum和不大于0。
因为题目给的是一个合法的字符串,那么每一次询问的时候,我们把对应的位置的数值给改变一下,
然后检测如下条件是否成立。
sum(1~l),sum( 1~l+1 ) ,sum(1~r),sum( 1~ r+1 )
需要以上的值均小于等于0,那么这个字符串一定是合法的。
我们可以通过树状数组来做,因为基础的树状数组模板就有单点修改,区间查询的功能。
细节见代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define rt return #define dll(x) scanf("%I64d",&x) #define xll(x) printf("%I64d\n",x) #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define db(x) cout<<"== [ "<<x<<" ] =="<<endl; using namespace std; typedef long long ll; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;} inline void getInt(int* p); const int maxn=100010; const int inf=0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ int tree[maxn]; int n,q; char s[maxn]; int lowbit(int x) { return x&(-1*x); } void add(int x,int k) { for (int i = x; i <=n ; i+=lowbit(i)) { tree[i]+=k; } } int query(int x){ int res=0; while(x>0) { res+=tree[x]; x-=lowbit(x); } return res; } int main() { // freopen("D:\\common_text\\code_stream\\in.txt","r",stdin); //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout); while(~scanf("%d %d",&n,&q)) { MS0(tree); scanf("%s",s+1); repd(i,1,n) { if(s[i]=='(') { add(i,-1); }else { add(i,1); } } int l,r; while(q--) { scanf("%d %d",&l,&r); if(s[l]==s[r]) { printf("Yes\n"); }else { if(s[l]=='(') { // 改 为 ) -1 -> 1 add(l,2); // 1 -> -1 add(r,-2); }else { add(l,-2); add(r,2); } if(query(l)<=0&&query(l+1)<=0&&query(r)<=0&&query(r+1)<=0&&query(n)==0) { printf("Yes\n"); }else { printf("No\n"); } if(s[l]=='(') { add(l,-2); add(r,2); }else { add(l,2); add(r,-2); } } } } return 0; } inline void getInt(int* p) { char ch; do { ch = getchar(); } while (ch == ' ' || ch == '\n'); if (ch == '-') { *p = -(getchar() - '0'); while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 - ch + '0'; } } else { *p = ch - '0'; while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 + ch - '0'; } } }
标签:树状,sum,第十二届,字符串,add,Parenthesis,balanced,include,define 来源: https://www.cnblogs.com/qieqiemin/p/10686320.html