树链剖分在边上的应用
比维护点稍微麻烦一点,是对每条边标号,并且要记录每个点父亲边的编号和重儿子
然后注意各种细节
线段树上和bzoj1858的维护方法类似,覆盖的优先级高于加
具体见程序,完全是为了提升状态的练习

 type node=record
po,next,num:longint;
end; var son,a,d,fa,fp,f2,b,c,p,size,top:array[..] of longint;
w:array[..] of node;
tree,laz1,laz2:array[..*] of longint;
x,y,z,i,n,t:longint;
ch:char;
s:string; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; procedure add(x,y,c:longint);
begin
inc(t);
w[t].po:=y;
w[t].num:=c;
w[t].next:=p[x];
p[x]:=t;
end; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; procedure push(i:longint);
begin
if laz1[i]<>- then
begin
tree[i*]:=laz1[i];
tree[i*+]:=laz1[i];
laz1[i*]:=laz1[i];
laz1[i*+]:=laz1[i];
laz1[i]:=-;
laz2[i]:=;
end;
if laz2[i]<> then
begin
inc(tree[i*],laz2[i]);
inc(tree[i*+],laz2[i]);
if laz1[i*]<>- then inc(laz1[i*],laz2[i])
else inc(laz2[i*],laz2[i]);
if laz1[i*+]<>- then inc(laz1[i*+],laz2[i])
else inc(laz2[i*+],laz2[i]);
laz2[i]:=;
end;
end; procedure dfs1(x:longint);
var i,y:longint;
begin
i:=p[x];
size[x]:=;
while i<> do
begin
y:=w[i].po;
if fa[x]<>y then
begin
fp[y]:=w[i].num;
fa[y]:=x;
d[y]:=d[x]+;
dfs1(y);
size[x]:=size[x]+size[y];
end;
i:=w[i].next;
end;
end; procedure dfs2(x:longint);
var i,y,q:longint;
begin
i:=p[x];
q:=;
while i<> do
begin
if fa[w[i].po]=x then
if size[w[q].po]<size[w[i].po] then q:=i;
i:=w[i].next;
end;
if q= then exit;
top[w[q].po]:=top[x];
son[x]:=w[q].num;
inc(t);
c[t]:=w[q].num;
b[w[q].num]:=t;
dfs2(w[q].po);
i:=p[x];
while i<> do
begin
y:=w[i].po;
if (i<>q) and (fa[y]=x) then
begin
inc(t);
c[t]:=w[i].num;
b[w[i].num]:=t;
top[y]:=y;
dfs2(y);
end;
i:=w[i].next;
end;
end; procedure build(i,l,r:longint);
var m:longint;
begin
laz1[i]:=-;
laz2[i]:=;
if l=r then tree[i]:=a[c[l]]
else begin
m:=(l+r) shr ;
build(i*,l,m);
build(i*+,m+,r);
tree[i]:=max(tree[i*],tree[i*+]);
end;
end; procedure change(i,l,r,x,y,z:longint);
var m:longint;
begin
if x>y then exit;
if (x<=l) and (y>=r) then
begin
laz1[i]:=z;
tree[i]:=z;
laz2[i]:=;
end
else begin
push(i);
m:=(l+r) shr ;
if x<=m then change(i*,l,m,x,y,z);
if y>m then change(i*+,m+,r,x,y,z);
tree[i]:=max(tree[i*],tree[i*+]);
end;
end; procedure fadd(i,l,r,x,y:longint);
var m:longint;
begin
if x>y then exit;
if (x<=l) and (y>=r) then
begin
if laz1[i]<>- then inc(laz1[i],z)
else inc(laz2[i],z);
inc(tree[i],z);
end
else begin
push(i);
m:=(l+r) shr ;
if x<=m then fadd(i*,l,m,x,y);
if y>m then fadd(i*+,m+,r,x,y);
tree[i]:=max(tree[i*],tree[i*+]);
end;
end; function getans(i,l,r,x,y:longint):longint;
var m,s:longint;
begin
if x>y then exit();
if (x<=l) and (y>=r) then exit(tree[i])
else begin
push(i);
m:=(l+r) shr ;
s:=;
if x<=m then s:=getans(i*,l,m,x,y);
if y>m then s:=max(s,getans(i*+,m+,r,x,y));
exit(s);
end;
end; procedure cover(x,y:longint);
var f1,f2:longint;
begin
f1:=top[x];
f2:=top[y];
while f1<>f2 do
begin
if d[f1]>=d[f2] then
begin
change(,,n-,b[fp[f1]],b[fp[x]],z);
x:=fa[f1];
end
else begin
change(,,n-,b[fp[f2]],b[fp[y]],z);
y:=fa[f2];
end;
f1:=top[x];
f2:=top[y];
end;
if x=y then exit
else if b[fp[x]]>b[fp[y]] then swap(x,y);
change(,,n-,b[son[x]],b[fp[y]],z);
end; procedure add(x,y:longint);
var f1,f2:longint;
begin
f1:=top[x];
f2:=top[y];
while f1<>f2 do
begin
if d[f1]>=d[f2] then
begin
fadd(,,n-,b[fp[f1]],b[fp[x]]);
x:=fa[f1];
end
else begin
fadd(,,n-,b[fp[f2]],b[fp[y]]);
y:=fa[f2];
end;
f1:=top[x];
f2:=top[y];
end;
if x=y then exit
else if b[fp[x]]>b[fp[y]] then swap(x,y);
fadd(,,n-,b[son[x]],b[fp[y]]);
end; function ask(x,y:longint):longint;
var s,f1,f2:longint;
begin
s:=;
f1:=top[x];
f2:=top[y];
while f1<>f2 do
begin
if d[f1]>=d[f2] then
begin
s:=max(s,getans(,,n-,b[fp[f1]],b[fp[x]]));
x:=fa[f1];
end
else begin
s:=max(s,getans(,,n-,b[fp[f2]],b[fp[y]]));
y:=fa[f2];
end;
f1:=top[x];
f2:=top[y];
end;
if x=y then exit(s)
else if b[fp[x]]>b[fp[y]] then swap(x,y);
s:=max(s,getans(,,n-,b[son[x]],b[fp[y]]));
exit(s);
end; begin
readln(n);
for i:= to n- do
begin
readln(x,y,a[i]);
add(x,y,i);
add(y,x,i);
end;
t:=;
dfs1();
top[]:=;
dfs2();
while true do
begin
read(ch);
s:='';
while ch<>' ' do
begin
s:=s+ch;
if s='Stop' then halt;
read(ch);
end;
read(x,y);
if s='Max' then
writeln(ask(x,y))
else if s='Change' then
change(,,n-,b[x],b[x],y)
else if s='Cover' then
begin
read(z);
cover(x,y);
end
else if s='Add' then
begin
read(z);
add(x,y);
end;
readln;
end;
end.

bzoj1984的更多相关文章

  1. 【BZOJ1984】月下“毛景树” 树链剖分+线段树

    [BZOJ1984]月下"毛景树" Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校 ...

  2. [BZOJ1984]月下“毛景树”解题报告|树链剖分

    Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校园里.爬啊爬~爬啊爬~~毛毛虫爬到了一颗小小的“毛景树” ...

  3. 【树链剖分】【分块】【最近公共祖先】【块状树】bzoj1984 月下“毛景树”

    裸题,但是因为权在边上,所以要先把边权放到这条边的子节点上,然后进行链更新/查询的时候不能更新/查询其lca. #include<cstdio> #include<cmath> ...

  4. [bzoj1984]月下“毛景树”

    Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园.毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校园里.爬啊爬~爬啊爬~~毛毛虫爬到了一颗小小的" ...

  5. 【BZOJ-1984】月下“毛景树” 树链剖分

    1984: 月下“毛景树” Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 1314  Solved: 416[Submit][Status][Discu ...

  6. BZOJ1984: 月下“毛景树”

    1984: 月下“毛景树” Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 713  Solved: 245[Submit][Status] Descri ...

  7. [BZOJ1984][Luogu4315]月下“毛景树”

    题目大意 给出一棵 n 个点的无根树,待边权,要求维护一下操作: 修改某条边的边权 修改点 u 到点 v 路径上所有边的边权 点 u 到点 v 路径上所有边的边权加上某个值 查询点 u 到点 v 路径 ...

  8. 树剖+线段树||树链剖分||BZOJ1984||Luogu4315||月下“毛景树”

    题面:月下“毛景树” 题解:是道很裸的树剖,但处理的细节有点多(其实是自己线段树没学好).用一个Dfs把边权下移到点权,用E数组记录哪些边被用到了:前三个更新的操作都可以合并起来,可以发现a到b节点间 ...

  9. 2018.10.27 bzoj1984: 月下“毛景树”(树链剖分)

    传送门 唉蒟蒻又退化了,这道sb题居然做了20min,最后发现是updcovupdcovupdcov写成了updaddupdaddupdadd我还能说什么233233233 就是让你转边权为点权之后, ...

随机推荐

  1. 【转】【SQLServer】SQL Server 2008“阻止保存要求重新创建表的更改”

    不是很理解为什么在2008中会加入阻止保存要求重新创建表的更改这个选项.症状表现为修改表结构的时候会"阻止"你.而且我遇到的情况是居然有的时候阻止你,有的时候不阻止你,摸不到头脑. ...

  2. java新手笔记5 类

    1.进制转换 /* 企业发放的奖金根据利润提成. 利润(I)低于或等于10万元时,奖金可提10%: 利润高于10万元,低于20万元时, 低于10万元的部分按10%提成,高于10万元的部分,可提成7.5 ...

  3. 加载jar文件输出class和method

    package file import java.util.jar.JarEntry import java.util.jar.JarFile import org.junit.Test; class ...

  4. 08_一个java.lang.NullPointException报错

    [报错代码] public class UserDaoTest{ private SqlSessionFactory sqlSessionFactory; //此方法在执行testFindUserBy ...

  5. UVA 1401 Remember the Word(用Trie加速动态规划)

    Remember the Word Neal is very curious about combinatorial problems, and now here comes a problem ab ...

  6. 配置iSCSI

    先查下yum list | grep iscsi, 存在iscsi包, 进行安装:yum install iscsi-initiator-utils.x86_64, cat /etc/iscsi/in ...

  7. 关于C++几个容易混淆的概念总结

    1.函数指针和指针函数 函数指针说的就是一个指针,但这个指针指向的函数,不是普通的基本数据类型或者类对象.函数指针的定义: int (*func)(int a, int b); // 声明函数指针 指 ...

  8. (转)UIColor 的使用

    os开发-UIColor的使用. 在ios开发中,经常遇到对UIColor的相关操作. 比如这样 self.backgroundColor = [UIColorredColor]; 这里的redCol ...

  9. Web Service 的服务端的引用

    1.先说说服务端的引用 先写一个Web Service 的文件  上图 创建一个web 项目或者网站  然后添加新项 创建一个web服务 得到 下面的页面 然后运行起来 然后复制下地址 接下来创建另一 ...

  10. phpredis

    安装php的redis扩展: http://pecl.php.net/package/redis 也可以用PHP直接连redis: http://www.cnblogs.com/kudosharry/ ...