【模板】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. jQuery获取元素对象本身的html

    Jquery获取html标签,包含该标签本身 2018年04月01日 20:16:10 清--水 阅读数:829   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.c ...

  2. ACE中UDP通信

    转载于:http://www.cnblogs.com/TianFang/archive/2006/12/07/585205.html udp是一种无连接的协议,提供无连接不可靠的服务. 在ace中,通 ...

  3. Java反转字符串的方式?

    1. 将String转换成字符数组,再利用字符数组进行首尾调换. 2. 利用递归的方式,主要是:reverse(str.substring(1)) + str.charAt(0); 3. 虽然Stri ...

  4. Desert King 最小比率生成树 (好题)

    Description David the Great has just become the king of a desert country. To win the respect of his ...

  5. getopt和getopt_long参数处理

    1:getopt函数 getopt主要用于解析程序运行时所带的参数,原型如下: #include <unistd.h> int getopt(int argc, char * const ...

  6. DOM动态操纵控件案例

    点击登陆显示登陆框 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head ...

  7. ACM1325Is it A tree?

    通过这道简单而又坑人的题目,练习并查集和set 容器的使用: Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

  8. bzoj 3197 DP

    这道题我们可以看成给定两个黑白树,可以修改其中一棵树的颜色,问最少修改多少颜色可以使两棵树同构. 首先我们知道在树的同构中树上最长链中点(如果是偶数的话就是中间两个点)是不变的,我们把这个点叫做树的重 ...

  9. Android--hardwareAccelerated 硬件加速详解 android:largeHeap="true"

    做项目时,引导页面的ViewPager报了OOM异常,图片并不大,在清单文件Application节点中添加了两行代码就解决了这个问题 android:hardwareAccelerated=&quo ...

  10. mouseover/mouseenter/mouseout/mouseleave的区别

    mouseover:鼠标指针穿过被选元素或其子元素,均会触发事件 mouseenter:鼠标指针穿过被选元素时才触发事件 mouseout:鼠标指针离开被选元素或其子元素则触发事件 mouseleav ...