Description

给定一棵结点数为n的树,初始点权均为0,有依次q个操作,每次操作有三个参数a,b,c,当a=1时,表示给b号结点到c号结点路径上的所有点(包括b,c,下同)权值都增加1,当a=2时,表示询问b号结点到c号结点路径上的所有点权值之和。

Input

第一行,一个正整数n。

接下来n-1行,每行一对正整数x,y,表示x号结点和y号结点之间有一条边。

第n+1行,一个正整数q。

最后q行,每行一组正整数a,b,c,表示操作的三个参数。b和c可能相等。

保证数据都是合法的。

Output

若干行,每行一个非负整数表示答案。

Sample Input

5

1 2

2 3

1 4

2 5

5

1 4 5

2 1 5

1 1 3

2 5 3

2 4 3

Sample Output

3

4

6

题解:

如题名,数剖练习题。

代码:

  1. var
  2. son,siz,dep,top,w,fa,tt2,c,flag:array[-..]of longint;
  3. tt:array[-..,-..]of longint;
  4. b:array[-..,..]of longint;
  5. i,j,k,l,n,m,totw,q,t:longint;
  6. function cc2(x,l,r:longint):longint;
  7. begin
  8. cc2:=; cc2:=cc2+(r-l+)*tt[x,];
  9. if tt[x,]=tt[x,] then exit;
  10. if r<=(tt[x,]+tt[x,])div then cc2:=cc2+cc2(tt[x,-],l,r)else
  11. if l>(tt[x,]+tt[x,])div then cc2:=cc2+cc2(tt[x,-],l,r)else
  12. begin cc2:=cc2+cc2(tt[x,-],l,(tt[x,]+tt[x,])div );
  13. cc2:=cc2+cc2(tt[x,-],((tt[x,]+tt[x,])div )+,r); end;
  14. end;
  15. procedure cc1(x,l,r:longint);
  16. begin
  17. if(l=tt[x,])and(r=tt[x,])then
  18. begin tt[x,]:=tt[x,]+; exit; end;
  19. if r<=(tt[x,]+tt[x,])div then cc1(tt[x,-],l,r)else
  20. if l>(tt[x,]+tt[x,])div then cc1(tt[x,-],l,r)else
  21. begin cc1(tt[x,-],l,(tt[x,]+tt[x,])div );
  22. cc1(tt[x,-],((tt[x,]+tt[x,])div )+,r); end;
  23. end;
  24. procedure ss(z,x,y:longint);
  25. var i,j,k,l,f1,f2,tot:longint;
  26. begin
  27. f1:=top[x]; f2:=top[y]; tot:=;
  28. while f1<>f2 do
  29. begin
  30. if dep[f1]<dep[f2] then
  31. begin j:=f1; f1:=f2; f2:=j; j:=x; x:=y; y:=j; end;
  32. if z= then cc1(,w[f1],w[x])else
  33. tot:=tot+cc2(,w[f1],w[x]); x:=fa[f1]; f1:=top[x];
  34. end;
  35. if w[x]>w[y] then begin j:=x; x:=y; y:=j; end;
  36. if z= then cc1(,w[x],w[y])else
  37. begin tot:=tot+cc2(,w[x],w[y]); writeln(tot); end;
  38. end;
  39. procedure dfs2(x:longint);
  40. var i,j,k,l:longint;
  41. begin
  42. j:=c[x];
  43. while b[j,]=x do
  44. begin
  45. if b[j,]=son[x] then
  46. begin
  47. flag[b[j,]]:=; top[b[j,]]:=top[x];
  48. inc(totw); w[b[j,]]:=totw; tt2[totw]:=b[j,];
  49. dfs2(b[j,]);
  50. end;
  51. inc(j);
  52. end;
  53. j:=c[x];
  54. while b[j,]=x do
  55. begin
  56. if flag[b[j,]]= then
  57. begin
  58. flag[b[j,]]:=; top[b[j,]]:=b[j,];
  59. inc(totw); w[b[j,]]:=totw; tt2[totw]:=b[j,];
  60. dfs2(b[j,]);
  61. end;
  62. inc(j);
  63. end;
  64. end;
  65. procedure dfs1(x:longint);
  66. var i,j,k,l:longint;
  67. begin
  68. j:=c[x]; siz[x]:=;
  69. while b[j,]=x do
  70. begin
  71. if fa[b[j,]]= then
  72. begin
  73. fa[b[j,]]:=x;
  74. dep[b[j,]]:=dep[x]+;
  75. dfs1(b[j,]); siz[x]:=siz[x]+siz[b[j,]];
  76. if(son[x]=)or(siz[son[x]]<siz[b[j,]])then
  77. son[x]:=b[j,];
  78. end;
  79. inc(j);
  80. end;
  81. end;
  82. procedure sort(l,r:longint);
  83. var
  84. i,j,x,x2:longint;
  85. y:array[..]of longint;
  86. begin
  87. i:=l; j:=r;
  88. x:=b[(l+r)div ,];
  89. x2:=b[(l+r)div ,];
  90. repeat
  91. while(b[i,]<x)or((b[i,]=x)and(b[i,]<x2))do inc(i);
  92. while(x<b[j,])or((b[j,]=x)and(b[j,]>x2))do dec(j);
  93. if not(i>j) then
  94. begin
  95. y:=b[i];
  96. b[i]:=b[j];
  97. b[j]:=y;
  98. inc(i);
  99. j:=j-;
  100. end;
  101. until i>j;
  102. if l<j then sort(l,j);
  103. if i<r then sort(i,r);
  104. end;
  105. procedure make(l,r:longint);
  106. var i,j,k:longint;
  107. begin
  108. inc(t); i:=t; tt[i,]:=l; tt[i,]:=r;
  109. if l<>r then
  110. begin
  111. tt[i,-]:=t+; make(l,(l+r)div );
  112. tt[i,-]:=t+; make(((l+r)div )+,r);
  113. end;
  114. end;
  115. begin
  116. readln(n); m:=(n-)*;
  117. for i:= to n- do
  118. begin
  119. readln(b[i*-,],b[i*-,]);
  120. b[i*,]:=b[i*-,]; b[i*,]:=b[i*-,];
  121. end;
  122. sort(,m); j:=;
  123. for i:= to n do
  124. begin
  125. if j>m then break;
  126. if b[j,]>i then continue;
  127. c[i]:=j; while b[j,]=i do inc(j);
  128. end;
  129. dep[]:=; fa[]:=-; dfs1();
  130. totw:=; top[]:=; tt2[]:=; w[]:=; flag[]:=; dfs2();
  131. t:=; make(,n);
  132. readln(q);
  133. for i:= to q do
  134. begin
  135. readln(j,k,l);
  136. ss(j,k,l);
  137. end;
  138. end.

CODE[VS]4633:Mz树链剖分练习的更多相关文章

  1. Codevs 4633 [Mz]树链剖分练习

    4633 [Mz]树链剖分练习 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 大师 Master 题目描述 Description 给定一棵结点数为n的树,初始点权均为0,有依次q个 ...

  2. [CodeVS4633][Mz]树链剖分练习

    思路: 轻重链剖分+线段树. #include<cstdio> #include<vector> #include<cstring> ; std::vector&l ...

  3. 2020牛客NOIP赛前集训营-提高组(第三场) C - 牛半仙的妹子Tree (树链剖分)

    昨天教练问我:你用树剖做这道题,怎么全部清空状态呢?    我:???不是懒标记就完了???    教练:树剖不是要建很多棵线段树吗,不止log个,你要一个一个清?    我:为什么要建很多棵线段树? ...

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

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

  5. 【BZOJ-3553】三叉神经树 树链剖分

    3553: [Shoi2014]三叉神经树 Time Limit: 160 Sec  Memory Limit: 256 MBSubmit: 347  Solved: 112[Submit][Stat ...

  6. 【BZOJ-2836】魔法树 树链剖分

    2836: 魔法树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 323  Solved: 129[Submit][Status][Discuss] ...

  7. 【BZOJ-4353】Play with tree 树链剖分

    4353: Play with tree Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 31  Solved: 19[Submit][Status][ ...

  8. 【BZOJ-4568】幸运数字 树链剖分 + 线性基合并

    4568: [Scoi2016]幸运数字 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 238  Solved: 113[Submit][Status ...

  9. 【BZOJ-4127】Abs 树链剖分 + 线段树 (有趣的姿势)

    4127: Abs Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 381  Solved: 132[Submit][Status][Discuss] ...

随机推荐

  1. 【未完成】Jmeter接口自动化测试:参数化设置

    1. 从CSV文件读取参数 创建一个CVS文件,文件第一行不写参数名,直接从参数值开始,每一列代表一个参数 在测试计划或者线程组中,添加一个配置元件-->CSV 数据文件设置 Filename: ...

  2. 各种版本mysql驱动包下载地址

    http://central.maven.org/maven2/mysql/mysql-connector-java/

  3. Django杂篇(1)

    目录 Django杂篇(1) bulk_create Pagination 创建多对多表关系的常用方法 form校验组件的应用 渲染页面 展示错误信息 校验数据 常用字段 Django杂篇(1) 这里 ...

  4. 自动生成DTO(Sugar框架)

    step1:启动api项目 step2:使用postman工具,填上接口地址http://localhost:7788/api/automapper/AutoMapperSuper step3:表格数 ...

  5. printk函数

    一个不同是 printk 允许你根据消息的严重程度对其分类, 通过附加不同的记录级别或者 优先级在消息上. 你常常用一个宏定义来指示记录级别. 例如, KERN_INFO, 我们之前曾 在一些打印语句 ...

  6. 移植 inetd

    inetd 的选择及获取 Busybox1.1.3 提供了 inetd 支持.如果读者使用的是较低版本的不提供 inetd 的 Busybox,那么可以考虑使 用 netkit 套件来提供网络服务.强 ...

  7. 1 环境搭建_及参考资料介绍_ASM驱动开发疑难解决

    1 环境搭建 > 这里和  周壑老师不同:大家觉得哪中适合当前的物理机,就配置那种. 如下: win7 32 位: vs 2010; visualDDk 驱动项目插件: V A --- 代码高亮 ...

  8. YXcms前台注入(有限制但可以绕过)

    这个cms很久前做过代码审计,很多问题,但是经过这么长时间,现在安全性提高了不少,这几天看了下,基本没有什么特别大的问题了(不包含后台). 在yxcms/protected/apps/member/c ...

  9. Android开发 MediaPlayer将视频播放时尺寸适配完美

    前言 视频播放有一个较为蛋疼的问题,那就是尺寸适配.如果不做尺寸适配视频将会变形拉伸或者压缩.下面我就介绍个人实现的算法. 满足一边的算法 满足一边?你可能是疑问是什么意思.意思是就是始终将视频的高度 ...

  10. 2-sat——输出方案poj3683

    一篇讲的详细的博客 https://blog.csdn.net/Hawo11/article/details/74908233 缩点后为什么要建立反图? 如果是按原图处理,选择一个点之后要把所有其后续 ...