(中等) UESTC 94 Bracket Sequence,线段树+括号。
There is a sequence of brackets, which supports two kinds of operations.
- we can choose a interval [l,r], and set all the elements range in this interval to left bracket or right bracket.
- we can reverse a interval, which means that for all the elements range in [l,r], if it's left bracket at that time, we change it into right bracket, vice versa.
Fish is fond of Regular Bracket Sequence
, so he want to know whether a interval [l,r] of the sequence is regular or not after doing some operations.
Let us define a regular brackets sequence in the following way:
- Empty sequence is a regular sequence.
- If
S
is a regular sequence, then(S)
is also a regular sequences. - If
A
andB
are regular sequences, thenAB
is a regular sequence.
题目大意就是说给你一个括号序列,对他进行操作和询问,包括反转和覆盖两个操作。
维护一个总和,还有一个最小前缀和(还要维护最大前缀和,在反转的时候计算最小的。)。当总和和最小前缀和都为0,则成立。
这个题又被坑了好久,没办法,水平太差了,错了十几次,电子科大的提交记录都被我刷屏了。。。错误百出。。。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring> #define lson L,M,po*2
#define rson M+1,R,po*2+1
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b) using namespace std; int BIT[*];
int QS[*];
int MS[*];
int XOR[*];
int COL[*];
char ss[]; void pushUP(int po)
{
BIT[po]=BIT[po*]+BIT[po*+];
QS[po]=max(QS[po*],BIT[po*]+QS[po*+]); //这里要注意。
MS[po]=min(MS[po*],BIT[po*]+MS[po*+]);
} void pushDown(int po,int len)
{
if(COL[po])
{
COL[po*]=COL[po];
COL[po*+]=COL[po];
XOR[po*]=XOR[po*+]=; //这里不能忘记。
BIT[po*]=(len-(len/))*COL[po];
BIT[po*+]=(len/)*COL[po]; QS[po*]=max(-,BIT[po*]);
QS[po*+]=max(-,BIT[po*+]);
MS[po*]=min(,BIT[po*]);
MS[po*+]=min(,BIT[po*+]); COL[po]=;
} if(XOR[po])
{
int temp; XOR[po*]=!XOR[po*];
XOR[po*+]=!XOR[po*+]; BIT[po*]=-BIT[po*];
BIT[po*+]=-BIT[po*+]; temp=QS[po*];
QS[po*]=-MS[po*];
MS[po*]=-temp; temp=QS[po*+];
QS[po*+]=-MS[po*+];
MS[po*+]=-temp; XOR[po]=;
}
} void build_tree(int L,int R,int po)
{
XOR[po]=;
COL[po]=; if(L==R)
{
if(ss[L]=='(')
{
BIT[po]=;
QS[po]=;
MS[po]=;
}
else
{
BIT[po]=-;
QS[po]=-;
MS[po]=-;
} return;
} int M=(L+R)/; build_tree(lson);
build_tree(rson); pushUP(po);
} void update_col(int ul,int ur,int ut,int L,int R,int po)
{
if(ul<=L&&ur>=R)
{
XOR[po]=;
COL[po]=ut;
BIT[po]=ut*(R-L+); QS[po]=max(-,BIT[po]);
MS[po]=min(,BIT[po]); return;
} pushDown(po,R-L+); int M=(L+R)/; if(ul<=M)
update_col(ul,ur,ut,lson);
if(ur>M)
update_col(ul,ur,ut,rson); pushUP(po);
} void update_xor(int ul,int ur,int L,int R,int po)
{
if(ul<=L&&ur>=R)
{
XOR[po]=!XOR[po];
BIT[po]=-BIT[po]; int temp=QS[po];
QS[po]=-MS[po];
MS[po]=-temp; return;
} pushDown(po,R-L+); int M=(L+R)/; if(ul<=M)
update_xor(ul,ur,lson);
if(ur>M)
update_xor(ul,ur,rson); pushUP(po);
} int query(int &qs,int ql,int qr,int L,int R,int po) //不能忘记写 & !!!
{
if(ql<=L&&qr>=R)
{
qs=MS[po];
return BIT[po];
} pushDown(po,R-L+); int M=(L+R)/;
int ans=; if(qr<=M)
return query(qs,ql,qr,lson);
if(ql>M)
return query(qs,ql,qr,rson); int temp1,temp2,a1; a1=query(temp1,ql,qr,lson);
ans=a1+query(temp2,ql,qr,rson); qs=min(temp1,temp2+a1); return ans;
} bool getans(int ql,int qr,int N)
{
int t1;
int ans; if((qr-ql)%==)
return ; ans=query(t1,ql,qr,,N,); if(ans==&&t1==)
return ;
else
return ;
} int main()
{
int T;
int N,Q;
char t1[],t2[];
int a,b;
cin>>T; for(int cas=;cas<=T;++cas)
{
printf("Case %d:\n",cas); scanf("%d",&N);
scanf("%s",ss); build_tree(,N-,); //这里应该是N-1。 scanf("%d",&Q); for(int i=;i<Q;++i)
{
scanf("%s %d %d",t1,&a,&b); if(t1[]=='s')
{
scanf("%s",t2);
update_col(a,b,t2[]=='('?:-,,N-,);
}
else if(t1[]=='r')
update_xor(a,b,,N-,);
else
if(getans(a,b,N-))
printf("YES\n");
else
printf("NO\n");
} printf("\n");
} return ;
}
(中等) UESTC 94 Bracket Sequence,线段树+括号。的更多相关文章
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- UESTC 1546 Bracket Sequence
Bracket Sequence Time Limit: 3000MS Memory Limit: 65536KB 64 ...
- Wow! Such Sequence!(线段树4893)
Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸
D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- hdu4893Wow! Such Sequence! (线段树)
Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger f ...
- HDU 6047 Maximum Sequence(线段树)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...
- Codeforces 438D The Child and Sequence - 线段树
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...
- hdu 5828 Rikka with Sequence 线段树
Rikka with Sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...
- [bzoj1095][ZJOI2007]Hide 捉迷藏——线段树+括号序列
题目大意 给定一棵所有点初始值为黑的无权树,你需要支援两种操作: 把一个点的颜色反转 统计最远黑色点对. 题解 本题是一个树上的结构.对于树上的结构,我们可以采用点分治.树链剖分等方法处理,这个题用了 ...
随机推荐
- tab奇偶行颜色交替+插件
(function($){ $.fn.tableUI=function(options){ var defaults={ evenRowclass:"evenRow", oddro ...
- java String不可变对象,但StringBuffer是可变对象
什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变的对象呢? 可以这样认为:如果一个对象,在它创建完成之后,不能再改变它的状态,那么这个对象就是不可变的.不 ...
- android apk jarsigner 签名打包
cmd 命令符打包: 规则: jarsigner -verbose -keystore 签名路径 -signedjar 签名后的apk存放路径 未签名的apk 签名文件的别名 项目如我的项目是: ...
- Illegal resource reference: @*android resources are private and not always present
0:前言 在android开发中,当使用别人的代码的时候,在style.xml中有此种错误 1:解决方案 删除*星号
- android xml文件中出现如下提醒:This tag and its children can be replaced by one <TextView/> and a compound drawable
第一个感叹号 是跟你说 让你把Imageview 和textview 结合起来 只用 textview textview有个属性叫 android:drawable...(top/bottom/.. ...
- Ubuntu 14.04 待机死机问题原来是自己改了这个配置
使用专有驱动没问题.使用开源驱动就会死机.驱动还是厂商的好@@
- MyEclipse使用总结——修改MyEclipse默认的Servlet和jsp代码模板
http://www.cnblogs.com/xdp-gacl/p/3769058.html 孤傲苍狼 只为成功找方法,不为失败找借口! MyEclipse使用总结——修改MyEclipse默认的 ...
- ng-bind-html在ng-repeat中问题的解决办法
<div ng-controller="MyCtrl"> Hello, {{name}}! <div class="row" ng-repea ...
- Linux下find命令用法小结
find是个使用频率比较高的命令.常常用它在系统特定目录下,查找具有某种特征的文件. find命令的格式:find [-path……] -options [-print -exec -ok] path ...
- java几种常用设计模式简单示例
1.单例设计模式 所谓单例设计模式简单说就是无论程序如何运行,采用单例设计模式的类(Singleton类)永远只会有一个实例化对象产生.具体实现步骤如下: (1) 将采用单例设计模式的类的构造方法私有 ...