【BZOJ2329/2209】[HNOI2011]括号修复/[Jsoi2011]括号序列

题解:我们的Splay每个节点维护如下东西:左边有多少多余的右括号,右边有多少多余的左括号,同时为了反转操作,还要维护左边有多少多余的左括号,右边有多少多余的右括号(如果一个右括号匹配一个左括号的话)。然后xjb维护一番即可。。。

答案是什么呢?$\lceil{左边多余的右括号数\over 2}\rceil+\lceil{右边多余的左括号数\over 2}\rceil$。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. using namespace std;
  5. const int maxn=100010;
  6. int n,m,rt;
  7. struct node
  8. {
  9. int v,f[2][2],ch[2],fa,siz,tag;
  10. bool r1,r2;
  11. }s[maxn];
  12. char str[maxn];
  13. //'('->0 ')'->1 r1->反 r2->翻
  14. inline int ab(const int &a) {return a>0?a:0;}
  15. inline void pushup(int x)
  16. {
  17. int l=s[x].ch[0],r=s[x].ch[1];
  18. s[x].siz=s[l].siz+s[r].siz+1;
  19. s[x].f[0][0]=s[l].f[0][0]+ab(s[r].f[0][0]-s[x].v-s[l].f[1][1]);
  20. s[x].f[1][1]=s[r].f[1][1]+ab(s[l].f[1][1]+s[x].v-s[r].f[0][0]);
  21. s[x].f[0][1]=s[l].f[0][1]+ab(s[r].f[0][1]+s[x].v-s[l].f[1][0]);
  22. s[x].f[1][0]=s[r].f[1][0]+ab(s[l].f[1][0]-s[x].v-s[r].f[0][1]);
  23. }
  24. inline void rev1(int x)
  25. {
  26. s[x].tag=-s[x].tag,s[x].v=-s[x].v,s[x].r1^=1,swap(s[x].f[0][0],s[x].f[0][1]),swap(s[x].f[1][0],s[x].f[1][1]);
  27. }
  28. inline void rev2(int x)
  29. {
  30. s[x].r2^=1,swap(s[x].ch[0],s[x].ch[1]),swap(s[x].f[0][0],s[x].f[1][0]),swap(s[x].f[0][1],s[x].f[1][1]);
  31. }
  32. inline void cover(int x,int y)
  33. {
  34. s[x].tag=s[x].v=y,s[x].f[0][y>0]=s[x].f[1][y>0]=s[x].siz,s[x].f[0][y<0]=s[x].f[1][y<0]=0,s[x].r1=s[x].r2=0;
  35. }
  36. inline void pushdown(int x)
  37. {
  38. int l=s[x].ch[0],r=s[x].ch[1];
  39. if(s[x].tag)
  40. {
  41. if(l) cover(l,s[x].tag);
  42. if(r) cover(r,s[x].tag);
  43. s[x].tag=0;
  44. }
  45. if(s[x].r1)
  46. {
  47. if(l) rev1(l);
  48. if(r) rev1(r);
  49. s[x].r1=0;
  50. }
  51. if(s[x].r2)
  52. {
  53. if(l) rev2(l);
  54. if(r) rev2(r);
  55. s[x].r2=0;
  56. }
  57. }
  58. inline void rotate(int x,int &k)
  59. {
  60. int y=s[x].fa,z=s[y].fa,d=(x==s[y].ch[1]);
  61. if(y!=k) s[z].ch[y==s[z].ch[1]]=x;
  62. else k=x;
  63. s[x].fa=z,s[y].fa=x,s[y].ch[d]=s[x].ch[d^1];
  64. if(s[x].ch[d^1]) s[s[x].ch[d^1]].fa=y;
  65. s[x].ch[d^1]=y;
  66. pushup(y),pushup(x);
  67. }
  68. inline void splay(int x,int &k)
  69. {
  70. while(x!=k)
  71. {
  72. int y=s[x].fa,z=s[y].fa;
  73. if(y!=k)
  74. {
  75. if((x==s[y].ch[0])^(y==s[z].ch[0])) rotate(x,k);
  76. else rotate(y,k);
  77. }
  78. rotate(x,k);
  79. }
  80. }
  81. int find(int x,int y)
  82. {
  83. if(!x) return 0;
  84. pushdown(x);
  85. if(s[s[x].ch[0]].siz>=y) return find(s[x].ch[0],y);
  86. if(s[s[x].ch[0]].siz+1<y) return find(s[x].ch[1],y-s[s[x].ch[0]].siz-1);
  87. return x;
  88. }
  89. inline void split(int a,int b)
  90. {
  91. splay(find(rt,a),rt),splay(find(rt,b+2),s[rt].ch[1]);
  92. }
  93. int build(int l,int r)
  94. {
  95. if(l>r) return 0;
  96. int x=(l+r)>>1;
  97. s[x].ch[0]=build(l,x-1),s[x].ch[1]=build(x+1,r);
  98. if(s[x].ch[0]) s[s[x].ch[0]].fa=x;
  99. if(s[x].ch[1]) s[s[x].ch[1]].fa=x;
  100. pushup(x);
  101. return x;
  102. }
  103. inline int rd()
  104. {
  105. int ret=0,f=1; char gc=getchar();
  106. while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=getchar();}
  107. while(gc>='0'&&gc<='9') ret=ret*10+(gc^'0'),gc=getchar();
  108. return ret*f;
  109. }
  110. int main()
  111. {
  112. //freopen("bz2329.in","r",stdin);
  113. n=rd(),m=rd();
  114. int i,a,b,x;
  115. scanf("%s",str+1);
  116. for(i=1;i<=n;i++) s[i+1].v=(str[i]=='(')?-1:1;
  117. rt=build(1,n+2);
  118. for(i=1;i<=m;i++)
  119. {
  120. scanf("%s",str),a=rd(),b=rd(),split(a,b),x=s[s[rt].ch[1]].ch[0];
  121. if(str[0]=='R')
  122. {
  123. scanf("%s",str);
  124. cover(x,(str[0]=='(')?-1:1),pushup(s[x].fa),pushup(s[s[x].fa].fa);
  125. }
  126. if(str[0]=='I') rev1(x),pushup(s[x].fa),pushup(s[s[x].fa].fa);
  127. if(str[0]=='S') rev2(x),pushup(s[x].fa),pushup(s[s[x].fa].fa);
  128. if(str[0]=='Q') printf("%d\n",((s[x].f[0][1]+1)>>1)+((s[x].f[1][0]+1)>>1));
  129. }
  130. return 0;
  131. }//4 5 (((( Replace 1 2 ) Query 1 2 Swap 2 3 Invert 3 4 Query 1 4
  132. //6 3 )(())( Q 1 6 Q 1 4 Q 3 4

【BZOJ2329/2209】[HNOI2011]括号修复/[Jsoi2011]括号序列 Splay的更多相关文章

  1. 洛谷 P3215 [HNOI2011]括号修复 / [JSOI2011]括号序列(fhq-treap)

    题目链接 题意:有一个长度为 \(n\) 的括号序列,你需要支持以下操作: 将 \([l,r]\) 中所有括号变为 \(c\) 将 \([l,r]\) 区间翻转 将 \([l,r]\) 区间中左括号变 ...

  2. [HNOI2011]括号修复 / [JSOI2011]括号序列

    传送门 Solution 一道题花费了两天的时间-- 在大佬@PinkRabbit的帮助下,终于AC了,感动-- 首先,我们考虑一个括号序列被修改成合法序列需要的次数: 我们需要修改的其实是形如... ...

  3. 【BZOJ-2329&2209】括号修复&括号序列 Splay

    2329: [HNOI2011]括号修复 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 1007  Solved: 476[Submit][Statu ...

  4. bzoj 2209: [Jsoi2011]括号序列 splay

    2209: [Jsoi2011]括号序列 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 833  Solved: 392[Submit][Status ...

  5. BZOJ 2209: [Jsoi2011]括号序列 [splay 括号]

    2209: [Jsoi2011]括号序列 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 1111  Solved: 541[Submit][Statu ...

  6. bzoj 2209 [Jsoi2011]括号序列 平衡树

    2209: [Jsoi2011]括号序列 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 1404  Solved: 699[Submit][Statu ...

  7. bzoj千题计划222:bzoj2329: [HNOI2011]括号修复(fhq treap)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2329 需要改变的括号序列一定长这样 :)))((( 最少改变次数= 多余的‘)’/2 [上取整] + ...

  8. BZOJ 2329/2209 [HNOI2011]括号修复 (splay)

    题目大意: 让你维护一个括号序列,支持 1.区间修改为同一种括号 2.区间内所有括号都反转 3.翻转整个区间,括号的方向不变 4.查询把某段区间变为合法的括号序列,至少需要修改多少次括号 给跪了,足足 ...

  9. BZOJ 2329: [HNOI2011]括号修复( splay )

    把括号序列后一定是))))((((这种形式的..所以维护一个最大前缀和l, 最大后缀和r就可以了..答案就是(l+1)/2+(r+1)/2...用splay维护,O(NlogN). 其实还是挺好写的, ...

随机推荐

  1. MVC教程二:从控制器中获取URL的值

    一.从控制器中获取URL的值有三种方式: 1.使用Request.QueryString[] 例如: string value = Request.QueryString["BookId&q ...

  2. R语言基于S4的面向对象编程

    前言 本文接上一篇文章 R语言基于S3的面向对象编程,本文继续介绍R语言基于S4的面向对象编程. S4对象系统具有明显的结构化特征,更适合面向对象的程序设计.Bioconductor社区,以S4对象系 ...

  3. 【这特么是个坑。。。】iOS 10.3下解决Charles抓包ssl证书信任问题

    针对近期iOS 10.3以上的系统charles抓https信任问题 前言 最近iPhone系统更新到ios 10.3后,在公司里用Charles抓包竟然出现了一些问题,https的请求都会失败,提示 ...

  4. pip -i 和 -U 参数

    例子: pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -U funcat -i: 指定库的安装源 -U:升级 原来已经安装的包,不带U ...

  5. 测试x264编码器的低延时编码和非延时编码

    最近在学x264的编码,经过大量的测试,编码1080P的视频,编码10000帧数据. 在设置为低延时编码的时候: 编码线程0,一帧耗时:7.000000 ms.编码线程0,一帧耗时:8.000000 ...

  6. android2.2应用开发之IccCard(sim卡或USIM卡)(转至 http://www.2cto.com/kf/201306/223784.html)

    如果要做android通讯录的联系人的机卡混排显示,由于手机卡类型的不同,导致手机卡存储容量以及可以存储信息不同,就要涉及到android去读Icc卡的信息. 一般的sim卡只能存储姓名跟一个电话号码 ...

  7. PL/SQL developer(绿色版)安装及配置

    1.PL/SQL Developer下载地址:百度网盘: 2.tsname.ora配置: orcl = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS )) ) (CO ...

  8. url中向后台传递中文乱码解决方法

    方法一: 1.jsp中代码 var userNo = $('#prisoner_id').val();      userNo = encodeURI(userNo);      allPrisone ...

  9. GOlang eclipse install

    http://golang.org/dl/ 下载golang https://codeload.github.com/GoClipse/goclipse/tar.gz/v0.8.1 解压 安装ecli ...

  10. 数据挖掘Apriori算法——学习笔记

    关联规则.频繁项集.支持度.置信度 关联规则挖掘: 一起购买的商品 支持度(support) 支持度会随着物品增多而减小.因为是同时购买的比率. 置信度(Confidence) 频繁且强规则,有一定意 ...