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. mac 远程桌面提示: 证书或相关链无效

    左上角 RDC -->首选项-->安全性-->即使验证失败,也始终链接

  2. python_16_自己建立模块

    import python_5_password

  3. systemd 中的requires, wants, before, after

    man systemd.unit    man systemd.service ###依赖关系和前后顺序* 依赖关系:Requires和Wants * 前后顺序:After,Before 依赖关系,前 ...

  4. axios获取后端数据

    axios向后端请求数据时,一直获取不到数据, 后来改成这样写获取到了数据 不是一个this,有人说用箭头函数就可以了.

  5. java基础面试题:Math.round(11.5)等於多少? Math.round(-11.5)等於多少?

    package com.swift; public class Math_Round { public static void main(String[] args) { /* * Math roun ...

  6. Zabbix监控oracle各服务器连接数

    需求: 根据机器名查询oracle连接数,并通过zabbix进行监控 脚本: [root@rac1 Zabbix_S]# cat get_conns.py#!/usr/bin/python#codin ...

  7. asp.net 中 UpdataPanel 的使用注意点

    1. 在UpdataPanel 前必须加上asp:ScriptManager的控件,保证页面能够正常显示

  8. LeetCode955删列造序 ||

    问题:删列造序 || 给定由 N 个小写字母字符串组成的数组 A,其中每个字符串长度相等. 选取一个删除索引序列,对于 A 中的每个字符串,删除对应每个索引处的字符. 比如,有 A = [" ...

  9. Cube HDU - 1220(思维)

    Cowl is good at solving math problems. One day a friend asked him such a question: You are given a c ...

  10. 文件的特殊权限(SUID,SGID,SBIT)

    文件的一般权限:r w x  对应 421  文件的特殊权限:SUID SGID SBIT对应 421  文件的隐藏权限:chattr设置隐藏权限,lsattr查看文件的隐藏权限. 文件访问控制列表: ...