There is a sequence of brackets, which supports two kinds of operations.

  1. we can choose a interval [l,r], and set all the elements range in this interval to left bracket or right bracket.
  2. 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:

  1. Empty sequence is a regular sequence.
  2. If S is a regular sequence, then (S) is also a regular sequences.
  3. If A and B are regular sequences, then AB is a regular sequence.

  

  题目大意就是说给你一个括号序列,对他进行操作和询问,包括反转和覆盖两个操作。

  维护一个总和,还有一个最小前缀和(还要维护最大前缀和,在反转的时候计算最小的。)。当总和和最小前缀和都为0,则成立。

  这个题又被坑了好久,没办法,水平太差了,错了十几次,电子科大的提交记录都被我刷屏了。。。错误百出。。。

代码如下:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4.  
  5. #define lson L,M,po*2
  6. #define rson M+1,R,po*2+1
  7. #define max(a,b) (a>b?a:b)
  8. #define min(a,b) (a<b?a:b)
  9.  
  10. using namespace std;
  11.  
  12. int BIT[*];
  13. int QS[*];
  14. int MS[*];
  15. int XOR[*];
  16. int COL[*];
  17. char ss[];
  18.  
  19. void pushUP(int po)
  20. {
  21. BIT[po]=BIT[po*]+BIT[po*+];
  22. QS[po]=max(QS[po*],BIT[po*]+QS[po*+]); //这里要注意。
  23. MS[po]=min(MS[po*],BIT[po*]+MS[po*+]);
  24. }
  25.  
  26. void pushDown(int po,int len)
  27. {
  28. if(COL[po])
  29. {
  30. COL[po*]=COL[po];
  31. COL[po*+]=COL[po];
  32. XOR[po*]=XOR[po*+]=; //这里不能忘记。
  33. BIT[po*]=(len-(len/))*COL[po];
  34. BIT[po*+]=(len/)*COL[po];
  35.  
  36. QS[po*]=max(-,BIT[po*]);
  37. QS[po*+]=max(-,BIT[po*+]);
  38. MS[po*]=min(,BIT[po*]);
  39. MS[po*+]=min(,BIT[po*+]);
  40.  
  41. COL[po]=;
  42. }
  43.  
  44. if(XOR[po])
  45. {
  46. int temp;
  47.  
  48. XOR[po*]=!XOR[po*];
  49. XOR[po*+]=!XOR[po*+];
  50.  
  51. BIT[po*]=-BIT[po*];
  52. BIT[po*+]=-BIT[po*+];
  53.  
  54. temp=QS[po*];
  55. QS[po*]=-MS[po*];
  56. MS[po*]=-temp;
  57.  
  58. temp=QS[po*+];
  59. QS[po*+]=-MS[po*+];
  60. MS[po*+]=-temp;
  61.  
  62. XOR[po]=;
  63. }
  64. }
  65.  
  66. void build_tree(int L,int R,int po)
  67. {
  68. XOR[po]=;
  69. COL[po]=;
  70.  
  71. if(L==R)
  72. {
  73. if(ss[L]=='(')
  74. {
  75. BIT[po]=;
  76. QS[po]=;
  77. MS[po]=;
  78. }
  79. else
  80. {
  81. BIT[po]=-;
  82. QS[po]=-;
  83. MS[po]=-;
  84. }
  85.  
  86. return;
  87. }
  88.  
  89. int M=(L+R)/;
  90.  
  91. build_tree(lson);
  92. build_tree(rson);
  93.  
  94. pushUP(po);
  95. }
  96.  
  97. void update_col(int ul,int ur,int ut,int L,int R,int po)
  98. {
  99. if(ul<=L&&ur>=R)
  100. {
  101. XOR[po]=;
  102. COL[po]=ut;
  103. BIT[po]=ut*(R-L+);
  104.  
  105. QS[po]=max(-,BIT[po]);
  106. MS[po]=min(,BIT[po]);
  107.  
  108. return;
  109. }
  110.  
  111. pushDown(po,R-L+);
  112.  
  113. int M=(L+R)/;
  114.  
  115. if(ul<=M)
  116. update_col(ul,ur,ut,lson);
  117. if(ur>M)
  118. update_col(ul,ur,ut,rson);
  119.  
  120. pushUP(po);
  121. }
  122.  
  123. void update_xor(int ul,int ur,int L,int R,int po)
  124. {
  125. if(ul<=L&&ur>=R)
  126. {
  127. XOR[po]=!XOR[po];
  128. BIT[po]=-BIT[po];
  129.  
  130. int temp=QS[po];
  131. QS[po]=-MS[po];
  132. MS[po]=-temp;
  133.  
  134. return;
  135. }
  136.  
  137. pushDown(po,R-L+);
  138.  
  139. int M=(L+R)/;
  140.  
  141. if(ul<=M)
  142. update_xor(ul,ur,lson);
  143. if(ur>M)
  144. update_xor(ul,ur,rson);
  145.  
  146. pushUP(po);
  147. }
  148.  
  149. int query(int &qs,int ql,int qr,int L,int R,int po) //不能忘记写 & !!!
  150. {
  151. if(ql<=L&&qr>=R)
  152. {
  153. qs=MS[po];
  154. return BIT[po];
  155. }
  156.  
  157. pushDown(po,R-L+);
  158.  
  159. int M=(L+R)/;
  160. int ans=;
  161.  
  162. if(qr<=M)
  163. return query(qs,ql,qr,lson);
  164. if(ql>M)
  165. return query(qs,ql,qr,rson);
  166.  
  167. int temp1,temp2,a1;
  168.  
  169. a1=query(temp1,ql,qr,lson);
  170. ans=a1+query(temp2,ql,qr,rson);
  171.  
  172. qs=min(temp1,temp2+a1);
  173.  
  174. return ans;
  175. }
  176.  
  177. bool getans(int ql,int qr,int N)
  178. {
  179. int t1;
  180. int ans;
  181.  
  182. if((qr-ql)%==)
  183. return ;
  184.  
  185. ans=query(t1,ql,qr,,N,);
  186.  
  187. if(ans==&&t1==)
  188. return ;
  189. else
  190. return ;
  191. }
  192.  
  193. int main()
  194. {
  195. int T;
  196. int N,Q;
  197. char t1[],t2[];
  198. int a,b;
  199. cin>>T;
  200.  
  201. for(int cas=;cas<=T;++cas)
  202. {
  203. printf("Case %d:\n",cas);
  204.  
  205. scanf("%d",&N);
  206. scanf("%s",ss);
  207.  
  208. build_tree(,N-,); //这里应该是N-1。
  209.  
  210. scanf("%d",&Q);
  211.  
  212. for(int i=;i<Q;++i)
  213. {
  214. scanf("%s %d %d",t1,&a,&b);
  215.  
  216. if(t1[]=='s')
  217. {
  218. scanf("%s",t2);
  219. update_col(a,b,t2[]=='('?:-,,N-,);
  220. }
  221. else if(t1[]=='r')
  222. update_xor(a,b,,N-,);
  223. else
  224. if(getans(a,b,N-))
  225. printf("YES\n");
  226. else
  227. printf("NO\n");
  228. }
  229.  
  230. printf("\n");
  231. }
  232.  
  233. return ;
  234. }

(中等) UESTC 94 Bracket Sequence,线段树+括号。的更多相关文章

  1. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  2. UESTC 1546 Bracket Sequence

                                        Bracket Sequence Time Limit: 3000MS   Memory Limit: 65536KB   64 ...

  3. Wow! Such Sequence!(线段树4893)

    Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...

  4. 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 ...

  5. hdu4893Wow! Such Sequence! (线段树)

    Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger f ...

  6. HDU 6047 Maximum Sequence(线段树)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...

  7. 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 ...

  8. hdu 5828 Rikka with Sequence 线段树

    Rikka with Sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...

  9. [bzoj1095][ZJOI2007]Hide 捉迷藏——线段树+括号序列

    题目大意 给定一棵所有点初始值为黑的无权树,你需要支援两种操作: 把一个点的颜色反转 统计最远黑色点对. 题解 本题是一个树上的结构.对于树上的结构,我们可以采用点分治.树链剖分等方法处理,这个题用了 ...

随机推荐

  1. 两个数组各个数相加或相乘变成一个矩阵求第K大

    input 1<=T<=20 1<=n<=100000,1<=k<=n*n a1 a2 ... an 0<ai<=10000 b1 b2 ... bn ...

  2. php 版本比较

    判断当前运行的 PHP 版本是否高于或等于你提供的版本号. function is_php($version) { static $_is_php; $version = (string) $vers ...

  3. Mac系统Git生成ssh公钥

    Mac系统Git生成ssh公钥 在使用Git仓库进行代码管理时,新的电脑上往往需要生成ssh公钥进行匹配,Mac系统生成Git公钥过程如下: 1.检查本机是否已有公钥 在终端中输入如下命令: ? 1 ...

  4. OpenCV4Android释疑: 透析Android以JNI调OpenCV的三种方式(让OpenCVManager永不困扰)

    OpenCV4Android释疑: 透析Android以JNI调OpenCV的三种方式(让OpenCVManager永不困扰) 前文曾详细探讨了关于OpenCV的使用,原本以为天下已太平.但不断有人反 ...

  5. android Actionmode 样式自定义

    <style name="Base.Theme.DesignDemo" parent="Theme.AppCompat.Light.NoActionBar" ...

  6. HTML一些小细节

    这里主要是记录一些看起来不重要,但是其实作用不小或者使用起来某种情况下好用的东西,又或者是重要的但容易忽略的基础知识 1. HTML5之后的第一个标签是<!DOCTYPE html> 这个 ...

  7. Django - 用户注册

    使用Django工程自动创建的auth_user表来存储用户信息 在app目录下创建forms.py mysite/music/forms.py from django.contrib.auth.mo ...

  8. PAT (Advanced Level) 1101. Quick Sort (25)

    树状数组+离散化 #include<cstdio> #include<cstring> #include<cmath> #include<map> #i ...

  9. 支付宝集成+网站支付+APP支付+手机网站支付

    网站支付宝 1.申请签约后获得相应的pid:208***开头和key 这里说明下pc网站支付采用md5加密所以这里只需要提供pid和key不需要上传公钥. 2.下载即时到账demo http://do ...

  10. discuz 添加板块失败解决办法

    最近把服务器环境升了下级,发现discuz后台添加栏目添加不了了,数据库没变,源代码没变,就突然添加不了了.刚开始添加1个板块成功了,再添加就怎么也添不进去了.只是页面刷新了一下,啥提示没有. 经过一 ...