【模板】Link Cut Tree

思路:

  LCT模板;

代码:

#include <bits/stdc++.h>
using namespace std;
#define maxn 300005
int n,m,val[maxn];
int top,ch[maxn][],f[maxn],xr[maxn],q[maxn],rev[maxn];
inline void in(int &now)
{
int if_z=;now=;
char Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
now*=if_z;
}
inline void updata(int now)
{
xr[now]=xr[ch[now][]]^xr[ch[now][]]^val[now];
}
inline void downdata(int now)
{
int l=ch[now][],r=ch[now][];
if(rev[now])
{
rev[l]^=,rev[r]^=,rev[now]^=;
swap(ch[now][],ch[now][]);
}
}
inline bool isroot(int now)
{
return ch[f[now]][]!=now&&ch[f[now]][]!=now;
}
inline void rotate(int now)
{
int fa=f[now],ffa=f[fa],l,r;
if(ch[fa][]==now) l=;else l=;r=l^;
if(!isroot(fa))
{
if(ch[ffa][]==fa) ch[ffa][]=now;
else ch[ffa][]=now;
}
f[now]=ffa,f[fa]=now,f[ch[now][r]]=fa;
ch[fa][l]=ch[now][r],ch[now][r]=fa;
updata(fa),updata(now);
}
inline void splay(int now)
{
top=,q[top]=now;
for(int i=now;!isroot(i);i=f[i]) q[++top]=f[i];
for(int i=top;i;i--) downdata(q[i]);
while(!isroot(now))
{
int fa=f[now],ffa=f[fa];
if(!isroot(fa))
{
if((ch[fa][]==now)^(ch[ffa][]==fa)) rotate(now);
else rotate(fa);
}
rotate(now);
}
}
void access(int now)
{
for(int t=;now;t=now,now=f[now])
{
splay(now);
ch[now][]=t;
updata(now);
}
}
void makeroot(int now)
{
access(now);
splay(now);
rev[now]^=;
}
int find(int now)
{
access(now);
splay(now);
while(ch[now][]) now=ch[now][];
return now;
}
void split(int x,int y)
{
makeroot(x);
access(y);
splay(y);
}
void cut(int x,int y)
{
split(x,y);
if(ch[y][]==x) ch[y][]=,f[x]=;
}
void link(int x,int y)
{
makeroot(x);
f[x]=y;
}
int main()
{
in(n),in(m);int op,x,y,xx,yy;
for(int i=;i<=n;i++) in(val[i]),xr[i]=val[i];
while(m--)
{
in(op);
if(op==)
{
in(x),in(y),split(x,y);
printf("%d\n",xr[y]);
}
if(op==)
{
in(x),in(y),xx=find(x),yy=find(y);
if(xx!=yy) link(x,y);
}
if(op==)
{
in(x),in(y),xx=find(x),yy=find(y);
if(xx==yy) cut(x,y);
}
if(op==)
{
in(x),in(y);
access(x);
splay(x);
val[x]=y;
updata(x);
}
}
}

AC日记——【模板】Link Cut Tree 洛谷 P3690的更多相关文章

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

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

  2. 洛谷P3690 [模板] Link Cut Tree [LCT]

    题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...

  3. 洛谷.3690.[模板]Link Cut Tree(动态树)

    题目链接 LCT(良心总结) #include <cstdio> #include <cctype> #include <algorithm> #define gc ...

  4. 模板Link Cut Tree (动态树)

    题目描述 给定N个点以及每个点的权值,要你处理接下来的M个操作.操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联 ...

  5. AC日记——校门外的树 洛谷 P1047

    题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,……,L,都种 ...

  6. AC日记——无线网络发射器选址 洛谷 P2038

    题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129 条东西向街道和129 条南北向街道所形成的网格状,并且相邻 ...

  7. AC日记——小A的糖果 洛谷七月月赛

    小A的糖果 思路: for循环贪心: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #defi ...

  8. AC日记——矩阵取数游戏 洛谷 P1005

    矩阵取数游戏 思路: dp+高精: 代码: #include <bits/stdc++.h> using namespace std; #define ll long long struc ...

  9. AC日记——红色的幻想乡 洛谷 P3801

    红色的幻想乡 思路: 线段树+容斥原理: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #de ...

随机推荐

  1. STL使用总结

    转载于http://blog.csdn.net/daisy_chenting/article/details/6898184 1.    概述 泛型编程思想最早缘于A.Stepanov提出的部分算法可 ...

  2. Sql Server 2008 R2 数据库脚本导出方法

    经常忘记怎么搞,截几张图记录一下. 1 选中要导出的数据库,右键—>任务—>生成脚本 2 3 4 查看保存的脚本

  3. DOM案例五星评分控件

    模仿网页上评分的五个五星. <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> < ...

  4. sgu 131 状压DP

    棋盘覆盖(二) 时间限制:1000 ms  |  内存限制:65535 KB     描述 The banquet hall of Computer Scientists' Palace has a ...

  5. 你还在用notifyDataSetChanged?

    想到发这篇帖子是源于我的上一篇帖子#Testin杯#多线程断点续传后台下载 .帖子中讲述的项目使用了listView这个控件,而且自定义了adapter.在更新item的进度条时发现每次使用notif ...

  6. [LeetCode] Sort Colors 对于元素取值有限的数组,只遍历一遍的排序方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  7. centos6.8安装并配置zimbra

    一.对域名设置MX记录 二.安装准备 1.关闭selinux vi /etc/selinux/config SELINUX=disabled 2.iptables防火墙端口设置 # iptables ...

  8. 通过gitlabAPI批量创建用户

    上午服务器领导通知我给服务器所有同事添加gitlab账号,服务器总共67个人,这要是一个一个在页面添加,我得累死,是否有其他的办法呢?有问题找google,果然是可以通过gitlab的API批量添加的 ...

  9. JS 本地属性与继承属性

    判断是否拥有某种属性 1.in 运算符 var obj = {name:'jack'}; alert('name' in obj); // --> true alert('toString' i ...

  10. [转]python os模块 常用命令

    python编程时,经常和文件.目录打交道,这是就离不了os模块.os模块包含普遍的操作系统功能,与具体的平台无关.以下列举常用的命令 1. os.name()——判断现在正在实用的平台,Window ...