CODE[VS]4633:Mz树链剖分练习
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]4633:Mz树链剖分练习的更多相关文章
- Codevs 4633 [Mz]树链剖分练习
4633 [Mz]树链剖分练习 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 大师 Master 题目描述 Description 给定一棵结点数为n的树,初始点权均为0,有依次q个 ...
- [CodeVS4633][Mz]树链剖分练习
思路: 轻重链剖分+线段树. #include<cstdio> #include<vector> #include<cstring> ; std::vector&l ...
- 2020牛客NOIP赛前集训营-提高组(第三场) C - 牛半仙的妹子Tree (树链剖分)
昨天教练问我:你用树剖做这道题,怎么全部清空状态呢? 我:???不是懒标记就完了??? 教练:树剖不是要建很多棵线段树吗,不止log个,你要一个一个清? 我:为什么要建很多棵线段树? ...
- 【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] ...
随机推荐
- Windows 设置内网和外网同时使用
想要电脑同时使用内网和外网必须具备两个网卡,一个是无线网卡一个是本地连接,无线网卡用来连接wifi也就是外网,而本地连接需要网线连接内网,外网是不需要做设置的,我们只需要设置内网即可,鼠标右击电脑右下 ...
- 网站统计中的PV-UV-IP的定义与区别
--------首先来看看ip.uv和pv的定义---------- PV(访问量):即Page View, 即页面浏览量或点击量,用户每次刷新即被计算一次.UV(独立访客):即Unique Visi ...
- System.Web.Mvc.IAuthorizationFilter.cs
ylbtech-System.Web.Mvc.IAuthorizationFilter.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutra ...
- day22_3-json模块
# 参考资料:# python模块(转自Yuan先生) - 狂奔__蜗牛 - 博客园# https://www.cnblogs.com/guojintao/articles/9070485.html# ...
- python语句结构(while循环)
while循环 pythhon中while语句的一般形式 while 判断语句: 执行语句 i=0 sum=0 while i<=100: sum+=i i=i+1 print(sum) #运行 ...
- Matlab AlexNet 识别花
1. 首先,你要又并行计算的工具箱,在插件选项里面找到,安装即可 2. 下载训练的数据集,采用matlab演示的材料即可 https://matlabacademy-content.mathworks ...
- BOM相关知识点
1.BOM概念:Browser Object Model 浏览器对象模型作用:提供了使用JS操作浏览器的接口 2.BOM包含了许多对象信息,包括如下这些:(1)screen 屏幕信息(2)locati ...
- vue+ivew使用Collapse 折叠面板把全部面板展开
1.需求: 在使用搜索功能时候,只显示搜索到的panel并且将搜索到的含有该专家的panel展开,如图 1.html,注意黄色部分,作为每个panel的key值,要唯一 ...
- LoadRunner脚本编写(5)-- 检查点,关联等函数
LoadRunner脚本编写(5)-- 检查点,关联等函数 http://www.51testing.com/?34866/action_viewspace_itemid_70224.html来继续翻 ...
- HTML - 文本标签相关
<html> <head></head> <body> <!-- 标题标签 : h1到h6, 文字大小依次变小, 加粗显示, 自带换行 标签中的部 ...