bzoj 2002 LCT
LCT最基础的题,就用到了一个ACCESS操作
首先我们将这个绵羊弹飞的情况看成一颗树,那么假设X点被弹飞到
Y点,那么Y为X的父亲节点,弹飞的话父亲节点为n+1(虚设)
那么每个询问就是询问X点到根节点n+1的路径长度(节点数)
每个修改操作就是将以X为根节点的子树和X的父亲断开,连接到Y上
这样简单的维护森林连通性的问题,动态树中的LCT解决就行了
/**************************************************************
Problem:
User: BLADEVIL
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ //By BLADEVIL
var
n, m :longint;
father, size :array[-..] of longint;
son :array[-..,..] of longint;
root :array[-..] of boolean; procedure update(x:longint);
begin
size[x]:=size[son[x,]]+size[son[x,]]+;
end; procedure left_rotate(x:longint);
var
y :longint;
begin
y:=son[x,];
son[x,]:=son[y,];
father[son[x,]]:=x;
son[y,]:=x;
if x=son[father[x],] then
son[father[x],]:=y else
if x=son[father[x],] then
son[father[x],]:=y;
father[y]:=father[x];
father[x]:=y;
root[y]:=root[x] xor root[y];
root[x]:=root[x] xor root[y];
update(x); update(y);
end; procedure right_rotate(x:longint);
var
y :longint;
begin
y:=son[x,];
son[x,]:=son[y,];
father[son[x,]]:=x;
son[y,]:=x;
if x=son[father[x],] then
son[father[x],]:=y else
if x=son[father[x],] then
son[father[x],]:=y;
father[y]:=father[x];
father[x]:=y;
root[y]:=root[y] xor root[x];
root[x]:=root[y] xor root[x];
update(x); update(y);
end; procedure splay(x:longint);
begin
while not root[x] do
if x=son[father[x],] then
left_rotate(father[x]) else
right_rotate(father[x]);
end; procedure access(x:longint);
var
y :longint;
begin
splay(x);
while father[x]<> do
begin
y:=father[x];
splay(y);
root[son[y,]]:=true;
root[x]:=false;
son[y,]:=x;
update(y);
splay(x);
end;
end; procedure init;
var
i :longint;
begin
read(n);
for i:= to n do
begin
read(father[i]);
father[i]:=father[i]+i;
if father[i]>n then father[i]:=n+;
end;
read(m);
end; procedure main;
var
i :longint;
x, y, z :longint;
begin
for i:= to n+ do size[i]:=;
fillchar(root,sizeof(root),true);
for i:= to m do
begin
read(x);
if x= then
begin
read(y); inc(y);
access(y);
writeln(size[son[y,]]);
end else
begin
read(y,z); inc(y);
splay(y);
father[son[y,]]:=father[y];
root[son[y,]]:=true;
son[y,]:=;
size[y]:=size[son[y,]]+;
father[y]:=y+z;
if father[y]>n then father[y]:=n+;
end;
end;
end; begin
init;
main;
end.
bzoj 2002 LCT的更多相关文章
- BZOJ 2002 LCT板子题
思路: LCT啊... (分块也行) 不过YOUSIKI出了一道“弹飞大爷” 就不能用分块水过去了 //By SiriusRen #include <cstdio> #include &l ...
- 以 BZOJ 2002 为例学习有根树LCT(Link-Cut Tree)
以BZOJ 2002 弹飞绵羊为例学习有根树LCT(Link-Cut Tree) 注:本文非常简单,只涉及有根树LCT,对于无根树,LCT还有几个本文没有提到的操作,以后慢慢更新 =v= 知识储备 [ ...
- BZOJ 2002 && BZOJ 2409 LCT && BZOJ 3282 初步练习
#include <cstdio> ; inline void Get_Int(int & x) { ; ') ch=getchar(); +ch-'; ch=getchar(); ...
- lct 模版题 bzoj 2002 2049
很早就有人给我推荐的模版题,然后我最近才刷的(' ' ) 昨天的tree 不知道比他们高到哪里去了,我和他谈笑风生啊! bzoj 2002 弹飞绵羊 重点:这道题的cut和link 由于这道题链 ...
- bzoj 2002 Bounce 弹飞绵羊
bzoj 2002 Bounce 弹飞绵羊 设一个虚拟节点表示被弹飞,则每个点的后继点是唯一确定的,每个点向它的后继点连边,就形成了一颗树. 询问就是问某个节点到虚拟节点的路径长度,修改就删除原来向后 ...
- [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)
[BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...
- bzoj 2002 [Hnoi2010]Bounce 弹飞绵羊(LCT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2002 [题意] 给定n个数的序列,i可以跳到i+k[i],需要能够修改k并可以查询跳出 ...
- bzoj 2002 : [Hnoi2010]Bounce 弹飞绵羊 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 题面: 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 lct 动态树 splay
http://www.lydsy.com/JudgeOnline/problem.php?id=2002 http://blog.csdn.net/frods/article/details/5224 ...
随机推荐
- excel上传和下载
需要注意的地方: 1.js构造表单并提交 2.js中文传参encodeURI(encodeURI("中文")),action接收并转换value = URLDecoder.deco ...
- HTML5+CSS3前端开发资源整合
HTML5+CSS3前端开发资源整合 推个广告 个人网站:http://www.51pansou.com HTML5视频下载:HTML5视频 HTML5源码下载:HTML5源码 meta相关: & ...
- php中关于抽象(abstract)类和抽象方法的问题解析
在面向对象(OOP)语言中,一个类可以有一个或多个子类,而每个类都有至少一个公有方法作为外部代码访问的接口.而抽象方法就是为了方便继承而引入的,现在来看一下抽象类和抽象方法分别是如何定义以及他们的特点 ...
- PHP搜索MYSQL数据库加分页浏览小结
PHP搜索加分页浏览小结: 1 分页后再做搜索 2 这里对于url的拼接,以及模糊查询,搜索时候的显示添加,SQL语句的拼接 3 对于页面传递过来的超级链接的变量,如果不存在就要设置,对于可能抛出异常 ...
- The Rotation Game (POJ 2286) 题解
[问题描述] (由于是英文的,看不懂,这里就把大意给大家说一下吧……都是中国人,相信大家也不愿意看英文……) 如图,一个井字形的棋盘,中间有着1-3任意的数,有ABCDEFGH八个操作,每个操作意味着 ...
- C# 验证IP是否正确简易方法 源代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- linux网络子系统内核分析
1.选择路由 若要将数据包发至PC2,则linux系统通过查询路由表可知168.1.1.10(目的地址)的网关地址为192.168.1.1,此时linux系统选择网卡1发送数据包. 2.邻居子系统(通 ...
- Flask —— 使用Python和OpenShift进行即时Web开发
最近Packtpub找到了我,让我给他们新出版的关于Flask的书写书评.Flask是一个很流行的Python框架.那本书是Ron DuPlain写的<Flask 即时Web开发>.我决定 ...
- myeclipse激活+Aptana安装配置
一.Myeclipse安装激活. 安装过程一路向下. 1.破解公钥,确保MyEclipse没有开启,否则失败! 用WinRAR或7-zip打开安装目录下Common\plugins\com.genui ...
- Android WIFI 启动流程
参考:http://blog.chinaunix.net/uid-26215986-id-3260413.html 一. WIFI 工作步骤 1. Wifi模块初始化 2. Wifi启动 3. 查找热 ...