Description
1 p 2…p n
ai and p bi
Parenthesis sequence S is balanced if and only if:
S is empty;
or there exists balanced parenthesis sequence A,B such that S=AB;
or there exists balanced parenthesis sequence S' such that S=(S').
Input
The input contains at most 30 sets. For each set:
5,1≤q≤10 5).
1 p 2…p n.
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
#include<set>标签:lg,const,int,1809,balanced,Parenthesis,CSU,include,define From: https://blog.51cto.com/u_15870896/5838621
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define ff first
#define ss second
#define mp(i,j) make_pair(i,j)
#define pb push_back
#define pii pair<int,LL>
#define in(x) scanf("%d", &x);
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-9;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int T, n, m, l, r;
char s[N];
int f[N][20], lg[N];
int rmq(int l, int r)
{
int k = lg[r - l + 1];
return min(f[l][k], f[r - (1 << k) + 1][k]);
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
scanf("%s", s + 1);
int x = 0; lg[0] = -1;
rep(i, 1, n)
{
x += s[i] == '(' ? 1 : -1;
f[i][0] = x; lg[i] = lg[i / 2] + 1;
}
for (int j = 1; (1 << j) <= n; j++)
{
rep(i, 1, n)
{
if (i + (1 << j - 1) > n) break;
f[i][j] = min(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
}
}
while (m--)
{
scanf("%d%d", &l, &r);
if (l > r) swap(l, r);
if (s[l] == s[r] || s[l] == ')') { printf("Yes\n"); continue; }
if (rmq(l, r - 1) - 2 < 0) printf("No\n"); else printf("Yes\n");
}
}
return 0;
}