csu 1809 Parenthesis
分析,把‘(’当成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的更多相关文章
- CSU 1809 Parenthesis(线段树+前缀和)
Parenthesis Problem Description: Bobo has a balanced parenthesis sequence P=p1 p2-pn of length n and ...
- CSU 1809 Parenthesis(RMQ-ST+思考)
1809: Parenthesis Submit Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n ...
- CSU 1809 Parenthesis 思维+线段树
1809: Parenthesis Submit Page Summary Time Limit: 5 Sec Memory Limit: 128 Mb Submitte ...
- CSU 1809 - Parenthesis - [前缀和+维护区间最小值][线段树/RMQ]
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1809 Bobo has a balanced parenthesis sequenc ...
- 【贪心】CSU 1809 Parenthesis (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 题目大意: 给一个长度为N(N<=105)的合法括号序列.Q(Q<= ...
- 湖南省2016省赛题。1809: Parenthesis 线段树
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 给定一串平衡的序列,要求交换两个位置之后,问其是否还平衡. 首先要注意到交换的是两个位置,这 ...
- 湖南省第十二届大学生计算机程序设计竞赛 G Parenthesis
1809: Parenthesis Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q ...
- Parenthesis(前缀和+线段树)
1809: Parenthesis Time Limit: 5 Sec Memory Limit: 128 Mb Submitted: 2291 Solved: 622 Des ...
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...
随机推荐
- SQL练习题
create table Student( Sno varchar(20) primary key, Sname varchar(20) not null, Ssex varchar(20) not ...
- MyEclipse启动慢的办法
禁用myeclipse updating indexes MyEclipse 总是不停的在 Update index,研究发现Update index...是Maven在下载更新,但很是影响myecl ...
- mysql部署到云主机的笔记
写了个程序,需要把数据库部署到云主机上 MySQL基于安全考虑root账户一般只能本地访问,但是在开发过程中可能需要打开root的远程访问权限 为了安全,新添加一个用户来进行远程登录 登录MYSQL: ...
- SQL2005中的事务与锁定(五)- 转载
------------------------------------------------------------------------ -- Author : HappyFlyStone - ...
- WLST 命令和变量
下列部分将详细描述 WLST 命令和变量.主题包括: WSLT 命令类别概述 浏览命令 控制命令 部署命令 诊断命令 编辑命令 信息命令 生命周期命令 节点管理器命令 树命令 W ...
- Dynamics AX 2012 R2 安装额外的AOS
众所周知,AX系统分为三层:Client,Application Server,Database Server. 我们添加额外的Application Server主要是出于以下两个原因: 使用多台服 ...
- GAT2.0使用文档(组合接口测试)
3.2接口用例场景组件 在此之前,大家应该都已经开发完成了一个最简单的接口测试用例,但是之前的接口用例的期望结果是固定值,不能动态的去做对比,有很大局限性.下面开始介绍怎样通过场景组件来动态对测试结果 ...
- php缓冲区 sapi缓冲区
<?php#设置php.ini中output_buffering = 32#使用apache可以看到效果 #nginx+php-fpm看不到效果 nginx缓存 sockets通信问题?#imp ...
- JAVA基础知识之JDBC——ResultSet的滚动和更新(statement的额外参数)
ResultSet不仅可以内存中的一张二维表一样保存statement执行SQL的结果集,还能通过结果集修改DB的数据.ResultSetMetaData则可以用来获得ResultSet对象的相关信息 ...
- bat 命令分行写
myprog parameter parameter parameter parameter parameter parameter parameter parameter parameter par ...