3282: Tree

Time Limit: 30 Sec  Memory Limit: 512 MB
Submit: 1714  Solved: 765
[Submit][Status][Discuss]

Description

给定N个点以及每个点的权值,要你处理接下来的M个操作。操作有4种。操作从0到3编号。点从1到N编号。

0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和。保证x到y是联通的。

1:后接两个整数(x,y),代表连接x到y,若x到Y已经联通则无需连接。

2:后接两个整数(x,y),代表删除边(x,y),不保证边(x,y)存在。

3:后接两个整数(x,y),代表将点X上的权值变成Y。

Input

第1行两个整数,分别为N和M,代表点数和操作数。

第2行到第N+1行,每行一个整数,整数在[1,10^9]内,代表每个点的权值。

第N+2行到第N+M+1行,每行三个整数,分别代表操作类型和操作所需的量。

Output

对于每一个0号操作,你须输出X到Y的路径上点权的Xor和。

Sample Input

3 3
1
2
3
1 1 2
0 1 2
0 1 1

Sample Output

3
1

HINT

1<=N,M<=300000

Source

[Submit][Status][Discuss]

莫名其妙的红色,LCT模板题

  1. #include <bits/stdc++.h>
  2.  
  3. inline int nextChar(void) {
  4. static const int siz = 1 << 20;
  5.  
  6. static char buffer[siz];
  7. static char *head = buffer + siz;
  8. static char *tail = buffer + siz;
  9.  
  10. if (head == tail)fread(head = buffer, 1, siz, stdin);
  11.  
  12. return int(*head++);
  13. }
  14.  
  15. inline int nextInt(void) {
  16. register int ret = 0;
  17. register int neg = false;
  18. register int bit = nextChar();
  19.  
  20. for (; bit < 48; bit = nextChar())
  21. if (bit == '-')neg ^= true;
  22.  
  23. for (; bit > 47; bit = nextChar())
  24. ret = ret * 10 + bit - '0';
  25.  
  26. return neg ? -ret : ret;
  27. }
  28.  
  29. const int mxn = 300005;
  30.  
  31. int n, m, top;
  32.  
  33. int stk[mxn];
  34. int val[mxn];
  35. int sum[mxn];
  36. int fat[mxn];
  37. int rev[mxn];
  38. int son[mxn][2];
  39.  
  40. inline bool isroot(int t) {
  41. int f = fat[t];
  42. if (!f)return true;
  43. if (son[f][0] == t)return false;
  44. if (son[f][1] == t)return false;
  45. return true;
  46. }
  47.  
  48. inline void update(int t) {
  49. sum[t] = val[t];
  50. if (son[t][0])sum[t] ^= sum[son[t][0]];
  51. if (son[t][1])sum[t] ^= sum[son[t][1]];
  52. }
  53.  
  54. inline void push(int t) {
  55. rev[t] = 0;
  56. std::swap(son[t][0], son[t][1]);
  57. if (son[t][0])rev[son[t][0]] ^= 1;
  58. if (son[t][1])rev[son[t][1]] ^= 1;
  59. }
  60.  
  61. inline void pushdown(int t) {
  62. for (stk[++top] = t; t; )
  63. stk[++top] = t = fat[t];
  64. for (; top; --top)
  65. if (rev[stk[top]])
  66. push(stk[top]);
  67. }
  68.  
  69. inline void connect(int t, int f, int k) {
  70. if (t)fat[t] = f;
  71. if (f)son[f][k] = t;
  72. }
  73.  
  74. inline void rotate(int t) {
  75. int f = fat[t];
  76. int g = fat[f];
  77. int s = son[f][1] == t;
  78. connect(son[t][!s], f, s);
  79. connect(f, t, !s);
  80. fat[t] = g;
  81. if (g && son[g][0] == f)son[g][0] = t;
  82. if (g && son[g][1] == f)son[g][1] = t;
  83. update(f);
  84. update(t);
  85. }
  86.  
  87. inline void splay(int t) {
  88. pushdown(t);
  89. while (!isroot(t)) {
  90. int f = fat[t];
  91. int g = fat[f];
  92. if (isroot(f))
  93. rotate(t);
  94. else {
  95. int a = f && son[f][1] == t;
  96. int b = g && son[g][1] == f;
  97. if (a == b)
  98. rotate(f), rotate(t);
  99. else
  100. rotate(t), rotate(t);
  101. }
  102. }
  103. }
  104.  
  105. inline void access(int t) {
  106. for (int p = 0; t; p = t, t = fat[t])
  107. splay(t), son[t][1] = p, update(t);
  108. }
  109.  
  110. inline void makeroot(int t) {
  111. access(t), splay(t), rev[t] ^= 1;
  112. }
  113.  
  114. inline void cut(int a, int b) {
  115. makeroot(a), access(b), splay(b);
  116. if (son[b][0] == a)son[b][0] = fat[a] = 0;
  117. }
  118.  
  119. inline void link(int t, int f) {
  120. makeroot(t), fat[t] = f;
  121. }
  122.  
  123. inline int find(int t) {
  124. access(t), splay(t);
  125. while (son[t][0])
  126. t = son[t][0];
  127. return t;
  128. }
  129.  
  130. signed main(void) {
  131. n = nextInt();
  132. m = nextInt();
  133. for (int i = 1; i <= n; ++i)
  134. val[i] = sum[i] = nextInt();
  135. for (int i = 1; i <= m; ++i) {
  136. int k = nextInt();
  137. int x = nextInt();
  138. int y = nextInt();
  139. switch (k) {
  140. case 0 :
  141. makeroot(x);
  142. access(y);
  143. splay(y);
  144. printf("%d\n", sum[y]);
  145. break;
  146. case 1:
  147. if (find(x) != find(y))
  148. link(x, y);
  149. break;
  150. case 2:
  151. if (find(x) == find(y))
  152. cut(x, y);
  153. break;
  154. case 3:
  155. access(x);
  156. splay(x);
  157. val[x] = y;
  158. update(x);
  159. break;
  160. }
  161. }
  162. }

  

@Author: YouSiki

BZOJ 3282: Tree的更多相关文章

  1. [BZOJ 3282] Tree 【LCT】

    题目链接:BZOJ - 3282 题目分析 这道题是裸的LCT,包含 Link , Cut 和询问两点之间的路径信息. 写代码时出现的错误:Access(x) 的循环中应该切断的是原来的 Son[x] ...

  2. BZOJ 3282: Tree( LCT )

    LCT.. -------------------------------------------------------------------------------- #include<c ...

  3. bzoj 3282: Tree (Link Cut Tree)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3282 题面: 3282: Tree Time Limit: 30 Sec  Memory L ...

  4. BZOJ 3282 Tree Link-Cut-Tree(LCT)

    题目大意: 给定N个点以及每一个点的权值,要你处理接下来的M个操作.操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y ...

  5. BZOJ 3282 Tree(动态树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3282 [题目大意] 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的x ...

  6. BZOJ 3282 Tree ——KD-Tree

    [题目分析] 明显的LCT维护连通性的题目. access的操作是比较巧妙的,可以把结点到根变成偏爱路径,而且保证了该点是链上深度最深的点. 而且需边的思想也很巧妙,保证了复杂度. 但是只能用于修改路 ...

  7. BZOJ 3282 Tree ——Link-Cut Tree

    [题目分析] 明显的LCT维护连通性的题目. access的操作是比较巧妙的,可以把结点到根变成偏爱路径,而且保证了该点是链上深度最深的点. 而且需边的思想也很巧妙,保证了复杂度. 但是只能用于修改路 ...

  8. 洛谷 P3690 【模板】Link Cut Tree (动态树) || bzoj 3282: Tree

    https://blog.csdn.net/saramanda/article/details/55253627 https://blog.csdn.net/CHHNZ/article/details ...

  9. BZOJ 2002 && BZOJ 2409 LCT && BZOJ 3282 初步练习

    #include <cstdio> ; inline void Get_Int(int & x) { ; ') ch=getchar(); +ch-'; ch=getchar(); ...

随机推荐

  1. Spring学习(5):DI的配置

    一.  一些概念 应用程序中说的依赖一般指类之间的关系. 泛化:表示类与类之间的继承关系.接口与接口之间的继承关系: 实现:表示类对接口的实现: 依赖:当类与类之间有使用关系时就属于依赖关系,不同于关 ...

  2. OpenFastPath(2):原生态Linux Socket应用如何移植到OpenFastPath上?

    版本信息: ODP(Open Data Plane): 1.19.0.2 OFP(Open Fast Path): 3.0.0 1.存在的问题 OpenFastPath作为一个开源的用户态TCP/IP ...

  3. Hadoop源码阅读环境搭建(IDEA)

    拿到一份Hadoop源码之后,经常关注的两件事情就是 1.怎么阅读?涉及IDEA和Eclipse工程搭建.IDEA搭建,选择源码,逐步导入即可:Eclipse可以选择后台生成工程,也可以选择IDE导入 ...

  4. 如何掌握 Kubernetes ?系统学习 k8s 的大纲一份

    深度剖析 Kubernetes 深度剖析 k8s 如何学习 Kubernetes ?如何入门 Kubernetes? 为了帮帮初学者,2018 年 InfoQ 旗下(就是你知道的那个 InfoQ 哇) ...

  5. 三羊献瑞:next_permutation()

    三羊献瑞 观察下面的加法算式: 祥 瑞 生 辉  +   三 羊 献 瑞-------------------   三 羊 生 瑞 气 (如果有对齐问题,可以参看[图1.jpg]) 其中,相同的汉字代 ...

  6. ssm-maven 所需添加的所有映射

    <dependencies> <!--Mybatis依赖--> <dependency> <groupId>org.mybatis</groupI ...

  7. python os.walk详解

    os模块大全详情 os.walkos.walk方法,主要用来遍历一个目录内各个子目录和子文件. os.walk(top, topdown=True, onerror=None, followlinks ...

  8. Openresty(Lua+Nginx)实践

    简介: OpenResty(也称为 ngx_openresty)是一个全功能的 Web 应用服务器.它打包了标准的 Nginx 核心,很多的常用的第三方模块,以及它们的大多数依赖项. OpenRest ...

  9. c# bitmap和new bitmap(bitmap)及在System.Drawing.Image.get_RawFormat()报错“参数无效”

    问题情境: 给picturebox赋image属性,我用一下代码,出错: Bitmap theBitmap = convertCameraData.display(rawDataArray, heig ...

  10. 修复webpack自动刷新页面慢的问题

    新建.babelrc文件,配置如下 { "presets": [ "es2015" ], "ignore":[ "react-ro ...