题目见此

分析,把‘(’当成1, ‘)’当成-1, 计算前缀和sum。

记交换括号左边的序号为u, 右边为v,讨论左右括号:

1、s[u] == ‘(’ && s[v] == ‘)’ 那么[u, v - 1]的前缀和会全部-2

2、s[u] == ‘(’ && s[v] == ‘(’ 显然

3、s[u] == ‘)’ && s[v] == ‘(’ 那么[u, v - 1]的前缀和会全部+2

4、s[u] == ‘)’ && s[v] == ‘)’ 显然

对于一个括号序列,序列合法的要求是对∃sum[i]>=0 && sum[N]=0, 所以2,3,4的变换都不会改变序列的合法性。对于第一种情况,只要min(sum[i])>=2,i∈[u,v−1]即可。可以用ST或者线段树实现快速查询最小值。

/*****************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define offcin ios::sync_with_stdio(false)
#define sigma_size 26
#define lson l,m,v<<1
#define rson m+1,r,v<<1|1
#define slch v<<1
#define srch v<<1|1
#define sgetmid int m = (l+r)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define pb push_back
#define fi first
#define se second const int INF = 0x3f3f3f3f;
const LL INFF = 1e18;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-9;
const LL mod = 1e9+7;
const int maxmat = 10;
const ull BASE = 31; /*****************************************************/ const int maxn = 2e5 + 5;
int N, Q;
char s[maxn];
int a[maxn];
int seg[maxn << 2], col[maxn << 2];
void PushUp(int v) {
seg[v] = min(seg[slch], seg[srch]);
}
void build(int l, int r, int v) {
if (l == r) seg[v] = a[l];
else {
sgetmid;
build(lson);
build(rson);
PushUp(v);
}
}
int query(int L, int R, int l, int r, int v) {
if (L <= l && r <= R) return seg[v];
sgetmid;
int ans = INF;
if (L <= m) ans = min(ans, query(L, R, lson));
if (R > m) ans = min(ans, query(L, R, rson));
return ans;
}
int main(int argc, char const *argv[]) {
int N, Q;
while (~scanf("%d%d", &N, &Q)) {
mem(seg, 0); mem(a, 0);
scanf("%s", s);
for (int i = 1; i <= N; i ++) {
if (s[i - 1] == '(') a[i] = a[i - 1] + 1;
else a[i] = a[i - 1] - 1;
}
build(1, N, 1);
for (int i = 0; i < Q; i ++) {
int u, v;
scanf("%d%d", &u, &v);
if (u > v) swap(u, v);
if (s[u - 1] == '(' && s[v - 1] == ')') {
int pos = query(u, v - 1, 1, N, 1);
if (pos >= 2) puts("Yes");
else puts("No");
}
else puts("Yes");
}
}
return 0;
}

csu 1809 Parenthesis的更多相关文章

  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(RMQ-ST+思考)

    1809: Parenthesis Submit Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n ...

  3. CSU 1809 Parenthesis 思维+线段树

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

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

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

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

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

  6. 湖南省2016省赛题。1809: Parenthesis 线段树

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 给定一串平衡的序列,要求交换两个位置之后,问其是否还平衡. 首先要注意到交换的是两个位置,这 ...

  7. 湖南省第十二届大学生计算机程序设计竞赛 G Parenthesis

    1809: Parenthesis Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q ...

  8. Parenthesis(前缀和+线段树)

    1809: Parenthesis Time Limit: 5 Sec     Memory Limit: 128 Mb     Submitted: 2291     Solved: 622 Des ...

  9. 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)

    原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...

随机推荐

  1. Saving changes is not permitted in SQL Server

    From Save (Not Permitted) Dialog Box on MSDN : The Save (Not Permitted) dialog box warns you that sa ...

  2. RDIFramework.NET ━ .NET快速信息化系统开发框架 V2.8 版本发布

    (新年巨献) RDIFramework.NET ━ .NET快速信息化系统开发框架 V2.8 版本发布 历时数月,RDIFramework.NET V2.8版本发布了,感谢大家的支持. RDIFram ...

  3. html5,表单与label标签配套使用

    <form action=""><input type="checkbox" name="dx" value=" ...

  4. ios项目总结一:开发中常用的设计模式

    一.单例设计模式 1.应用场景: 程序运行期间,在内存中只有一个实例存在,主要用于资源共享,对硬件的访问等等 2.优点: 跨模块,解耦合,使用简单 3.敏捷原则: 单一职责原则 4.SDK实例: UI ...

  5. Linux多节点互信配置

    SSH互信设置步骤:   1. 每个节点上分别生成自己的公钥和私钥   2. 将各节点的公钥文件汇总到一个总的认证文件authorized_keys中   3. 将这个包含了所有节点公钥的认证文件au ...

  6. [已解决] windows 下 git 免输密码

    在 .git/config 里面加上这句话即 [credential] helper = store 原文地址:http://www.cnblogs.com/gifisan/p/5829578.htm ...

  7. mysql已有数据字符集转换

    下面模拟把latin1字符集的数据转换为utf8字符集 一.创建测试表和测试数据: 1.修改会话级别的连接字符集 mysql > set names latin1; 查看一下: 2.创建测试表: ...

  8. [课程设计]Scrum 1.5 多鱼点餐系统开发进度(点餐页面框架修复及继续布置)

    Scrum 1.5 多鱼点餐系统开发进度(点餐页面框架修复及继续布置)  1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅 ...

  9. centos7忘记root密码修改方式

    1.在进入系统选择时按下e键

  10. Shell.xaml

    <Window x:Class="HelloWorld.Shell" xmlns="http://schemas.microsoft.com/winfx/2006/ ...