CODE[VS]4633Mz树链剖分练习
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
题解:
如题名,数剖练习题。
代码:
var
son,siz,dep,top,w,fa,tt2,c,flag:array[-..]of longint;
tt:array[-..,-..]of longint;
b:array[-..,..]of longint;
i,j,k,l,n,m,totw,q,t:longint;
function cc2(x,l,r:longint):longint;
begin
cc2:=; cc2:=cc2+(r-l+)*tt[x,];
if tt[x,]=tt[x,] then exit;
if r<=(tt[x,]+tt[x,])div then cc2:=cc2+cc2(tt[x,-],l,r)else
if l>(tt[x,]+tt[x,])div then cc2:=cc2+cc2(tt[x,-],l,r)else
begin cc2:=cc2+cc2(tt[x,-],l,(tt[x,]+tt[x,])div );
cc2:=cc2+cc2(tt[x,-],((tt[x,]+tt[x,])div )+,r); end;
end;
procedure cc1(x,l,r:longint);
begin
if(l=tt[x,])and(r=tt[x,])then
begin tt[x,]:=tt[x,]+; exit; end;
if r<=(tt[x,]+tt[x,])div then cc1(tt[x,-],l,r)else
if l>(tt[x,]+tt[x,])div then cc1(tt[x,-],l,r)else
begin cc1(tt[x,-],l,(tt[x,]+tt[x,])div );
cc1(tt[x,-],((tt[x,]+tt[x,])div )+,r); end;
end;
procedure ss(z,x,y:longint);
var i,j,k,l,f1,f2,tot:longint;
begin
f1:=top[x]; f2:=top[y]; tot:=;
while f1<>f2 do
begin
if dep[f1]<dep[f2] then
begin j:=f1; f1:=f2; f2:=j; j:=x; x:=y; y:=j; end;
if z= then cc1(,w[f1],w[x])else
tot:=tot+cc2(,w[f1],w[x]); x:=fa[f1]; f1:=top[x];
end;
if w[x]>w[y] then begin j:=x; x:=y; y:=j; end;
if z= then cc1(,w[x],w[y])else
begin tot:=tot+cc2(,w[x],w[y]); writeln(tot); end;
end;
procedure dfs2(x:longint);
var i,j,k,l:longint;
begin
j:=c[x];
while b[j,]=x do
begin
if b[j,]=son[x] then
begin
flag[b[j,]]:=; top[b[j,]]:=top[x];
inc(totw); w[b[j,]]:=totw; tt2[totw]:=b[j,];
dfs2(b[j,]);
end;
inc(j);
end;
j:=c[x];
while b[j,]=x do
begin
if flag[b[j,]]= then
begin
flag[b[j,]]:=; top[b[j,]]:=b[j,];
inc(totw); w[b[j,]]:=totw; tt2[totw]:=b[j,];
dfs2(b[j,]);
end;
inc(j);
end;
end;
procedure dfs1(x:longint);
var i,j,k,l:longint;
begin
j:=c[x]; siz[x]:=;
while b[j,]=x do
begin
if fa[b[j,]]= then
begin
fa[b[j,]]:=x;
dep[b[j,]]:=dep[x]+;
dfs1(b[j,]); siz[x]:=siz[x]+siz[b[j,]];
if(son[x]=)or(siz[son[x]]<siz[b[j,]])then
son[x]:=b[j,];
end;
inc(j);
end;
end;
procedure sort(l,r:longint);
var
i,j,x,x2:longint;
y:array[..]of longint;
begin
i:=l; j:=r;
x:=b[(l+r)div ,];
x2:=b[(l+r)div ,];
repeat
while(b[i,]<x)or((b[i,]=x)and(b[i,]<x2))do inc(i);
while(x<b[j,])or((b[j,]=x)and(b[j,]>x2))do dec(j);
if not(i>j) then
begin
y:=b[i];
b[i]:=b[j];
b[j]:=y;
inc(i);
j:=j-;
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end;
procedure make(l,r:longint);
var i,j,k:longint;
begin
inc(t); i:=t; tt[i,]:=l; tt[i,]:=r;
if l<>r then
begin
tt[i,-]:=t+; make(l,(l+r)div );
tt[i,-]:=t+; make(((l+r)div )+,r);
end;
end;
begin
readln(n); m:=(n-)*;
for i:= to n- do
begin
readln(b[i*-,],b[i*-,]);
b[i*,]:=b[i*-,]; b[i*,]:=b[i*-,];
end;
sort(,m); j:=;
for i:= to n do
begin
if j>m then break;
if b[j,]>i then continue;
c[i]:=j; while b[j,]=i do inc(j);
end;
dep[]:=; fa[]:=-; dfs1();
totw:=; top[]:=; tt2[]:=; w[]:=; flag[]:=; dfs2();
t:=; make(,n);
readln(q);
for i:= to q do
begin
readln(j,k,l);
ss(j,k,l);
end;
end.
CODE[VS]4633Mz树链剖分练习的更多相关文章
- 【BZOJ-2325】道馆之战 树链剖分 + 线段树
2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1153 Solved: 421[Submit][Statu ...
- 【BZOJ-3553】三叉神经树 树链剖分
3553: [Shoi2014]三叉神经树 Time Limit: 160 Sec Memory Limit: 256 MBSubmit: 347 Solved: 112[Submit][Stat ...
- 【BZOJ-2836】魔法树 树链剖分
2836: 魔法树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 323 Solved: 129[Submit][Status][Discuss] ...
- 【BZOJ-4353】Play with tree 树链剖分
4353: Play with tree Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 31 Solved: 19[Submit][Status][ ...
- 【BZOJ-4568】幸运数字 树链剖分 + 线性基合并
4568: [Scoi2016]幸运数字 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 238 Solved: 113[Submit][Status ...
- 【BZOJ-4127】Abs 树链剖分 + 线段树 (有趣的姿势)
4127: Abs Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 381 Solved: 132[Submit][Status][Discuss] ...
- 【BZOJ-4515】游戏 李超线段树 + 树链剖分 + 半平面交
4515: [Sdoi2016]游戏 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 304 Solved: 129[Submit][Status][ ...
- 【BZOJ-3589】动态树 树链剖分 + 线段树 + 线段覆盖(特殊的技巧)
3589: 动态树 Time Limit: 30 Sec Memory Limit: 1024 MBSubmit: 405 Solved: 137[Submit][Status][Discuss] ...
- NOIP 2013 货车运输【Kruskal + 树链剖分 + 线段树 】【倍增】
NOIP 2013 货车运输[树链剖分] 树链剖分 题目描述 Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在 ...
随机推荐
- 【原创经验分享】JQuery(Ajax)调用WCF服务
最近在学习这个WCF,由于刚开始学 不久,发现网上的一些WCF教程都比较简单,感觉功能跟WebService没什么特别大的区别,但是看网上的介绍,就说WCF比WebService牛逼多少多少,反正我刚 ...
- 【基于WPF+OneNote+Oracle的中文图片识别系统阶段总结】之篇二:基于OneNote难点突破和批量识别
篇一:WPF常用知识以及本项目设计总结:http://www.cnblogs.com/baiboy/p/wpf.html 篇二:基于OneNote难点突破和批量识别:http://www.cnblog ...
- 你真的会玩SQL吗?玩爆你的数据报表之存储过程编写(上)
你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...
- 黄聪:如何给wordpress的编辑器添加一个自定义按钮,并且实现插入功能
1.添加按钮 在 functions.php 文件里面添加下面代码: add_action('media_buttons', 'add_my_media_button'); function ad ...
- 【DDD/CQRS/微服务架构案例】在Ubuntu 14.04.4 LTS中运行WeText项目的服务端
在<WeText项目:一个基于.NET实现的DDD.CQRS与微服务架构的演示案例>文章中,我介绍了自己用Visual Studio 2015(C# 6.0 with .NET Frame ...
- 还是俄罗斯方块之android版
前面的,口水话 请直接跳过. 虽然现在不比以前了 也没多少人气了,放到首页 都不到几百的点击量.也许博客园整体水平也是在往水的方向发展.不谈那些了,哥也曾经辉煌过 有过一天上千的点击量 ,哥也曾经有过 ...
- 一次页面从Jq到Vuejs+PartialView的迁徙
题外话 本篇分享不能帮助你入门vue,入门的文章也是无意义的,官方文档http://cn.vuejs.org/v2/guide/ 已经写的不能再清晰了.希望我们勇敢的主动地给自己创造实践的机会. 手里 ...
- Redis命令拾遗一(字符串类型)
文章归博客园和作者“蜗牛”共同所有 .转载和爬虫请注明原文Redis系列链接 http://www.cnblogs.com/tdws/tag/NoSql/ Redis有五种基本数据类型.他们分别是字符 ...
- 301和302 Http状态有啥区别?
301和302 Http状态有啥区别? 301,302 都是HTTP状态的编码,都代表着某个URL发生了转移,不同之处在于: 301 redirect: 301 代表永久性转移(Permanently ...
- 有了大量微信用户,就不需要App了吗?
小卢同学是我半年前在中关村车库咖啡认识的一个自由创业者,他从北航毕业后在一家IT上市公司只上了1年多的班就辞职创业了,他的创业项目属于国内度假旅游垂直细分领域:积累大量详细的旅游攻略,组成一个个温馨舒 ...