Little Devil I

Problem Description
There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.

The devil likes to make thing in chaos. This kingdom’s road system is like simply a tree(connected graph without cycle). A road has a color of black or white. The devil often wants to make some change of this system.

In details, we call a path on the tree from a to b consists of vertices lie on the shortest simple path between a and b. And we say an edge is on the path if both its two endpoints is in the path, and an edge is adjacent to the path if exactly one endpoint of it is in the path.

Sometimes the devil will ask you to reverse every edge’s color on a path or adjacent to a path.

The king’s daughter, WJMZBMR, is also a cute loli, she is surprised by her father’s lolicon-like behavior. As she is concerned about the road-system’s status, sometimes she will ask you to tell there is how many black edge on a path.

Initially, every edges is white.

 
Input
The first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains an integer n, which is the size of the tree. The vertices be indexed from 1.
On the next n-1 lines, each line contains two integers a,b, denoting there is an edge between a and b. 
The next line contains an integer Q, denoting the number of the operations.
On the next Q lines, each line contains three integers t,a,b. t=1 means we reverse every edge’s color on path a to b. t=2 means we reverse every edge’s color adjacent to path a to b. t=3 means we query about the number of black edge on path a to b.

T<=5.
n,Q<=10^5.
Please use scanf,printf instead of cin,cout,because of huge input.

 
Output
For each t=3 operation, output the answer in one line.
 
Sample Input
1
10
2 1
3 1
4 1
5 1
6 5
7 4
8 3
9 5
10 6

10
2 1 6
1 3 8
3 8 10
2 3 4
2 10 8
2 4 10
1 7 6
2 7 3
2 1 4
2 10 10

 
Sample Output
3

Hint

reverse color means change from white to black or vice virsa.

 
Author
WJMZBMR
 

题意:

  给定一棵树,每条边有黑白两种颜色,初始都是白色,现在有三种操作:

     1 u v:u到v路径上的边都取成相反的颜色

     2 u v:u到v路径上相邻的边都取成相反的颜色(相邻即仅有一个节点在路径上)

         3 u v:查询u到v路径上有多少个黑色边

题解:

  这题卡了我一天

  首先树剖一下,

  操作1,对于一条重链,我们直接在线段树W上修改即可,这就是个简单的区间线段树,

      轻边的话,我们观察到它是由2个点决定的,那么考虑到操作2,我们可以思考到,假设一条的一个端点别进行了操作2,那么这条边就被修改了,两个端点同时被操作,那么我们不修改,这样的话此时我们将这里的轻边的两个端点都异或1就好了,这里需要用到线段树L

  操作2,对于重链,我们直接在线段树L上进行修改,但是我们要考虑到途中的重边或许会与改路径相连但又不在此路径上,

      熟悉树剖过程的话,其实也就在端点两端会有重边相接,和向上跳的过程中,轻边旁会有相交的重边,只要对跳点的重儿子进行线段树W上的修改即可

  操作3,重链,我们直接查询线段树W就好了,轻边的话我们查询L进行异或就行

  

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #pragma comment(linker, "/STACK:102400000,102400000")
  4. #define ls i<<1
  5. #define rs ls | 1
  6. #define mid ((ll+rr)>>1)
  7. #define pii pair<int,int>
  8. #define MP make_pair
  9. typedef long long LL;
  10. const long long INF = 1e18+1LL;
  11. const double Pi = acos(-1.0);
  12. const int N = 5e5+, M = 1e3+, mod = 1e9+, inf = 2e9;
  13.  
  14. int dep[N],head[N],t=,sz[N],fa[N],indexS,top[N],pos[N],son[N];
  15. struct ss{int to,next;}e[N*];
  16. int n;
  17. void add(int u,int v)
  18. {e[t].to = v;e[t].next = head[u];head[u] = t++;}
  19. void dfs(int u) {
  20. int k = ;
  21. sz[u] = ;
  22. dep[u] = dep[fa[u]] + ;
  23. for(int i = head[u]; i; i = e[i].next) {
  24. int to = e[i].to;
  25. if(to == fa[u]) continue;
  26. fa[to] = u;
  27. dfs(to);
  28. sz[u] += sz[to];
  29. if(sz[to] > sz[k]) k = to;
  30. }
  31. if(k) son[u] = k;
  32. }
  33. void dfs(int u,int chain) {
  34. int k = ;
  35. pos[u] = ++indexS;
  36. top[u] = chain;
  37. if(son[u] > )
  38. dfs(son[u],chain);
  39. for(int i = head[u]; i; i = e[i].next) {
  40. int to = e[i].to;
  41. if(dep[to] > dep[u] && son[u] != to)
  42. dfs(to,to);
  43. }
  44. }
  45. int lazy[N],sum[N],L[N],lazyw[N],color[N];
  46. void init() {
  47. indexS = ;
  48. t = ;
  49. memset(sz,,sizeof(sz));
  50. memset(head,,sizeof(head));
  51. memset(fa,,sizeof(fa));
  52. memset(son,,sizeof(son));
  53. memset(L,,sizeof(L));
  54. memset(sum,,sizeof(sum));
  55. memset(lazy,,sizeof(lazy));
  56. memset(lazyw,,sizeof(lazyw));
  57. memset(color,,sizeof(color));
  58. memset(L,,sizeof(L));
  59. }
  60. void push_down(int i,int ll,int rr) {
  61. if(lazyw[i] && ll != rr) {
  62. lazyw[ls] ^= ;
  63. if(ll!=)sum[ls] = (mid-ll+) - sum[ls];
  64. else sum[ls] = (mid-ll) - sum[ls];
  65. lazyw[rs] ^= ;
  66. sum[rs] = (rr - mid) - sum[rs];
  67. }
  68. lazyw[i] = ;
  69. }
  70. void push_up(int i,int ll,int rr) {
  71. sum[i] = sum[ls] + sum[rs];
  72. }
  73. int queryW(int i,int ll,int rr,int x,int y) {
  74. int res = ;
  75. push_down(i,ll,rr);
  76. if(ll == x && rr == y)
  77. return sum[i];
  78. if(y <= mid) res += queryW(ls,ll,mid,x,y);
  79. else if(x > mid) res += queryW(rs,mid+,rr,x,y);
  80. else {
  81. res += queryW(ls,ll,mid,x,mid);
  82. res += queryW(rs,mid+,rr,mid+,y);
  83. }
  84. push_up(i,ll,rr);
  85. return res;
  86. }
  87. int queryL(int i,int ll,int rr,int x) {
  88. if(lazy[i]) {
  89. if(ll == rr) L[i] ^= ;
  90. else {
  91. lazy[ls] ^= ;
  92. lazy[rs] ^= ;
  93. }
  94. lazy[i] = ;
  95. }
  96. if(ll == rr) return L[i];
  97. if(x <= mid) return queryL(ls,ll,mid,x);
  98. else return queryL(rs,mid+,rr,x);
  99. }
  100. void updateL(int i,int ll,int rr,int x,int y) {
  101. if(ll == x && rr == y) {
  102. lazy[i] ^= ;
  103. return ;
  104. }
  105. if(y <= mid) updateL(ls,ll,mid,x,y);
  106. else if(x > mid) updateL(rs,mid+,rr,x,y);
  107. else {
  108. updateL(ls,ll,mid,x,mid);
  109. updateL(rs,mid+,rr,mid+,y);
  110. }
  111. }
  112. void updateW(int i,int ll,int rr,int x,int y) {
  113. push_down(i,ll,rr);
  114. if(ll == x && rr == y) {
  115. lazyw[i] ^= ;
  116. if(ll != )sum[i] = rr-ll+ - sum[i];
  117. else sum[i] = rr-ll+- - sum[i];
  118. return ;
  119. }
  120. if(y <= mid) updateW(ls,ll,mid,x,y);
  121. else if(x > mid) updateW(rs,mid+,rr,x,y);
  122. else {
  123. updateW(ls,ll,mid,x,mid);
  124. updateW(rs,mid+,rr,mid+,y);
  125. }
  126. push_up(i,ll,rr);
  127. }
  128. void updateL(int x,int y) {
  129. int firx = , firy = ;
  130. while(top[x] != top[y]) {
  131. if(dep[top[x]] < dep[top[y]]) swap(x,y),swap(firx,firy);
  132. if(son[x])updateW(,,n,pos[son[x]],pos[son[x]]);
  133. updateL(,,n,pos[top[x]],pos[x]);
  134. x = fa[top[x]];
  135. }
  136. if(dep[x] > dep[y]) swap(x,y);
  137. if(son[x] && x==y)
  138. updateW(,,n,pos[son[x]],pos[son[x]]);
  139. if(x!=y && son[y])updateW(,,n,pos[son[y]],pos[son[y]]);
  140. if(x != && son[fa[x]] == x) updateW(,,n,pos[x],pos[x]);
  141. updateL(,,n,pos[x],pos[y]);
  142. }
  143. void updateW(int x,int y) {
  144. while(top[x] != top[y]) {
  145. if(dep[top[x]] < dep[top[y]]) swap(x,y);
  146. if(top[x] != x) updateW(,,n,pos[son[top[x]]],pos[x]);
  147. color[top[x]]^=;
  148. x = fa[top[x]];
  149. }
  150. if(x == y) {}
  151. else {
  152. if(dep[x] < dep[y] && son[x]) updateW(,,n,pos[son[x]],pos[y]);
  153. else if(son[y])updateW(,,n,pos[son[y]],pos[x]);
  154. }
  155. }
  156. int solve(int x,int y) {
  157. int res = ;
  158. while(top[x] != top[y]) {
  159. if(dep[top[x]] < dep[top[y]]) swap(x,y);
  160. if(top[x] != x) res += queryW(,,n,pos[son[top[x]]],pos[x]);
  161. int fx = queryL(,,n,pos[top[x]])^queryL(,,n,pos[fa[top[x]]]);
  162. res += fx^color[top[x]];
  163. x = fa[top[x]];
  164. }
  165. if(x == y) {
  166. }
  167. else {
  168. if(dep[x] < dep[y] && son[x])res += queryW(,,n,pos[son[x]],pos[y]);
  169. else if(son[y])res += queryW(,,n,pos[son[y]],pos[x]);
  170. }
  171. return res;
  172. }
  173. int main() {
  174. int T;
  175. scanf("%d",&T);
  176. while(T--) {
  177. scanf("%d",&n);
  178. init();
  179. for(int i = ; i < n; ++i) {
  180. int x,y;
  181. scanf("%d%d",&x,&y);
  182. add(x,y),add(y,x);
  183. }
  184. dfs();
  185. dfs(,);
  186. int Q;
  187. scanf("%d",&Q);
  188. while(Q--) {
  189. int op,x,y;
  190. scanf("%d%d%d",&op,&x,&y);
  191. if(op == ) {
  192. updateW(x,y);
  193. }
  194. else if(op == )
  195. {
  196. updateL(x,y);
  197. }
  198. else
  199. {
  200. if(x == y) puts("");
  201. else
  202. printf("%d\n",solve(x,y));
  203. }
  204. }
  205. }
  206. return ;
  207. }

HDU 4897 Little Devil I 树链剖分+线段树的更多相关文章

  1. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

  2. BZOJ3862Little Devil I——树链剖分+线段树

    题目大意: 给一棵树,每条边可能是黑色或白色(起始都是白色),有三种操作: 1.将u到v路径上所有边颜色翻转(黑->白,白->黑) 2.将只有一个点在u到v路径上的边颜色翻转 3.查询u到 ...

  3. HDU4897 (树链剖分+线段树)

    Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...

  4. Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组

    Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...

  5. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  6. 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树

    [BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...

  7. BZOJ2243 (树链剖分+线段树)

    Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...

  8. POJ3237 (树链剖分+线段树)

    Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...

  9. bzoj4034 (树链剖分+线段树)

    Problem T2 (bzoj4034 HAOI2015) 题目大意 给定一颗树,1为根节点,要求支持三种操作. 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子 ...

  10. Aizu 2450 Do use segment tree 树链剖分+线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

随机推荐

  1. 《C++专项练习》 — (2)

    序 C++基础专项练习二,,,水平依然不到家! 错题分析与总结 1 . 有如下模板定义: template <class T> T fun(T x,T y){ return x*x+y*y ...

  2. jQuery.data() 的实现方式

    jQuery.data() 的作用是为普通对象或 DOM Element 附加(及获取)数据. 下面将分三个部分分析其实现方式:     1. 用name和value为对象附加数据:即传入三个参数,第 ...

  3. 3. express 框架使用 vue框架 weiUI

    express 1. 安装 npm install express --save 2. 创建项目 vue js 安装Vuejs vue-cli 是一个官方发布 vue.js 项目脚手架,使用 vue- ...

  4. 用spring annotation声明的bean,当打包在jar中时,无法被扫描到

    发现一个问题,十分蛋疼. 我们项目是由N个工程组成的,外围工程是web工程,内部的工程打包成jar,放入外围工程的WEB-INF/lib 内部的工程用到了spring的注解,例如@Service.@C ...

  5. STM32F407 独立看门狗 个人笔记

    什么是看门狗 如果程序跑飞了怎么办? 可以用看门狗来监控. 看门狗是: 一个递减的计数器,如果不按时给计数器赋值,计数器的值减到一定程度,就会使系统复位. 也就是说如果程序运行异常,无法正常给计数器赋 ...

  6. python +selenium 自带case +生成报告的模板

    https://github.com/huahuijay/python-selenium2这个就是 python +selenium的 里面还自带case 然后也有生成报告的模板 我的: https: ...

  7. Boolean.valueOf("true")的用法

    Boolean.valueOf(a);a为true时返回true不管大小写,a为其他值时都返回false:

  8. python018 Python3 输入和输出

    Python3 输入和输出在前面几个章节中,我们其实已经接触了 Python 的输入输出的功能.本章节我们将具体介绍 Python 的输入输出. 输出格式美化Python两种输出值的方式: 表达式语句 ...

  9. Robberies(01背包)

    The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually g ...

  10. php对象(继承,多态)

    /2.继承//function abc(){// $arr = func_get_args();//}//子类只能有一个父类 一个父类 可以有多个子类//override 重写//overlood 重 ...