【题目分析】

明显的LCT维护连通性的题目。

access的操作是比较巧妙的,可以把结点到根变成偏爱路径,而且保证了该点是链上深度最深的点。

而且需边的思想也很巧妙,保证了复杂度。

但是只能用于修改路径上的点的权值,不能用于修改整棵子树的信息。

相比Splay,只能说是阉割了修改信息的作用,而增加了删边,加边的操作。

【代码】

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath> #include <set>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <queue> using namespace std; #define maxn 1000005
#define inf (0x3f3f3f3f) int read()
{
int x=0,f=1; char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
} int ch[maxn][2],fa[maxn],num[maxn],xs[maxn],st[maxn],top=0,rev[maxn],n,m,sta[maxn]; void update(int k)
{xs[k]=num[k]^xs[ch[k][0]]^xs[ch[k][1]];} bool isroot(int k)
{return ch[fa[k]][0]!=k&&ch[fa[k]][1]!=k;} void pushdown(int k)
{
if (rev[k])
{
rev[k]^=1;
rev[ch[k][0]]^=1;
rev[ch[k][1]]^=1;
swap(ch[k][0],ch[k][1]);
}
} void rot(int x)
{
int y=fa[x],z=fa[y],l,r;
if (ch[y][0]==x) l=0; else l=1;
r=l^1;
if (!isroot(y))
{
if (ch[z][0]==y) ch[z][0]=x;
else ch[z][1]=x;
}
fa[x]=z; fa[y]=x; fa[ch[x][r]]=y;
ch[y][l]=ch[x][r]; ch[x][r]=y;
update(y); update(x);
} void splay(int x)
{
top=0; sta[++top]=x;
for (int i=x;!isroot(i);i=fa[i]) sta[++top]=fa[i];
while (top) pushdown(sta[top--]);
while (!isroot(x))
{
int y=fa[x],z=fa[y];
if (!isroot(y))
{
if (ch[y][0]==x^ch[z][0]==y) rot(y);
else rot(x);
}
rot(x);
}
} void access(int x)
{
for (int t=0;x;t=x,x=fa[x])
splay(x),ch[x][1]=t,update(x);
} void makeroot(int x)
{
access(x); splay(x); rev[x]^=1;
} int find(int x)
{
access(x);
splay(x);
while (ch[x][0]) x=ch[x][0];
return x;
} void cut(int x,int y)
{
makeroot(x);
access(y);
splay(y);
if (ch[y][0]==x) ch[y][0]=fa[x]=0;
} void link(int x,int y)
{
makeroot(x);
fa[x]=y;
} int main()
{
n=read(); m=read();
for (int i=1;i<=n;++i) xs[i]=num[i]=read();
int opt,x,y;
while (m--)
{
opt=read();x=read();y=read();
switch (opt)
{
case 0:makeroot(x); access(y); splay(y); printf("%d\n",xs[y]); break;
case 1:if (find(x)!=find(y)) link(x,y); break;
case 2:if (find(x)==find(y)) cut(x,y); break;
case 3:access(x); splay(x); num[x]=y; update(x); break;
}
}
}

  

BZOJ 3282 Tree ——Link-Cut Tree的更多相关文章

  1. bzoj 3282: Tree (Link Cut Tree)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3282 题面: 3282: Tree Time Limit: 30 Sec  Memory L ...

  2. 【BZOJ 3282】Tree Link Cut Tree模板题

    知道了为什么要换根(changeroot),access后为什么有时要splay,以及LCT的其他操作,算是比较全面的啦吧,,, 现在才知道这些,,,真心弱,,, #include<cstdio ...

  3. [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)

    [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...

  4. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  5. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

  6. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

  7. Link/cut Tree

    Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...

  8. 洛谷P3690 Link Cut Tree (模板)

    Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...

  9. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  10. bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门

    link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...

随机推荐

  1. @AutoWired注解使用时可能会发生的错误

    @Autowired注解:用于对Bean的属性变量.属性的set方法及构造函数进行标注,配合对应的注解处理器完成Bean的自动配置工作.@Autowired注解默认按照Bean类型进行装配.  1.在 ...

  2. 迅为10.1寸人机界面工业HMI安卓电容屏定制生产供应商

    10.1寸人机界面介绍: 配置铁电存储器:非易失性记忆体,掉电后数据不丢失. 连接云端,支持云服务:数据综合管理,更有效率. 静电防护技术:高强度抗干扰,防静电,防电磁干扰. 提供所有接口的调用源码, ...

  3. UVA - 11082 Matrix Decompressing (最大流,技巧)

    很经典的网络流模型,行编号和列编号分别看成一个点,行和列和分别看出容量,一个点(x,y)看出是一条边,边的容量下界是1,所以先减去1,之后在加上就好了. 建图的时候注意分配好编号,解从残留网络中的边找 ...

  4. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  5. IOS Array 排序方法

    NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) { if ([obj1 integerVal ...

  6. 数据库-SQL语法:GROUP BY与HAVING

    注意:select 后的字段,必须要么包含在group by中,要么包含在having 后的聚合函数里. 1. GROUP BY 是分组查询, 一般 GROUP BY 是和聚合函数配合使用. grou ...

  7. js解析json格式

    function save(){ var value2 = { "china":[ {"name":"hangzhou", "it ...

  8. shell脚本,awk替换{}里面的内容

    如何将oxo{axbxc}oxo{dxexf}oxo里面的{}里面的x 替换为; 用awk实现 [root@localhost 09-30]# echo 'oxo{axbxc}oxo{dxexf}ox ...

  9. 使用Microsoft Hadoop(一)

    To run this program, stage some data in HDFS: 1. create a text file called input.txt that has one in ...

  10. 身份证号正则校验(js校验+JAVA校验)

    js校验身份证号[15位和18位] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3 ...