湖南省第十二届大学生计算机程序设计竞赛 G Parenthesis
1809: Parenthesis
Description
Input
Output
Sample Input
4 2
(())
1 3
2 3
2 1
()
1 2
Sample Output
No
Yes
No
HINT
题意:
给你长度n的合法括号匹配和q个询问
每次询问你 交换ai,bi两个位置的符号,交换后是否还是合法的
题解:
分块
(转化为1,)转化为-1
合法括号序列满足前缀和永远大于等于0
利用这个每次修改两个位置
维护这个关系就好了
分块可过的
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std; #pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 2e5+, M = 1e6+, mod = 1e6+, inf = ; int block,n,q,m;
int sum[N],pos[N],mi[N],a[N],b[N],add[N],f[N];
char ch[N];
void init() {
int s = ;
pos[] = ;
memset(b,,sizeof(b));
memset(sum,,sizeof(sum));
memset(add,,sizeof(add));
for(int i = ; i <= n; ++i) b[i] = a[i];
for(int i = ; i <= n; ++i) sum[i] = sum[i-] + b[i];
for(int i = ; i < N; ++i) mi[i] = inf;
for(int i = ; i <= n; ++i) {
if(pos[i] != pos[i-]) s = ;
s += b[i];
mi[pos[i]] = min(mi[pos[i]],s);
}
for(int i = ; i <= n; ++i) add[pos[i]] += b[i];
f[] = ;
s = ;
for(int i = ; i <= n; ++i) {
s+=b[i];
if(s < ) f[i] = ;
else f[i] = f[i-];
}
}
int solve(int l,int r) {
int OK = ;
swap(b[l],b[r]);
if(pos[l] == pos[r]) {
int s = sum[block * (pos[l]-)];
for(int i = block * (pos[l]-) + ; i <= min(pos[l] * block,n); ++i) {
s += b[i];
if(s < ) OK = ;
}
for(int i = pos[l]+; i <= m; ++i) {
if(s + mi[i] < ) OK = ;
s += (add[i]);
}if(s < ) OK = ;
} else {
int s = sum[block * (pos[l]-)];
for(int i = block*(pos[l]-) + ; i <= min(pos[l] * block,n); ++i) {
s += b[i];
if(s < ) OK = ;
}
for(int i = pos[l]+; i <= pos[r]-; ++i) {
if(s + mi[i] < ) OK = ;
s += (add[i]);
}
for(int i = block * (pos[r]-) + ; i <= min(pos[r]*block,n); ++i) {
s += b[i];
if(s < ) OK = ;
}
for(int i = pos[r]+; i <= m; ++i) {
if(s + mi[i] < ) OK = ;
s += (add[i]);
}
}
swap(b[l],b[r]);
if(OK == ) return ;
else return ;
}
int main() {
while(scanf("%d%d",&n,&q)!=EOF) {
scanf("%s",ch);
block = int(sqrt(n));
memset(a,,sizeof(a));
memset(pos,,sizeof(pos));
for(int i = ; i <= n; i++) {if(ch[i-] == '(') a[i] = ; else a[i] = -; pos[i]=(i-)/block+;}
if(n%block) m = n/block + ; else m = n/block;
init();
for(int i = ; i <= q; ++i) {
int l,r;
scanf("%d%d",&l,&r);
if(l > r) swap(l,r);
if(solve(l,r) == ) printf("Yes\n");
else printf("No\n");
}
}
return ;
}
湖南省第十二届大学生计算机程序设计竞赛 G Parenthesis的更多相关文章
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...
- 2016年湖南省第十二届大学生计算机程序设计竞赛Problem A 2016 找规律归类
Problem A: 2016 Time Limit: 5 Sec Memory Limit: 128 MB Description 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) ...
- 湖南省第十二届大学生计算机程序设计竞赛 F 地铁 多源多汇最短路
1808: 地铁 Description Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站,用 1,2,…,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i ...
- 湖南省第十二届大学生计算机程序设计竞赛 B 有向无环图 拓扑DP
1804: 有向无环图 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 187 Solved: 80[Submit][Status][Web Board ...
- 湖南省第十二届大学生计算机程序设计竞赛 A 2016
1803: 2016 Description 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. In ...
- 【模拟】CSU 1807 最长上升子序列~ (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1807 题目大意: 给你一个长度为N(N<=105)的数列,数列中的0可以被其他数 ...
- 【最短路】【数学】CSU 1806 Toll (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1806 题目大意: N个点M条有向边,给一个时间T(2≤n≤10,1≤m≤n(n-1), ...
- 【树状数组】CSU 1811 Tree Intersection (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1811 题目大意: 一棵树,N(2<=N<=105)个节点,每个节点有一种颜 ...
- 【数学】CSU 1810 Reverse (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1810 题目大意: 一个长度为N的十进制数,R(i,j)表示将第i位到第j位翻转过来后的 ...
随机推荐
- 【leetcode】Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
- Django用户管理及认证
同步组http://www.douban.com/group/topic/29387804/ ldapsearch -x -w password -D "cn=me,cn=Users,dc= ...
- 一步步搭建docker私有仓库并从私有仓库中下载镜像
一步步搭建docker私有仓库 #下载镜像 docker pull registry#查看镜像 docker images #运行私有仓库,指定端口和数据卷 docker run -d -p : -v ...
- java web 学习 --第十一天(Java三级考试)
第十天的学习内容:http://www.cnblogs.com/tobecrazy/p/3473954.html Servlet理论知识: 1.servlet 生成class位置 tomcat编译后生 ...
- monitor disk
#!/bin/bash # #top #Big_USERS - find big disk space users in various directories ################### ...
- codeforces 493B.Vasya and Wrestling 解题报告
题目链接:http://codeforces.com/problemset/problem/493/B 题目意思:给出 n 个 techniques,每个 technique 的值为 ai. ai & ...
- CodeSign error: code signing is required for product type Application in SDK iOS
在真机测试的时候往往会突然出现这样一个错误,code signing is required for product type 'Application' in SDK 'iOS 7.0' ,就是说 ...
- MyBatis之CRUD
1 mybatis框架介绍 1.1回顾jdbc操作数据库的过程 1.2 mybatis开发步骤 A.提供一个SqlMapperConfig.xml(src目录下),该文件主要配置数据库连接,事务,二级 ...
- Struts2自定义拦截器
1. 需求 自定义拦截器实现,用户登录的访问控制. 2. 定义拦截器类 public class LoginInterceptor extends AbstractInterceptor { @Ove ...
- net 页面跳转
前台: < a href="xx.html" target="_blank"> 后台: Response.Redirect("XXX.as ...