题意:

思路:分块

使用树状数组维护sum[i]的前缀和

使用主席树维护root到u的路径上点的编号出现的个数

每次操作如果是修改就加入队列

如果是询问,考虑块内操作对询问的影响,每次在x点加上y会使x到root的点sum都加上y

每根号n次操作就暴力重构一次,清空队列并求出新的sum[i]的前缀和

  1. var t:array[..]of record
  2. l,r:longint;
  3. s:int64;
  4. end;
  5. sum,bit:array[..]of int64;
  6. stk:array[..,..]of longint;
  7. head,vet,next,fa,a:array[..]of longint;
  8. root:array[..]of longint;
  9. n,m,x,y,i,j,top,tot,cnt,kuai,rt,op:longint;
  10. ans,tmp:int64;
  11.  
  12. procedure add(a,b:longint);
  13. begin
  14. inc(tot);
  15. next[tot]:=head[a];
  16. vet[tot]:=b;
  17. head[a]:=tot;
  18. end;
  19.  
  20. procedure pushup(p:longint);
  21. begin
  22. t[p].s:=t[t[p].l].s+t[t[p].r].s;
  23. end;
  24.  
  25. procedure update(l,r,x:longint;var p:longint);
  26. var mid:longint;
  27. begin
  28. inc(cnt); t[cnt]:=t[p];
  29. p:=cnt; inc(t[p].s);
  30. if l=r then exit;
  31. mid:=(l+r)>>;
  32. if x<=mid then update(l,mid,x,t[p].l)
  33. else update(mid+,r,x,t[p].r);
  34. pushup(p);
  35. end;
  36.  
  37. function query(l,r,x,y,p:longint):int64;
  38. var mid:longint;
  39. begin
  40. if (l>=x)and(r<=y) then exit(t[p].s);
  41. mid:=(l+r)>>;
  42. query:=;
  43. if x<=mid then query:=query+query(l,mid,x,y,t[p].l);
  44. if y>mid then query:=query+query(mid+,r,x,y,t[p].r);
  45. pushup(p);
  46. end;
  47.  
  48. procedure dfs1(u:longint);
  49. var e,v:longint;
  50. begin
  51. root[u]:=root[fa[u]];
  52. update(,n,u,root[u]);
  53. e:=head[u];
  54. while e<> do
  55. begin
  56. v:=vet[e];
  57. if v<>fa[u] then
  58. begin
  59. fa[v]:=u;
  60. dfs1(v);
  61. end;
  62. e:=next[e];
  63. end;
  64. end;
  65.  
  66. procedure dfs2(u:longint);
  67. var e,v:longint;
  68. begin
  69. sum[u]:=a[u];
  70. e:=head[u];
  71. while e<> do
  72. begin
  73. v:=vet[e];
  74. if v<>fa[u] then
  75. begin
  76. dfs2(v);
  77. sum[u]:=sum[u]+sum[v];
  78. end;
  79. e:=next[e];
  80. end;
  81. end;
  82.  
  83. function lowbit(x:longint):longint;
  84. begin
  85. exit(x and (-x));
  86. end;
  87.  
  88. procedure addbit(x:longint;y:int64);
  89. begin
  90. while x<=n do
  91. begin
  92. bit[x]:=bit[x]+y;
  93. x:=x+lowbit(x);
  94. end;
  95. end;
  96.  
  97. function querybit(x,y:longint):int64;
  98. var k:longint;
  99. begin
  100. k:=y;
  101. querybit:=;
  102. while k> do
  103. begin
  104. querybit:=querybit+bit[k];
  105. k:=k-lowbit(k);
  106. end;
  107. k:=x-;
  108. while k> do
  109. begin
  110. querybit:=querybit-bit[k];
  111. k:=k-lowbit(k);
  112. end;
  113. end;
  114.  
  115. procedure build;
  116. var i:longint;
  117. begin
  118. for i:= to n do bit[i]:=;
  119. for i:= to n do addbit(i,sum[i]);
  120. end;
  121.  
  122. begin
  123. assign(input,'common.in'); reset(input);
  124. assign(output,'common.out'); rewrite(output);
  125. readln(n,m);
  126. kuai:=;
  127. for i:= to n do read(a[i]);
  128. for i:= to n do
  129. begin
  130. readln(x,y);
  131. if x= then rt:=y
  132. else
  133. begin
  134. add(x,y);
  135. add(y,x);
  136. end;
  137. end;
  138. dfs1(rt);
  139. fillchar(head,sizeof(head),);
  140. tot:=;
  141. for i:= to n do
  142. if i<>rt then add(fa[i],i);
  143. dfs2(rt);
  144. build;
  145.  
  146. for i:= to m do
  147. begin
  148. readln(op,x,y);
  149. if op= then
  150. begin
  151. inc(top); stk[top,]:=x; stk[top,]:=y-a[x];
  152. a[x]:=y;
  153. end
  154. else
  155. begin
  156. ans:=querybit(x,y);
  157. for j:= to top do
  158. begin
  159. tmp:=query(,n,x,y,root[stk[j,]]);
  160. ans:=ans+stk[j,]*tmp;
  161. end;
  162. writeln(ans);
  163. end;
  164. if i mod kuai= then
  165. begin
  166. top:=;
  167. dfs2(rt);
  168. build;
  169. end;
  170. end;
  171. close(input);
  172. close(output);
  173. end.

【ZJOI2017 Round1练习&BZOJ4765】D1T3 普通计算姬(主席树,分块)的更多相关文章

  1. bzoj 4765: 普通计算姬 主席树+替罪羊树思想

    题目大意: 给定一棵\(n\)个节点的带权树有根树,设\(sum_p\)表示以点\(p\)为根的这棵子树中所有节点的权 计算姬支持下列两种操作: 给定两个整数\(u,v\),修改点\(u\)的权值为\ ...

  2. 【bzoj4765】普通计算姬(双重分块)

    题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4765 这道题已经攒了半年多了...因为懒,一直没去写...所以今天才把这道题写出来... ...

  3. BZOJ4766:文艺计算姬(矩阵树定理)

    Description "奋战三星期,造台计算机".小W响应号召,花了三星期造了台文艺计算姬.文艺计算姬比普通计算机有更多的艺术细胞. 普通计算机能计算一个带标号完全图的生成树个数 ...

  4. bzoj 4765 普通计算姬 dfs序 + 分块

    题目链接 Description "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些.普通计算机能计算数列区间和,而普通计算姬能 ...

  5. [bzoj4766] 文艺计算姬 (矩阵树定理+二分图)

    传送门 Description "奋战三星期,造台计算机".小W响应号召,花了三星期造了台文艺计算姬.文艺计算姬比普通计算机有更多的艺 术细胞.普通计算机能计算一个带标号完全图的生 ...

  6. 【bzoj4765】 普通计算姬

    题意 给出一棵有根树,$n$个点每个都有一个点权.$m$组操作每次可以修改一个点权或者询问编号在区间$[l,r]$的点的子树权值和的和. Solution 我们对节点编号分块,每一块统计该块中的节点的 ...

  7. 【bzoj4765】普通计算姬

    一道奇奇怪怪的数据结构题? 把树线性化,然后分块维护吧. 为了加速,求和用树状数组维护每个块的值. #include<bits/stdc++.h> #define N 100010 #de ...

  8. BZOJ 4765 普通计算姬 dfs序+分块+树状数组+好题!!!

    真是道好题...感到灵魂的升华... 按dfs序建树状数组,拿前缀和去求解散块: 按点的标号分块,分成一个个区间,记录区间子树和 的 总和... 具体地,需要记录每个点u修改后,对每一个块i的贡献,记 ...

  9. bzoj 4766: 文艺计算姬 矩阵树定理

    题目: 给定一个一边点数为\(n\),另一边点数为\(m\),共有\(n*m\)条边的带标号完全二分图\(K_{n,m}\) 计算其生成树个数 \(n,m,p \leq 10^{18} ,p为模数\) ...

随机推荐

  1. AJPFX关于Java内部类及其实例化

    public class Outer {    private int size;    public class Inner {        private int counter = 10;  ...

  2. myeclipse 安装svn(subeclipsesite)插件

    (1)到官网下载subeclipsesite,下载最新的版本:http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=224 ...

  3. ZooKeeper读书笔记

    <ZooKeeper读书笔记> 1.Zookeeper是什么?Zookeeper是一个典型的分布式数据一致性的解决方案,分布式应用可以基于它实现诸如数据发布/订阅.负载均衡.命名服务.分布 ...

  4. innerHTML与IE浏览器内存泄露问题

    使用 sIEve 扫描和筛选 如果大量使用 JavaScript 和 Ajax 技术开发 Web 2.0 应用程序,您很有可能会遇到浏览器的内存泄漏问题.如果您有一个单页应用程序或者一个页面要处理很多 ...

  5. 洛谷 P1351 联合权值

    题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...

  6. PHP一句话后门过狗姿势万千之传输层加工

    既然木马已就绪,那么想要利用木马,必然有一个数据传输的过程,数据提交是必须的,数据返回一般也会有的,除非执行特殊命令. 当我们用普通菜刀连接后门时,数据时如何提交的,狗狗又是如何识别的,下面结合一个实 ...

  7. SAS Fuctions

    1. monotonic(), 单调递增函数.返回一列变量的序列等,类似于_N_ . 2. index v indexw: INDEX Function Searches a character ex ...

  8. zabbix利用微信报警

    一.背景介绍 报警的方式有很多种,比如常用的邮件报警,短信,微信等.那这篇文章主要是实现微信报警. 值得注意的是,之前使用微信企业公众号,现在微信企业公众号更新成企业微信了.所以直接注册企业微信,进入 ...

  9. python基础一 day2

    内容:   3%%s   输出:3%s       后面的全部转义 结果: 如果是因为执行break语句导致循环提前结束,就不会执行else. 单位换算: 编码方式: ascii  unicode u ...

  10. [LUOGU] P3469 [POI2008]BLO-Blockade

    https://www.luogu.org/problemnew/show/P3469 求无向图分别删去每个点后不连通的点对数. 首先,对于任何一个点,它本身删了,就会和剩下的n-1个点不连通,点对是 ...