Flipping Parentheses(CSU1542 线段树)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1542
赛后发现这套题是2014东京区域赛的题目,看了排名才发现自己有多low = =!
题目大意是这样的,给一个已经匹配了的括号序列,现每次操作就是在位置p将这个位置的括号反转,问要达到将这个序列重新变成匹配序列,最左侧能够操作第几个括号达到目的。
这里,我的思路是用一个数组a,a[i]表示的是前i个括号,左括号'('与右括号')'的数量之差,题目数据就可以表示为:
s: ( ( ( ) ) )
a: 1 2 3 2 1 0
这样,一个匹配的括号序列对应的数组一定满足两个条件:1、数组没有一个元素为负 2、数组是以0结尾
现操作时,若是将'(' 转化为')' ,比如将位置4的括号反转,对应数组:
s: ( ( ( ( ) )
a: 1 2 3 4 3 2
可以看做是将数组位置4以后的每个元素都+2(反之,若是')'转化为'(',那么就是那个位置以后每个元素都-2)
可以看到:
对应第一种操作,将'(' 转化为')', 要达到匹配序列,只需要将括号序列里的第一个右括号')'翻转即可(因为此时为+2操作,又只可以操作右括号,所以操作第一个必然是最优的)
对应第二种操作,将')'转化为'(',则是找到从末尾往前找到最早的一个a[p]>=2的位置p,且a[p ~ n] >= 2全部成立,(因为是-2操作,所以必须保证在-2前这个数是>=2的)
计算时,可以使用线段树(因为有区间操作),节点保存的内容包括左右括号数量之差a,延时标记s,以及f = a[i] - i的值(若a[i] - i < 0,说明在区间[1, i]一定存在一个左括号,供查找第一个左括号时使用),而若要找到从后往前最长连续的a[p ~ n] >= 2的位置p,只需要判断区间的最小值是否>=2,查找方式与上一个类似。
查询时区间保存的是这个区间的 a 值和 f 值的最小值。区间修改+极值查询。
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)
#define rep(i, a, b) for(int i = a; i <= b; i ++) template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } //typedef __int64 LL;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-; int n, q, p, len;
char s[]={""};
struct Node {
int f, a, s;
}t[<<]; char rev(char c) { return (c == '(') ? ')' : '('; } void buildTree(int k, int L, int R, int p, int a)
{
t[k].s = ;
if(L == R) {
t[k].f = a - L; t[k].a = a;
return ;
}
int mid = (L + R) >> ;
if(p > mid) buildTree(rson, p, a);
else buildTree(lson, p, a);
t[k].f = min(t[k<<].f, t[k<<|].f);
t[k].a = min(t[k<<].a, t[k<<|].a);
} void init()
{
mem0(t);
int sum = ;
len = strlen(s) - ;
rep (i, , len) {
sum += (s[i] == '(') ? : -;
buildTree(, , len, i, sum);//建树
}
} //向下传延时标记
void pushDown(int k)
{
t[k<<].s += t[k].s; t[k<<].a += t[k].s; t[k<<].f += t[k].s;
t[k<<|].s += t[k].s; t[k<<|].a += t[k].s; t[k<<|].f += t[k].s;
t[k].s = ;
} //更新操作,将区间[p, len]的所有值都+val
void update(int k, int L, int R, int p, int val)
{
if(p <= L) {
t[k].s += val;
t[k].a += val;
t[k].f += val;
return ;
}
pushDown(k);
int mid = (L + R) >> ;
if(p <= mid) update(lson, p, val);//左侧可能需要更新
update(rson, p, val);//右侧是一定要更新的,因为需要更新的区间为[p, len]
t[k].f = min(t[k<<].f, t[k<<|].f);
t[k].a = min(t[k<<].a, t[k<<|].a);
} //查询最左侧的右括号
int query1(int k, int L, int R)
{
if(L == R) return L;
int mid = (L + R) >> ;
pushDown(k);
if(t[k<<].f < ) return query1(lson);
return query1(rson);
} //查询从len往前连续的最长的满足a>=2的点,也就是最后一个<2的位置+1
int query2(int k, int L, int R)
{
if(L == R) return min(len, L + );
int mid = (L + R) >> ;
pushDown(k);
if(t[k<<|].a < ) return query2(rson);
return query2(lson);
} int main()
{
//FIN;
while(~scanf("%d %d%*c %s", &n, &q, s + )) {
init();
rep (i, , q - ) {
scanf("%d", &p);
s[p] = rev(s[p]); update(, , len, p, s[p] == ')' ? - : );
if(s[p] == ')') p = query1(, , len);//find first )
else p = query2(, , len); //find last < 2
s[p] = rev(s[p]); update(, , len, p, s[p] == ')' ? - : );
printf("%d\n", p);
}
}
return ;
}
Flipping Parentheses(CSU1542 线段树)的更多相关文章
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- CSU - 1542 Flipping Parentheses (线段树)
CSU - 1542 Flipping Parentheses Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld ...
- Flipping Parentheses~Gym 100803G
Description A string consisting only of parentheses '(' and ')' is called balanced if it is one of t ...
- CSUOJ 1542 Flipping Parentheses
ACM International Collegiate Programming Contest Asia Regional Contest, Tokyo, 2014–10–19 Problem G ...
- spoj IITWPC4F - Gopu and the Grid Problem 线段树
IITWPC4F - Gopu and the Grid Problem no tags Gopu is interested in the integer co-ordinates of the ...
- Subsequence Count 2017ccpc网络赛 1006 dp+线段树维护矩阵
Problem Description Given a binary string S[1,...,N] (i.e. a sequence of 0's and 1's), and Q queries ...
- hdu 5831 Rikka with Parenthesis II 线段树
Rikka with Parenthesis II 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...
- hdu 5338 ZZX and Permutations (贪心+线段树+二分)
ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/O ...
- HDU 6155 Subsequence Count 线段树维护矩阵
Subsequence Count Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Oth ...
随机推荐
- 51nod1202 子序列个数
看到a[i]<=100000觉得应该从这个方面搞.如果a[x]没出现过,f[x]=f[x-1]*2;否则f[x]=f[x-1]*2-f[pos[a[x]]-1];ans=f[n]-1,然后WA了 ...
- Phpstorm Xdebug Web程序调试
平时调试php程序的时候,可以通过在代码中添加var_dump等函数来实现简单的断点调试. 下面介绍另一种方法,通过Phpstorm和Xdebug来进行调试. 1.下载Xdebug 这个是官网下载地址 ...
- (转载)UITableView的详细讲解
NSIndexPath类型是用来获取用户选择的indexPath,在别的函数里面,若需要知道用户选择了哪个cell,用上它可以省事很多.不必再去建全局变量section和row. NSIndexPat ...
- 编译安装apache+php(加常见问题解决)
[编译apache]./configure --prefix=/usr/local/lamp/httpd -with-apr=/usr/local/apr -with-apr-util=/usr/lo ...
- OCJP考试介绍
OCJP考试介绍 考试名称:SCJP / OCJP / 1Z0-851 考试时间:150分钟 考题题目:60道题 通过条件:大于等于61%的题目正确 考点查询:http://www.pearsonvu ...
- erl_0012 timer:tc 测试模块函数调用运行耗时
timer:tc 可以测试出函数调用耗时,是erlang性能测试的好工具. timer:tc(?MODULE, Fun, [Args]).
- python 包管理
如果导入的模块和主程序在同个目录下,直接import就行了 2.如果导入的模块是在主程序所在目录的子目录下,可以在子目录中增加一个空白的__init__.py文件,该文件使得python解释器将子目录 ...
- Heritrix源码分析(八) Heritrix8个处理器(Processor)介绍(转)
本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/643367 本博客已迁移到本人独立博客: http://www.yun5u ...
- db2数据库创建一张表,并为该表加上主键递增的性能和中间表的创建的sql语句
创建角色表 CREATE TABLE NBCTXP.TBL_NBC_NONBANKROLE ( ID BIGINT NOT NULL, ROLENAME VARCHAR(50), C ...
- dos 实用命令收集
隐藏文件夹: H:\>attrib gho +h +s +r 解决win2012服务器上网慢:netsh int tcp set global ecn=disable