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 ...
随机推荐
- 关键字static/const的作用
static关键字的作用:(1)设置变量的存储域,函数体内static变量的作用范围为该函数体,不同于auto变量,该变量的内存只被分配一次,因此其值在下次调用时仍维持上次的值:(2)限制变量的作用域 ...
- RDIFramework.NET ━ 9.12 表字段管理 ━ Web部分
RDIFramework.NET ━ .NET快速信息化系统开发框架 9.12 表字段管理 -Web部分 表(字段)综合管理模块主要功能是对数据表本身,表的各个字段做相应的控制,具体有以下功能: ...
- SpringMVC源码剖析(一)- 从抽象和接口说起
SpringMVC作为Struts2之后异军突起的一个表现层框架,正越来越流行,相信javaee的开发者们就算没使用过SpringMVC,也应该对其略有耳闻.我试图通过对SpringMVC的设计思想和 ...
- IDEA14下多jdk编译时,enum不支持;多个project共用在一个workplace下每个module时引用外部包
idea多个工程如何在一个项目中管理: 把多个多个项目放在一个叫work目录下,那么打开IntelliJ IDEA编译器,点击菜单 File->Open...,选择刚刚的work目录,即可. 在 ...
- 《30天自制操作系统》13_day_学习笔记
harib10a: 简化字符串的显示:我们发现字符串显示三条语句总是重复出现,并且总是一起出现的.接下来我们把它归纳到一个函数中,这样便于使用. x,y--位置的坐标 c--字符颜色 (col ...
- 如何设置一个严格30分钟过期的Session(转载)
本文地址: http://www.laruence.com/2012/01/10/2469.html 今天在我的微博(Laruence)上发出一个问题: 我在面试的时候, 经常会问一个问题: &quo ...
- Objective-C语言Foundation框架
Mac OS X开发会使用Cocoa框架,它是一种支持应用程序提供丰富用户体验的框架,它实际上由:Foundation和Application Kit(AppKit)框架组成.iOS开发,会使用Coc ...
- HTML5、CSS3响应式设计——笔记
1.1.响应式网页设计 响应式网页设计(RWD,Responsive Web Design)这个术语,由伊桑·马科特(EthanMarcotte)提出.他在A List Apart 发表了一篇开创性的 ...
- LeetCode----202. Happy Number(Java)
package isHappy202; /* * Write an algorithm to determine if a number is "happy". A happy n ...
- linux atom 不支持中文
linux atom 不支持中文 1.首先在ubuntu下安装泉驿正黑字体 sudo apt-get install ttf-wqy-* 2.Edit > Preferences > Se ...