好吧我并不想讲LCT

只是贴4个代码~

【BZOJ2049】[Sdoi2008]Cave 洞穴勘测

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #define isr(A) (s[s[A].fa].ch[0]!=A&&s[s[A].fa].ch[1]!=A)
  5. using namespace std;
  6. const int maxn=10010;
  7. int n,m;
  8. struct NODE
  9. {
  10. int ch[2],fa,rev;
  11.  
  12. }s[10010];
  13. char str[20];
  14. void pushdown(int x)
  15. {
  16. if(s[x].rev)
  17. {
  18. swap(s[x].ch[0],s[x].ch[1]);
  19. if(s[x].ch[0]) s[s[x].ch[0]].rev^=1;
  20. if(s[x].ch[1]) s[s[x].ch[1]].rev^=1;
  21. s[x].rev=0;
  22. }
  23. }
  24. void rotate(int x)
  25. {
  26. int y=s[x].fa,z=s[y].fa,d=(x==s[y].ch[1]);
  27. if(!isr(y)) s[z].ch[y==s[z].ch[1]]=x;
  28. s[y].ch[d]=s[x].ch[d^1],s[y].fa=x,s[x].fa=z;
  29. if(s[x].ch[d^1]) s[s[x].ch[d^1]].fa=y;
  30. s[x].ch[d^1]=y;
  31. }
  32. void pd(int x){if(!isr(x)) pd(s[x].fa); pushdown(x);}
  33. void splay(int x)
  34. {
  35. pd(x);
  36. while(!isr(x))
  37. {
  38. int y=s[x].fa,z=s[y].fa;
  39. if(!isr(y))
  40. {
  41. if((x==s[y].ch[1])^(y==s[z].ch[1])) rotate(x);
  42. else rotate(y);
  43. }
  44. rotate(x);
  45. }
  46. }
  47. void access(int x)
  48. {
  49. int y=0;
  50. while(x) splay(x),s[x].ch[1]=y,y=x,x=s[x].fa;
  51. }
  52. void maker(int x)
  53. {
  54. access(x),splay(x),s[x].rev^=1;
  55. }
  56. int findr(int x)
  57. {
  58. access(x),splay(x);
  59. while(s[x].ch[0]) pushdown(x),x=s[x].ch[0];
  60. return x;
  61. }
  62. void link(int x,int y)
  63. {
  64. maker(x),s[x].fa=y;
  65. }
  66. void cut(int x,int y)
  67. {
  68. maker(y),access(x),splay(x);
  69. s[x].ch[0]=s[y].fa=0;
  70. }
  71. int rd()
  72. {
  73. int ret=0; char gc=getchar();
  74. while(gc<'0'||gc>'9') gc=getchar();
  75. while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
  76. return ret;
  77. }
  78. int main()
  79. {
  80. n=rd(),m=rd();
  81. int i,a,b;
  82. for(i=1;i<=m;i++)
  83. {
  84. scanf("%s",str);
  85. a=rd(),b=rd();
  86. switch(str[0])
  87. {
  88. case 'D':cut(a,b); break;
  89. case 'C':link(a,b); break;
  90. case 'Q':if(findr(a)==findr(b)) printf("Yes\n");
  91. else printf("No\n");
  92. }
  93. }
  94. return 0;
  95. }

【BZOJ2631】tree

此题的下传标记实在是长,好在我把它写到结构体里了

据说此题必须用unsigned int,不明觉厉~

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #define isr(A) (s[s[A].fa].ch[0]!=A&&s[s[A].fa].ch[1]!=A)
  5. #define mod 51061
  6. using namespace std;
  7. typedef unsigned int ui;
  8. struct node
  9. {
  10. ui ch[2],fa,rev;
  11. ui ts,tc,sum,siz,v;
  12. void C(ui x) {v=v*x%mod,sum=sum*x%mod,tc=tc*x%mod,ts=ts*x%mod;}
  13. void S(ui x) {v=(v+x)%mod,sum=(sum+siz*x)%mod,ts=(ts+x)%mod;}
  14. }s[100010];
  15. char str[10];
  16. ui n,m;
  17. void pushup(ui x)
  18. {
  19. s[x].siz=s[s[x].ch[0]].siz+s[s[x].ch[1]].siz+1;
  20. s[x].sum=(s[s[x].ch[0]].sum+s[s[x].ch[1]].sum+s[x].v)%mod;
  21. }
  22. void pushdown(ui x)
  23. {
  24. if(s[x].rev)
  25. {
  26. swap(s[x].ch[0],s[x].ch[1]);
  27. if(s[x].ch[0]) s[s[x].ch[0]].rev^=1;
  28. if(s[x].ch[1]) s[s[x].ch[1]].rev^=1;
  29. s[x].rev=0;
  30. }
  31. if(s[x].tc!=1)
  32. {
  33. if(s[x].ch[0]) s[s[x].ch[0]].C(s[x].tc);
  34. if(s[x].ch[1]) s[s[x].ch[1]].C(s[x].tc);
  35. s[x].tc=1;
  36. }
  37. if(s[x].ts)
  38. {
  39. if(s[x].ch[0]) s[s[x].ch[0]].S(s[x].ts);
  40. if(s[x].ch[1]) s[s[x].ch[1]].S(s[x].ts);
  41. s[x].ts=0;
  42. }
  43. }
  44. void rotate(ui x)
  45. {
  46. ui y=s[x].fa,z=s[y].fa,d=(x==s[y].ch[1]);
  47. if(!isr(y)) s[z].ch[y==s[z].ch[1]]=x;
  48. s[y].ch[d]=s[x].ch[d^1],s[x].fa=z,s[y].fa=x;
  49. if(s[x].ch[d^1]) s[s[x].ch[d^1]].fa=y;
  50. s[x].ch[d^1]=y;
  51. pushup(y),pushup(x);
  52. }
  53. void updata(ui x)
  54. {
  55. if(!isr(x)) updata(s[x].fa);
  56. pushdown(x);
  57. }
  58. void splay(ui x)
  59. {
  60. updata(x);
  61. while(!isr(x))
  62. {
  63. ui y=s[x].fa,z=s[y].fa;
  64. if(!isr(y))
  65. {
  66. if((x==s[y].ch[0])^(y==s[z].ch[0])) rotate(x);
  67. else rotate(y);
  68. }
  69. rotate(x);
  70. }
  71. }
  72. void access(ui x)
  73. {
  74. ui y=0;
  75. while(x) splay(x),s[x].ch[1]=y,pushup(x),y=x,x=s[x].fa;
  76. }
  77. void maker(ui x)
  78. {
  79. access(x),splay(x),s[x].rev^=1;
  80. }
  81. void cut(ui x,ui y)
  82. {
  83. maker(y),access(x),splay(x);
  84. s[x].ch[0]=s[y].fa=0,pushup(x);
  85. }
  86. void link(ui x,ui y)
  87. {
  88. maker(y),s[y].fa=x;
  89. }
  90. ui rd()
  91. {
  92. ui ret=0; char gc=getchar();
  93. while(gc<'0'||gc>'9') gc=getchar();
  94. while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
  95. return ret;
  96. }
  97. int main()
  98. {
  99. n=rd(),m=rd();
  100. ui i,a,b,c,d;
  101. for(i=1;i<=n;i++) s[i].v=1;
  102. for(i=1;i<n;i++) link(rd(),rd());
  103. for(i=1;i<=m;i++)
  104. {
  105. scanf("%s",str),a=rd(),b=rd();
  106. switch(str[0])
  107. {
  108. case '*':c=rd(),maker(a),access(b),splay(b),s[b].C(c); break;
  109. case '+':c=rd(),maker(a),access(b),splay(b),s[b].S(c); break;
  110. case '-':c=rd(),d=rd(),cut(a,b),link(c,d); break;
  111. case '/':maker(a),access(b),splay(b),printf("%u\n",s[b].sum); break;
  112. }
  113. }
  114. return 0;
  115. }

【BZOJ3282】Tree

判断一下是否联通就好了

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstring>
  4. #define isr(A) (s[s[A].fa].ch[0]!=A&&s[s[A].fa].ch[1]!=A)
  5. using namespace std;
  6. int n,m;
  7. struct node
  8. {
  9. int ch[2],fa,sum,v,rev;
  10. }s[300010];
  11. void pushup(int x)
  12. {
  13. s[x].sum=s[s[x].ch[0]].sum^s[s[x].ch[1]].sum^s[x].v;
  14. }
  15. void pushdown(int x)
  16. {
  17. if(s[x].rev)
  18. {
  19. swap(s[x].ch[0],s[x].ch[1]);
  20. if(s[x].ch[0]) s[s[x].ch[0]].rev^=1;
  21. if(s[x].ch[1]) s[s[x].ch[1]].rev^=1;
  22. s[x].rev=0;
  23. }
  24. }
  25. void rotate(int x)
  26. {
  27. int y=s[x].fa,z=s[y].fa,d=(x==s[y].ch[1]);
  28. if(!isr(y)) s[z].ch[y==s[z].ch[1]]=x;
  29. s[x].fa=z,s[y].fa=x,s[y].ch[d]=s[x].ch[d^1];
  30. if(s[x].ch[d^1]) s[s[x].ch[d^1]].fa=y;
  31. s[x].ch[d^1]=y;
  32. pushup(y),pushup(x);
  33. }
  34. void updata(int x)
  35. {
  36. if(!isr(x)) updata(s[x].fa);
  37. pushdown(x);
  38. }
  39. void splay(int x)
  40. {
  41. updata(x);
  42. while(!isr(x))
  43. {
  44. int y=s[x].fa,z=s[y].fa;
  45. if(!isr(y))
  46. {
  47. if((x==s[y].ch[0])^(y==s[z].ch[0])) rotate(x);
  48. else rotate(y);
  49. }
  50. rotate(x);
  51. }
  52. }
  53. void access(int x)
  54. {
  55. int y=0;
  56. while(x) splay(x),s[x].ch[1]=y,pushup(x),y=x,x=s[x].fa;
  57. }
  58. void maker(int x)
  59. {
  60. access(x),splay(x),s[x].rev^=1;
  61. }
  62. void cut(int x,int y)
  63. {
  64. maker(x),access(y),splay(y);
  65. if(s[x].fa==y) s[x].fa=s[y].ch[0]=0,pushup(y);
  66. }
  67. void link(int x,int y)
  68. {
  69. maker(x),s[x].fa=y;
  70. }
  71. void split(int a,int b)
  72. {
  73. maker(a),access(b),splay(b);
  74. }
  75. int findr(int x)
  76. {
  77. access(x),splay(x);
  78. while(s[x].ch[0]) x=s[x].ch[0];
  79. return x;
  80. }
  81. int rd()
  82. {
  83. int ret=0; char gc=getchar();
  84. while(gc<'0'||gc>'9') gc=getchar();
  85. while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
  86. return ret;
  87. }
  88. int main()
  89. {
  90. n=rd(),m=rd();
  91. int i,a,b,c;
  92. for(i=1;i<=n;i++) s[i].sum=s[i].v=rd();
  93. for(i=1;i<=m;i++)
  94. {
  95. c=rd(),a=rd(),b=rd();
  96. switch(c)
  97. {
  98. case 0:split(a,b),printf("%d\n",s[b].sum); break;
  99. case 1:if(findr(a)!=findr(b)) link(a,b); break;
  100. case 2:cut(a,b); break;
  101. case 3:splay(a),s[a].v=b,pushup(a); break;
  102. }
  103. }
  104. return 0;
  105. }

[CROATIAN2009]OTOCI

我经常把ch[]数组开成int ch[0];不知道有谁跟我经常犯一样的错误。。。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #define isr(A) (s[s[A].fa].ch[0]!=A&&s[s[A].fa].ch[1]!=A)
  5. using namespace std;
  6. int n,m;
  7. struct node
  8. {
  9. int ch[2],fa,sum,v,rev;
  10. }s[30010];
  11. char str[20];
  12. void pushup(int x)
  13. {
  14. s[x].sum=s[s[x].ch[0]].sum+s[s[x].ch[1]].sum+s[x].v;
  15. }
  16. void pushdown(int x)
  17. {
  18. if(s[x].rev)
  19. {
  20. swap(s[x].ch[0],s[x].ch[1]);
  21. if(s[x].ch[0]) s[s[x].ch[0]].rev^=1;
  22. if(s[x].ch[1]) s[s[x].ch[1]].rev^=1;
  23. s[x].rev=0;
  24. }
  25. }
  26. void rotate(int x)
  27. {
  28. int y=s[x].fa,z=s[y].fa,d=(x==s[y].ch[1]);
  29. if(!isr(y)) s[z].ch[y==s[z].ch[1]]=x;
  30. s[y].fa=x,s[x].fa=z,s[y].ch[d]=s[x].ch[d^1];
  31. if(s[x].ch[d^1]) s[s[x].ch[d^1]].fa=y;
  32. s[x].ch[d^1]=y;
  33. pushup(y),pushup(x);
  34. }
  35. void updata(int x)
  36. {
  37. if(!isr(x)) updata(s[x].fa);
  38. pushdown(x);
  39. }
  40. void splay(int x)
  41. {
  42. updata(x);
  43. while(!isr(x))
  44. {
  45. int y=s[x].fa,z=s[y].fa;
  46. if(!isr(y))
  47. {
  48. if((x==s[y].ch[0])^(y==s[z].ch[0])) rotate(x);
  49. else rotate(y);
  50. }
  51. rotate(x);
  52. }
  53. }
  54. void access(int x)
  55. {
  56. int y=0;
  57. while(x) splay(x),s[x].ch[1]=y,pushup(x),y=x,x=s[x].fa;
  58. }
  59. void maker(int x)
  60. {
  61. access(x),splay(x),s[x].rev^=1;
  62. }
  63. void link(int x,int y)
  64. {
  65. maker(x),s[x].fa=y;
  66. }
  67. int findr(int x)
  68. {
  69. access(x),splay(x);
  70. while(s[x].ch[0]) x=s[x].ch[0];
  71. return x;
  72. }
  73. void split(int x,int y)
  74. {
  75. maker(y),access(x),splay(x);
  76. }
  77. int rd()
  78. {
  79. int ret=0; char gc=getchar();
  80. while(gc<'0'||gc>'9') gc=getchar();
  81. while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
  82. return ret;
  83. }
  84. int main()
  85. {
  86. n=rd();
  87. int i,a,b,c;
  88. for(i=1;i<=n;i++) s[i].sum=s[i].v=rd();
  89. m=rd();
  90. for(i=1;i<=m;i++)
  91. {
  92. scanf("%s",str),a=rd(),b=rd();
  93. switch(str[0])
  94. {
  95. case 'b':if(findr(a)!=findr(b)) printf("yes\n"),link(a,b);
  96. else printf("no\n"); break;
  97. case 'p':splay(a),s[a].v=b,pushup(a); break;
  98. case 'e':if(findr(a)!=findr(b)) printf("impossible\n");
  99. else split(a,b),printf("%d\n",s[a].sum); break;
  100. }
  101. }
  102. return 0;
  103. }

【BZOJ2049,2631,3282,1180】LCT模板四连A的更多相关文章

  1. LCT模板

    之前一直用的LCT模板,因为其实个人对LCT和Splay不是很熟,所以用起来总觉得略略的坑爹,过了一段时间就忘了,但事实上很多裸的LCT要改的东西是不多的,所以今天写了些注释,以后可能套起模板来会得心 ...

  2. [BZOJ - 2631] tree 【LCT】

    题目链接:BZOJ - 2631 题目分析 LCT,像线段树区间乘,区间加那样打标记. 这道题我调了一下午. 提交之后TLE了,我一直以为是写错了导致了死循环. 于是一直在排查错误.直到.. 直到我看 ...

  3. LCT 模板及套路总结

    这一个月貌似已经考了无数次\(LCT\)了..... 保险起见还是来一发总结吧..... A. LCT 模板 \(LCT\) 是由大名鼎鼎的 \(Tarjan\) 老爷发明的. 主要是用来维护树上路径 ...

  4. BZOJ2049[Sdoi2008]洞穴勘测——LCT

    题目描述 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假如 ...

  5. Xamarin XAML语言教程构建ControlTemplate控件模板 (四)

    Xamarin XAML语言教程构建ControlTemplate控件模板 (四) 2.在页面级别中构建控件模板 如果开发者要在页面级别中构建控件模板,首先必须将ResourceDictionary添 ...

  6. [洛谷P1501] [国家集训队]Tree II(LCT模板)

    传送门 这是一道LCT的板子题,说白了就是在LCT上支持线段树2的操作. 所以我只是来存一个板子,并不会讲什么(再说我也不会,只能误人子弟2333). 不过代码里的注释可以参考一下. Code #in ...

  7. LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

    P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...

  8. BZOJ 1180 / 2843 LCT模板题_双倍经验

    一大早上到机房想先拍一下模板,热热身. 结果....对照着染色敲的 LCT 竟然死活也调不过去(你说我抄都能抄错) 干脆自己重新敲了一遍,10min就敲完了....... 还是要相信自己 Code: ...

  9. [BZOJ2049][Sdoi2008]Cave 洞穴勘测 LCT模板

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 9705  Solved: 4674[Submit] ...

随机推荐

  1. machine_learning-knn算法具体解释(近邻算法)

    近邻算法是机器学习算法中的入门算法,该算法用于针对已有数据集对未知数据进行分类. 该算法核心思想是通过计算预測数据与已有数据的相似度猜測结果. 举例: 如果有例如以下一组数据(在下面我们统一把该数据作 ...

  2. Spring注解@ResponseBody,@RequestBody

    @RequestBody 将HTTP请求正文转换为适合的HttpMessageConverter对象. @ResponseBody 将内容或对象作为 HTTP 响应正文返回,并调用适合HttpMess ...

  3. openWRT学习之LUCI之中的一个helloworld演示样例

    备注1:本文 讲述的是原生的openWRT环境下的LUCI 备注2:本文參考了诸多资料.感谢网友分享.參考资料: http://www.cnblogs.com/zmkeil/archive/2013/ ...

  4. ubuntu下安装自动补全YouCompleteMe

    一.安装预备软件.#vim要带python2.7的支持,curl是下载插件必须用到的软件,还有git apt install vim-nox-py2 curl git #安装python头文件 apt ...

  5. SCUT入门-环境搭建

    SCUT是一款基于C#且开源的游戏服务端框架,并且有一定的上线项目.最近正在入门中... 1.安装 去官网可以直接下载安装版:http://www.scutgame.com/ 源代码建议OSC Chi ...

  6. angularJs 页面定时刷新

    angularJs 页面定时刷新 页面定时刷新并在页面离开时停止自动刷新 var autoRefresh; //自动刷新 autoRefresh = $interval($scope.loadData ...

  7. MySQL 5.7.16 zip包配置

    截止2016/10/16 最新版本Mysql为5.7.16,之前写过一篇APMW搭建的文章(传送门:http://www.cnblogs.com/airoot/p/4131906.html)里面介绍的 ...

  8. ParagraphFormat 对象【精品】

    ParagraphFormat 对象 贡献者:motolola   日期:2009-05-27  阅读:5261  回复: 相关标签:wps  >  API  >  paragraphfo ...

  9. [开机启动]Linux开机自启和运行级别

    嵌入式系统中程序自启动方法 在很多嵌入式系统中,由于可用资源较少,常常在系统启动后就直接让应用程序自动启动,以减少用户操作和节省资源.如何让自己的应用程序自动启动呢?    在Linux系统中,配置应 ...

  10. 数据库 Proc编程二

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...