首页 > 其他分享 >CSU 1809 Parenthesis

CSU 1809 Parenthesis

时间:2022-11-09 19:32:59浏览次数:42  
标签:lg const int 1809 balanced Parenthesis CSU include define


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>
#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;
}

标签:lg,const,int,1809,balanced,Parenthesis,CSU,include,define
From: https://blog.51cto.com/u_15870896/5838621

相关文章

  • CSU 1804 有向无环图
    DescriptionBobo有一个n个点,m条边的有向无环图(即对于任意点v,不存在从点v开始、点v结束的路径)。为了方便,点用1,2,…,n编号。设count(x,y)表示点x......
  • CSU 1803 2016
    Description 给出正整数n和m,统计满足以下条件的正整数对(a,b)的数量:1.1≤a≤n,1≤b≤m;2.a×b是2016的倍数。Input......
  • csu 1554: SG Value 思维题
    ​​http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1554​​这题在比赛的时候居然没想出来,然后发现居然是做过的题目的变种!!!!先不考虑插入操作,就给定一堆数字,求出不能......
  • D - Simple String CSU - 1550
    ​​http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1550​​很久都没补这题,最近想学网络流,就看看,队友以前用网络流过的,Orz,但是这题只需要简单的判断,可能想起来有点麻......
  • csu 1552: Friends 二分图 + Miller_Rabin
    ​​http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1552​​把那n个数写两次,分成相同的两堆,判断相加是质数的,连一条边,然后找最大匹配,ans=最大匹配/2做的时候一直......
  • csu 1551: Longest Increasing Subsequence Again BIT + 思维
    预处理last[i]表示以第i个开始,的合法后缀。pre[i]表示以第i个结尾,的合法前缀。那么每一个数a[i],肯定是一个合法后缀last[i]+一个合法前缀,那么合法前缀的数字要小于a[i],并......