bzoj2631 3282
这两题都是link cut tree的裸题
之前看Qtree的论文,只会在确定父子关系的情况下连边和删边
如果在任意两个点连边删边怎么做呢?
这时候我们不能随意的将一个点的父节点设为另一个点,因为其中某个点的父节点可能已经被设为另外某点了
其实很简单,连边的时候,我们只要把x变成其所在原树的根,这样x是没有父节点了
然后把x的父节点设为y即可,删边、询问路径的道理类似,具体见程序
给出的是bzoj2631的程序
- const mo=;
- var fa,q,a,mul,add,size,sum:array[..] of longint;
- rev:array[..] of boolean;
- son:array[..,..] of longint;
- i,x0,y0,x,y,z,n,m,t:longint;
- ch:char;
- function root(x:longint):boolean; //判断是Auxiliary tree(splay)上的父节点还是path parent
- begin
- exit((son[fa[x],]<>x) and (son[fa[x],]<>x));
- end;
- procedure swap(var a,b:longint);
- var c:longint;
- begin
- c:=a;
- a:=b;
- b:=c;
- end;
- procedure update(x:longint);
- var l,r:longint;
- begin
- l:=son[x,];
- r:=son[x,];
- size[x]:=(size[l]+size[r]+) mod mo;
- sum[x]:=(a[x]+sum[l]+sum[r]) mod mo;
- end;
- procedure calc(x,c,j:longint);
- begin
- if (c<=) then //常数优化,少用int64
- begin
- mul[x]:=mul[x]*c mod mo;
- add[x]:=(add[x]*c+j) mod mo;
- sum[x]:=(sum[x]*c+int64(j)*int64(size[x])) mod mo;
- a[x]:=(a[x]*c+j) mod mo;
- end
- else begin
- mul[x]:=int64(mul[x])*int64(c) mod mo;
- add[x]:=(int64(add[x])*int64(c)+j) mod mo;
- sum[x]:=(int64(sum[x])*int64(c)+int64(j)*int64(size[x])) mod mo;
- a[x]:=(int64(a[x])*int64(c)+j) mod mo;
- end;
- end;
- procedure push(x:longint);
- begin
- if rev[x] then
- begin
- rev[son[x,]]:=not rev[son[x,]];
- rev[son[x,]]:=not rev[son[x,]];
- swap(son[x,],son[x,]);
- rev[x]:=false;
- end;
- if (mul[x]<>) or (add[x]<>) then
- begin
- if son[x,]<> then calc(son[x,],mul[x],add[x]);
- if son[x,]<> then calc(son[x,],mul[x],add[x]);
- mul[x]:=;
- add[x]:=;
- end;
- end;
- procedure rotate(x,w:longint);
- var y:longint;
- begin
- y:=fa[x];
- if not root(y) then
- begin
- if son[fa[y],]=y then son[fa[y],]:=x
- else son[fa[y],]:=x;
- end;
- fa[x]:=fa[y];
- son[y,-w]:=son[x,w];
- if son[x,w]<> then fa[son[x,w]]:=y;
- son[x,w]:=y;
- fa[y]:=x;
- update(y);
- end;
- procedure splay(x:longint);
- var y,t,i:longint;
- ff:boolean;
- begin
- t:=;
- i:=x;
- while not root(i) do
- begin
- inc(t);
- q[t]:=i;
- i:=fa[i];
- end;
- inc(t);
- q[t]:=i;
- for i:=t downto do
- push(q[i]);
- ff:=true;
- if t= then exit;
- while ff do
- begin
- y:=fa[x];
- if y=q[t] then
- begin
- if son[y,]=x then rotate(x,)
- else rotate(x,);
- ff:=false;
- end
- else begin
- if fa[y]=q[t] then ff:=false;
- if son[fa[y],]=y then
- begin
- if son[y,]=x then rotate(y,)
- else rotate(x,);
- rotate(x,);
- end
- else begin
- if son[y,]=x then rotate(x,)
- else rotate(y,);
- rotate(x,);
- end;
- end;
- end;
- update(x);
- end;
- procedure access(x:longint);
- var y:longint;
- begin
- y:=;
- repeat
- splay(x);
- son[x,]:=y;
- update(x); //这里要记得维护
- y:=x;
- x:=fa[x];
- until x=;
- end;
- procedure makeroot(x:longint);
- begin
- access(x); //执行access之后,x和当前树的根之间的路径构成Auxiliary tree
- splay(x); //将x旋到Auxiliary tree的根,这时候x没有右孩子,左子树的点都是x的祖辈
- rev[x]:=not rev[x]; //执行翻转操作,这时候x是Auxiliary tree上最小的点,也就变成了原树的根
- end;
- procedure link(x,y:longint);
- begin
- makeroot(x);
- fa[x]:=y;
- end;
- procedure cut(x,y:longint);
- begin
- makeroot(x);
- access(y);
- splay(y);
- son[y,]:=;
- fa[x]:=;
- end;
- procedure path(x,y:longint); //构成x到y的路径
- begin
- makeroot(x);
- access(y); //x到y路径上的点就在一棵Auxiliary tree中
- splay(y);
- end;
- begin
- readln(n,m);
- for i:= to n do
- begin
- a[i]:=;
- size[i]:=;
- sum[i]:=;
- mul[i]:=;
- end;
- for i:= to n- do
- begin
- readln(x,y);
- link(x,y);
- end;
- for i:= to m do
- begin
- read(ch);
- if ch='+' then
- begin
- readln(x,y,z);
- z:=z mod mo;
- path(x,y);
- calc(y,,z);
- end
- else if ch='-' then
- begin
- readln(x0,y0,x,y);
- cut(x0,y0);
- link(x,y);
- end
- else if ch='*' then
- begin
- readln(x,y,z);
- z:=z mod mo;
- path(x,y);
- calc(y,z,);
- end
- else begin
- readln(x,y);
- path(x,y);
- writeln(sum[y]);
- end;
- end;
- end.
bzoj2631 3282的更多相关文章
- BZOJ 2002 && BZOJ 2409 LCT && BZOJ 3282 初步练习
#include <cstdio> ; inline void Get_Int(int & x) { ; ') ch=getchar(); +ch-'; ch=getchar(); ...
- hdu 3282 Running Median
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3282 Running Median Description For this problem, you ...
- 【BZOJ】【3282】Tree
LCT 喜闻乐见的Link-Cut-Tree…… srO zyf http://www.cnblogs.com/zyfzyf/p/4149109.html 目测我是第222个?………………不要在意这些 ...
- [BZOJ 3282] Tree 【LCT】
题目链接:BZOJ - 3282 题目分析 这道题是裸的LCT,包含 Link , Cut 和询问两点之间的路径信息. 写代码时出现的错误:Access(x) 的循环中应该切断的是原来的 Son[x] ...
- BZOJ 3282: Tree( LCT )
LCT.. -------------------------------------------------------------------------------- #include<c ...
- bzoj 3282: Tree (Link Cut Tree)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3282 题面: 3282: Tree Time Limit: 30 Sec Memory L ...
- 【BZOJ】3282: Tree(lct)
http://www.lydsy.com/JudgeOnline/problem.php?id=3282 复习了下lct,发现两个问题.. 1:一开始我以为splay那里直接全部rot(x)就好了,然 ...
- BZOJ 3282: Tree
3282: Tree Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1714 Solved: 765[Submit][Status][Discuss ...
- bzoj 3282
回顾一下LCT,容易写错的地方: 1.每次断掉Splay中的边,必须update一下父亲节点,再根据具体情况是否splay父亲节点. 2.养成没有用的值(比如当pre[u]不为0时的pnt[u])不去 ...
随机推荐
- VIEW层AJAX提交表单到Controller的实体
在MVC环境中,AJAX方式添加一个对象,这个对象在Models中是一个视图模型,在前台显示时是这样的代码: <%using (Html.BeginForm()) { %> ...
- EasyUI中combotree允许多选的时候onSelect事件会重复触发onCheck事件
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgEAAADkCAIAAACOkmAuAAAgAElEQVR4nO2dW2wc15nnO0EQBJsdzA
- Unity中使用RequireComponent,没有添加上组件
using UnityEngine; using System.Collections; [RequireComponent(typeof(MeshFilter), typeof(MeshRender ...
- 理解Java中的字符串类型
1.Java内置对字符串的支持: 所谓的内置支持,即不用像C语言通过char指针实现字符串类型,并且Java的字符串编码是符合Unicode编码标准,这也意味着不用像C++那样通过使用string和w ...
- C# 时间与时间戳互转
/// <summary> /// 将c# DateTime时间格式转换为Unix时间戳格式 /// </summary> /// <param name="t ...
- ci 多个文件同时上传
// 单个文件请手册,这里多个文件中,参数设置可参考手册 view 视图 <form...> <input type="file" name="user ...
- linux根目录详解
ubuntu 文件说明:http://tech.ccidnet.com/art/302/20080118/1347213_1.html/ 根目录 | |-boot/ 启动文件.所有与系统启动有关的 ...
- 解决IE6下不支持 png24的透明图片问题
常用的两种解决方案: 第一:使用IE滤镜解决 关键代码: css代码 _background:none;_filter:progid:DXImageTransform.Microsoft.Alpha ...
- shell脚本修复MySQL主从同步
发布:thebaby 来源:net [大 中 小] 分享一例shell脚本,用于修改mysql的主从同步问题,有需要的朋友参考下吧. 一个可以修改mysql主从同步的shell脚本. 例子 ...
- python【第十八篇】Django基础
1.什么是Django? Django是一个Python写成的开源Web应用框架.python流行的web框架还有很多,如tornado.flask.web.py等.django采用了MVC的框架模式 ...