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 ...
随机推荐
- Java Servlet(五):GenericServlet与Servlet、HttpServlet之间的关系(jdk7+tomcat7+eclipse)
本篇主要记录下,对GenericServlet的作用理解,及其与Servlet/HttpServlet之间的关系. 示例完成业务: 1.新建一个login.jsp页面,要求改页面能输入username ...
- smartdraw2013破解方法
smartdraw是一个非常好的画图工作,最大的优点就是支持多种图形,采用模板的方式在线扩充,可以快速画出你想要的图形,具体的介绍见其他资料. 这里是我自己的破解办法. 网上的下载都包含破解工具,但是 ...
- PAT——乙级真题1002代码
- AJAX-----10iframe模拟ajax文件上传效果原理2
在实际开发中其实我们可以给用户一些提示,比如上传成功或者上传失败,废话不多说,走码: <!DOCTYPE html> <html lang="en"> &l ...
- JS中反斜杠和单双引号的配合使用效果
<div id="tag"></div> <div id="tag1"></div> <div id=&q ...
- 机器学习实战-边学边读python代码(4)
程序2-4 分类器针对约会网站的测试代码(4) def datingClassTest():hoRatio = 0.10 //将文件读入内存矩阵datingDataMat,datingLabels = ...
- iOS使用textfield注意的细节
一般做登录界面或者要填写表之类的页面会经常使用到textfield.使用很简单,但是其实他有很多小的处理细节,这回让你显得有经验,交互性很好.在这里呢,我就直接拿stroyboard中的截图来说. c ...
- eclipse快捷方式
虽说右键可以直接发送快捷方式到桌面,但是点击桌面图标确提示错误,偶然发现右键选个什么,配置下启动文件就ok了(就是链接到安装目录里面那个可以启动的exe),后来怎么复现不了,伤感了,不过是可以用了,还 ...
- 使用percona-toolkit校验主从数据的一致性
主从数据校验使用percona-toolkit工具集的以下两个工具(主库上使用): pt-table-checksum #检查主从数据是否一致, pt-table-sync #把主库数据同步到从库 ...
- EasyUI中在表单提交之前进行验证
使用EasyUi我们可以在客户端表单提交之前进行验证,过程如下:只需在onSubmit的时候使用return $("#form1").form('validate')方法即可,E ...