题目链接

岛娘出的题。还是比較easy的

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <time.h>
  5. #include <vector>
  6. #include <map>
  7. #include <queue>
  8. #include <algorithm>
  9. #include <stack>
  10. #include <cstring>
  11. #include <cmath>
  12. #include <set>
  13. #include <vector>
  14. using namespace std;
  15. template <class T>
  16. inline bool rd(T &ret) {
  17. char c; int sgn;
  18. if (c = getchar(), c == EOF) return 0;
  19. while (c != '-' && (c<'0' || c>'9')) c = getchar();
  20. sgn = (c == '-') ?
  21.  
  22. -1 : 1;
  23. ret = (c == '-') ?
  24.  
  25. 0 : (c - '0');
  26. while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
  27. ret *= sgn;
  28. return 1;
  29. }
  30. template <class T>
  31. inline void pt(T x) {
  32. if (x <0) {
  33. putchar('-');
  34. x = -x;
  35. }
  36. if (x>9) pt(x / 10);
  37. putchar(x % 10 + '0');
  38. }
  39. typedef long long ll;
  40. typedef pair<int, int> pii;
  41. const int N = 100005;
  42. const int inf = 10000000;
  43. struct Node *null;
  44. struct Node {
  45. Node *fa, *ch[2];
  46. int id;
  47. int s[2];//s[0] this虚边所连的全部子树的连续白色点数总和(不是链)
  48. int col;
  49. int ls[2], rs[2], siz;
  50. //ls[0]是对于这条链 最上端向下的连续白色点数
  51. int Ls[2], Rs[2];
  52. //Ls[0]是对于这棵子树 与最上端点相连的连续白色点数
  53. bool rev;
  54. inline void clear(int _col, int _id) {
  55. fa = ch[0] = ch[1] = null;
  56. siz = 1;
  57. rev = 0;
  58. id = _id;
  59. col = _col;
  60. for (int i = 0; i < 2; i++) {
  61. ls[i] = rs[i] = s[i] = 0;
  62. }
  63. }
  64. inline void push_up() {
  65. if (this == null)return;
  66. siz = ch[0]->siz + ch[1]->siz + 1;
  67. for (int i = 0; i < 2; i++) {
  68. ls[i] = ch[0]->ls[i], rs[i] = ch[1]->rs[i];
  69. Ls[i] = ch[0]->Ls[i], Rs[i] = ch[1]->Rs[i];
  70. if (ch[0]->ls[i] == ch[0]->siz && i == col) {
  71. ls[i] = ch[0]->siz + 1 + ch[1]->ls[i];
  72. Ls[i]++;
  73. Ls[i] += s[i];
  74. Ls[i] += ch[1]->Ls[i];
  75. }
  76. if (ch[1]->rs[i] == ch[1]->siz && i == col) {
  77. rs[i] = ch[1]->siz + 1 + ch[0]->rs[i];
  78. Rs[i]++;
  79. Rs[i] += s[i];
  80. Rs[i] += ch[0]->Rs[i];
  81. }
  82. }
  83. }
  84. inline void push_down() {
  85. if (rev) {
  86. ch[0]->flip();
  87. ch[1]->flip();
  88. rev = 0;
  89. }
  90. }
  91. inline void setc(Node *p, int d) {
  92. ch[d] = p;
  93. p->fa = this;
  94. }
  95. inline bool d() {
  96. return fa->ch[1] == this;
  97. }
  98. inline bool isroot() {
  99. return fa == null || fa->ch[0] != this && fa->ch[1] != this;
  100. }
  101. inline void flip() {
  102. if (this == null)return;
  103. swap(ch[0], ch[1]);
  104. rev ^= 1;
  105. }
  106. inline void go() {//从链头開始更新到this
  107. if (!isroot())fa->go();
  108. push_down();
  109. }
  110. inline void rot() {
  111. Node *f = fa, *ff = fa->fa;
  112. int c = d(), cc = fa->d();
  113. f->setc(ch[!c], c);
  114. this->setc(f, !c);
  115. if (ff->ch[cc] == f)ff->setc(this, cc);
  116. else this->fa = ff;
  117. f->push_up();
  118. }
  119. inline Node*splay() {
  120. go();
  121. while (!isroot()) {
  122. if (!fa->isroot())
  123. d() == fa->d() ? fa->rot() : rot();
  124. rot();
  125. }
  126. push_up();
  127. return this;
  128. }
  129. inline Node* access() {//access后this就是到根的一条splay。而且this已经是这个splay的根了
  130. for (Node *p = this, *q = null; p != null; q = p, p = p->fa) {
  131. p->splay();
  132. if (p->ch[1] != null)
  133. for (int i = 0;i < 2;i++)
  134. p->s[i] += p->ch[1]->Ls[i];
  135. if (q != null)
  136. for (int i = 0; i < 2; i++)
  137. p->s[i] -= q->Ls[i];
  138. p->setc(q, 1);
  139. p->push_up();
  140. }
  141. return splay();
  142. }
  143. inline Node* find_root() {
  144. Node *x;
  145. for (x = access(); x->push_down(), x->ch[0] != null; x = x->ch[0]);
  146. return x;
  147. }
  148. void make_root() {
  149. access()->flip();
  150. }
  151. void cut() {//把这个点的子树脱离出去
  152. access();
  153. ch[0]->fa = null;
  154. ch[0] = null;
  155. push_up();
  156. }
  157. void cut(Node *x) {
  158. if (this == x || find_root() != x->find_root())return;
  159. else {
  160. x->make_root();
  161. cut();
  162. }
  163. }
  164. void link(Node *x) {
  165. if (find_root() == x->find_root())return;
  166. else {
  167. make_root(); fa = x;
  168. }
  169. }
  170. };
  171. Node pool[N], *tail;
  172. Node *node[N];
  173. int n, q;
  174. struct Edge {
  175. int to, nex;
  176. }edge[N << 1];
  177. int head[N], edgenum;
  178. void add(int u, int v) {
  179. Edge E = { v, head[u] };
  180. edge[edgenum] = E;
  181. head[u] = edgenum++;
  182. }
  183. void dfs(int u, int fa) {
  184. for (int i = head[u]; ~i; i = edge[i].nex) {
  185. int v = edge[i].to; if (v == fa)continue;
  186. node[v]->fa = node[u];
  187. dfs(v, u);
  188. for (int j = 0; j < 2; j++)
  189. node[u]->s[j] += node[v]->Ls[j];
  190. }
  191. node[u]->push_up();
  192. }
  193. int main() {
  194. while (cin >> n) {
  195. memset(head, -1, sizeof head); edgenum = 0;
  196. for (int i = 1, u, v; i < n; i++) {
  197. rd(u); rd(v);
  198. add(u, v);add(v, u);
  199. }
  200. tail = pool;
  201. null = tail++;
  202. null->clear(-1, 0); null->siz = 0;
  203. for (int i = 1; i <= n; i++)
  204. {
  205. node[i] = tail++;
  206. node[i]->clear(1, i);
  207. }
  208. dfs(1, 1);
  209. rd(q); int u, v;
  210. while (q--) {
  211. rd(u); rd(v);
  212. if (!u) {
  213. node[v]->access();
  214. pt(max(node[v]->Rs[0], node[v]->Rs[1])); puts("");
  215. }
  216. else {
  217. node[v]->access();
  218. node[v]->col ^= 1;
  219. node[v]->push_up();
  220. }
  221. }
  222. }
  223. return 0;
  224. }

SPOJ QTREE6 lct的更多相关文章

  1. SPOJ - OTOCI LCT

    OTOCI Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/viewProblem. ...

  2. SPOJ QTREE4 lct

    题目链接 这个题已经处于花式tle了,改版后的spoj更慢了.. tle的话就多交几把... #include <iostream> #include <fstream> #i ...

  3. SPOJ QTREE6

    题意 给你一棵\(n\)个点的树,编号\(1\)~\(n\).每个点可以是黑色,可以是白色.初始时所有点都是黑色.有两种操作 \(0\ u\):询问有多少个节点\(v\)满足路径\(u\)到\(v\) ...

  4. SPOJ QTREE3 lct

    题目链接 题意: 给定n个点 q个询问 以下n-1行给出树边,点有黑或白色.初始化为白色 以下q行: 询问有2种: 1. 0 x 把x点黑变白,白变黑 2.1 x 询问Path(1,x)路径上第一个黑 ...

  5. SPOJ QTREE2 lct

    题目链接 题意: 给一棵树.有边权 1.询问路径的边权和 2.询问沿着路径的第k个点标. 思路:lct裸题. #include <iostream> #include <fstrea ...

  6. SPOJ QTREE5 lct

    题目链接 对于每一个节点,记录这个节点所在链的信息: ls:(链的上端点)距离链内部近期的白点距离 rs:(链的下端点)距离链内部近期的白点距离 注意以上都是实边 虚边的信息用一个set维护. set ...

  7. SPOJ QTREE6 Query on a tree VI 树链剖分

    题意: 给出一棵含有\(n(1 \leq n \leq 10^5)\)个节点的树,每个顶点只有两种颜色:黑色和白色. 一开始所有的点都是黑色,下面有两种共\(m(1 \leq n \leq 10^5) ...

  8. bzoj3637 CodeChef SPOJ - QTREE6 Query on a tree VI 题解

    题意: 一棵n个节点的树,节点有黑白两种颜色,初始均为白色.两种操作:1.更改一个节点的颜色;2.询问一个节点所处的颜色相同的联通块的大小. 思路: 1.每个节点记录仅考虑其子树时,假设其为黑色时所处 ...

  9. 【SPOJ】QTREE6(Link-Cut-Tree)

    [SPOJ]QTREE6(Link-Cut-Tree) 题面 Vjudge 题解 很神奇的一道题目 我们发现点有黑白两种,又是动态加边/删边 不难想到\(LCT\) 最爆力的做法,显然是每次修改单点颜 ...

随机推荐

  1. vue计算属性computed和methods的区别

    computed和methods的区别 在new Vue的配置参数中的computed和methods都可以处理大量的逻辑代码,但是什么时候用哪个属性,要好好区分一下才能做到正确的运用vue. com ...

  2. 小程序之如何设置图片以及image组件的属性

    1. 设置图片,小程序支持两种引用图片方法,一种是本地引用,一种是网络资源引用. 但是引用本地图片的的时候不能用wxml样式去引用本地的图片,不会报错,也没效果.就是在wxss页面中不能引用本地的图片 ...

  3. sql语句执行顺序与性能优化(1)

    一.首先我们看一下mysql的sql语句的书写顺序 . select--distinct--from--on--where--group by--having--聚合函数cube.rollup--or ...

  4. Linux启动流程CentOS6和7的区别

    目 录 Linux启动流程    I 第1章 CentOS6启动流程    1 1.1 BIOS    1 1.2 MBR    1 1.3 GRUB    1 1.4 kernel(加载内核)    ...

  5. CSS3---关于背景

    1.background-origin:设置元素背景图片的原始起始位置. background-origin : border-box | padding-box | content-box;    ...

  6. Elastic-Job-Lite 源码分析 —— 作业分片策略

    摘要: 原创出处 http://www.iocoder.cn/Elastic-Job/job-sharding-strategy/ 「芋道源码」欢迎转载,保留摘要,谢谢! 本文基于 Elastic-J ...

  7. luogu3157 [CQOI2011]动态逆序对

    先算出一个点前头比它大和后头比它小的数量. 每次删点就扔进一个主席树里头,防止造成重复删答案. #include <iostream> #include <cstring> # ...

  8. OO第二次作业

    第一次作业: 由于第一次作业的调度较为简单,采用FIFO策略,以及不支持捎带功能,因此我的第一次电梯作业并没有设置单独的调度器,而会直接将任务交给电梯,电梯进行调度策略也仅为先运动到people的In ...

  9. [luoguP1186] 玛丽卡(spfa)

    传送门 因为要随机删除一条边,而枚举所有边肯定会超时,经过发现,先求出一遍最短路,而要删除的边肯定在最短路径上,删除其他的边对最短路没有影响. 所以可以先求出最短路,再枚举删除最短路上的每一条边再求最 ...

  10. [HDU3062]Party(2-sat)

    传送门 2-sat问题,只需要判断yes或no 所以可以直接连边,缩点,判断同一组的是否在同一个块中. #include <cstdio> #include <stack> # ...