1809: Parenthesis

Submit      Status     Time Limit: 5 Sec     Memory Limit: 128 Mb     Submitted: 1149     Solved: 326


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

题目链接:CSU 1809

下午又被同学叫去做做题,大约是湖南省某套题目,这回太excited的啦,直接爆零滚粗,然后各种查题解,虽然发现题目难度确实不小且解题程序代码量基本都比较大(其中感觉最骚的是那道地铁,没见过这么骚的建模),但是还是想做几题,然后晚上没事儿看看其他OJ突然发现某篇另外的其他地方的比赛题解里有讲到一种简单的判断括号序列的方法:记左括号为1,右括号为-1,求出前缀和,若结尾为0且前缀和序列没有负数,则说明括号序列合法,然后又想到这题应该可以好好利用这个方法做

那么跟这一题有什么关系呢?考虑题目中所说交换的情况,共四种,除去很容易想到的等价的交换两种(左括号交换左括号,右括号交换右括号),还有左-右与右-左两种,然后我们放到前缀和序列上来考虑,首先你得特判,若开头或结尾处被交换必定不合法(此处已除去等价交换的情况),因为开头必定是'('而结尾必定是')';然后再考虑中间的交换,我们方便一点令a<b(交换本来就是可逆的,a、b互换不影响结果),若'('与')'交换,则说明1被换到后面,-1被换到前面,这里我们分为三步,先把1拿出来,再把-1放到a位置,再把1放到b位置,我们会发现前缀$prefix_{a...b-1}$改变了-2,而$prefix_{b...n}$显然是不变的,那么只需判断改变前的一段是否均大于等于2以至于均-2后不会出现0;再考虑另外一种')'与'('交换,若按前面的前缀和打个草稿会发现这样做一定是合法的,改变一段的值一定为正,那么这题就用RMQ-ST就可以了,代码量不大,但是由于第二种情况忘记分开讨论了Debug很久……

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1e5 + 7;
char s[N];
int prefix[N], Min[N][18]; void init()
{
prefix[0] = 0;
CLR(Min, INF);
}
void RMQ_init(int l, int r)
{
int i, j;
for (i = l; i <= r; ++i)
Min[i][0] = min<int>(Min[i][0], prefix[i]);
for (j = 1; l + (1 << j) - 1 <= r; ++j)
{
for (i = l; i + (1 << j) - 1 <= r; ++i)
{
Min[i][j] = min<LL>(Min[i][j - 1], Min[i + (1 << (j - 1))][j - 1]);
}
}
}
int ST(int l, int r)
{
int k = log2(r - l + 1.0);
return min(Min[l][k], Min[r - (1 << k) + 1][k]);
}
int main(void)
{
int n, q, i;
while (~scanf("%d%d", &n, &q))
{
init();
scanf("%s", s + 1);
for (i = 1; i <= n; ++i)
prefix[i] = prefix[i - 1] + (s[i] == '(' ? 1 : -1);
RMQ_init(1, n);
while (q--)
{
int a, b;
scanf("%d%d", &a, &b);
if (a > b)
swap(a, b);
if (s[a] == s[b])
puts("Yes");
else
{
if (a == 1 || b == n || (ST(a, b - 1) < 2 && s[a] == '(' && s[b] == ')'))
puts("No");
else
puts("Yes");
}
}
}
return 0;
}

CSU 1809 Parenthesis(RMQ-ST+思考)的更多相关文章

  1. CSU 1809 Parenthesis(线段树+前缀和)

    Parenthesis Problem Description: Bobo has a balanced parenthesis sequence P=p1 p2-pn of length n and ...

  2. CSU 1809 Parenthesis 思维+线段树

    1809: Parenthesis Submit Page     Summary    Time Limit: 5 Sec     Memory Limit: 128 Mb     Submitte ...

  3. CSU 1809 - Parenthesis - [前缀和+维护区间最小值][线段树/RMQ]

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1809 Bobo has a balanced parenthesis sequenc ...

  4. 【贪心】CSU 1809 Parenthesis (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 题目大意: 给一个长度为N(N<=105)的合法括号序列.Q(Q<= ...

  5. csu 1809 Parenthesis

    题目见此 分析,把'('当成1, ')'当成-1, 计算前缀和sum. 记交换括号左边的序号为u, 右边为v,讨论左右括号: 1.s[u] == '(' && s[v] == ')' ...

  6. hdu 3183 A Magic Lamp RMQ ST 坐标最小值

    hdu 3183 A Magic Lamp RMQ ST 坐标最小值 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 题目大意: 从给定的串中挑 ...

  7. NYOJ 119 士兵杀敌(三) RMQ ST

    NYOJ 119 士兵杀敌(三) RMQ ST 题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=119 思路: ST在线 预处理O(nlog ...

  8. lca 欧拉序+rmq(st) 欧拉序+rmq(线段树) 离线dfs 倍增

    https://www.luogu.org/problemnew/show/P3379 1.欧拉序+rmq(st) /* 在这里,对于一个数,选择最左边的 选择任意一个都可以,[left_index, ...

  9. poj3368(RMQ——ST)

    Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16543   Accepted: 5985 ...

随机推荐

  1. python_64_装饰器7

    # home密码认证是本地文件认证,bbs密码认证是远程ldat认证 import time user, passwd = 'qi', '123' def auth(auth_type): print ...

  2. 漫谈 Clustering (番外篇): Expectation Maximization

    Expectation Maximization (EM) 是一种以迭代的方式来解决一类特殊最大似然 (Maximum Likelihood) 问题的方法,这类问题通常是无法直接求得最优解,但是如果引 ...

  3. Charles拦截请求

    一.通过Charles抓包,可拦截请求并篡改交互信息 1.可篡改客户端向服务器发起的请求信息(服务器收到的是假消息) 2.可篡改服务器返回给客户端的响应结果(客户端看到的是假消息) 二.篡改用户请求 ...

  4. JavaScript 常用的排序算法

    冒泡排序 function bubbleSort(array) { for (let i = 0; i < array.length; i++) for (let j = 0; j < a ...

  5. pycharm 语言配置

    在pycharm 安装所在位置找到 lib 文件夹 打开后找到 rescources_**.jar 文件 **为语言类型,英语为en 中文为cn, 用相应语言文件替换,便可变成相应语言 https:/ ...

  6. php+croppic.js实现剪切上传图片

    最近需要实现裁剪图片上传,想起之前公司用到的一个插件,却不知道叫什么名字了. 在网上找了有些时间,最终找到了这个网站. http://www.croppic.net/ 因为官网文档全部都是英文,所以看 ...

  7. JZOJ 5812. 【NOIP提高A组模拟2018.8.14】 区间

    5812. [NOIP提高A组模拟2018.8.14] 区间 (File IO): input:range.in output:range.out Time Limits: 1000 ms  Memo ...

  8. Android BadgeView 工具包

    前言:消息未读,显示红点或者红色数字,其实就是一个TextView,有推送一般就有badgeView. Step 1 因为在github上看到了一些类似的第三方库,嫌麻烦,不如直接封装一个类,直接使用 ...

  9. Python中函数参数类型和参数绑定

    参数类型 Python函数的参数类型一共有五种,分别是: POSITIONAL_OR_KEYWORD(位置参数或关键字参数) VAR_POSITIONAL(可变参数) KEYWORD_ONLY(关键字 ...

  10. border-color与color

    1.border-color就是color,即border-color的默认颜色就是color 当没有指定border-color的时候,会使用color作为边框的颜色! 类似的还有text-shad ...