粘板子:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 300050;
template<typename T>
inline void read(T&x)
{
T f = 1,c = 0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){c=c*10+ch-'0';ch=getchar();}
x = f*c;
}
int n,m,val[N];
struct LCT
{
int fa[N],ch[N][2],w[N],v[N];
bool res[N];
inline void rever(int u){swap(ch[u][0],ch[u][1]);res[u]^=1;}
inline bool isroot(int u){return ch[fa[u]][0]!=u&&ch[fa[u]][1]!=u;}
inline void update(int u){w[u]=w[ch[u][0]]^w[ch[u][1]]^v[u];}
inline void pushdown(int u)
{
if(!u)return ;
if(res[u])
{
rever(ch[u][0]);
rever(ch[u][1]);
res[u]=0;
}
}
void rotate(int x)
{
int y = fa[x],z = fa[y],k = (ch[y][1]==x);
if(!isroot(y))ch[z][ch[z][1]==y]=x;fa[x] = z;
ch[y][k] = ch[x][!k],fa[ch[x][!k]] = y;
ch[x][!k] = y,fa[y] = x;
update(y),update(x);
}
void down(int x)
{
if(!isroot(x))down(fa[x]);
pushdown(x);
}
void splay(int x)
{
down(x);
while(!isroot(x))
{
int y = fa[x],z = fa[y];
if(!isroot(y))
(ch[z][1]==y)^(ch[y][1]==x)?rotate(x):rotate(y);
rotate(x);
}
}
void access(int x)
{
int y = 0;
while(x)
{
splay(x);ch[x][1]=y;
update(x);y=x,x=fa[x];
}
}
void mtr(int x)
{
access(x);
splay(x);
rever(x);
}
int get_root(int x)
{
access(x);
splay(x);
while(ch[x][0])x=ch[x][0];
return x;
}
void link(int x,int y)
{
mtr(x);
if(get_root(y)!=x)fa[x]=y;
}
void cut(int x,int y)
{
mtr(x);
access(y),splay(y);
if(ch[y][0]==x)
{
ch[y][0]=fa[x]=0;
update(y);
}
}
void insert(int x,int y)
{
splay(x);
v[x]=y;
update(x);
}
int query(int x,int y)
{
mtr(x);
access(y);
splay(y);
return w[y];
}
}tr;
int main()
{
read(n),read(m);
for(int i=1;i<=n;i++)read(val[i]),tr.v[i]=tr.w[i]=val[i];
for(int op,x,y,i=1;i<=m;i++)
{
read(op),read(x),read(y);
if(op==0)printf("%d\n",tr.query(x,y));
else if(op==1)tr.link(x,y);
else if(op==2)tr.cut(x,y);
else tr.insert(x,y);
}
return 0;
}

LCT板子的更多相关文章

  1. BZOJ 2049 SDOI2008 洞穴勘测 LCT板子

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 题意概述:给出N个点,一开始不连通,M次操作,删边加边,保证图是一个森林,询问两点连 ...

  2. 沉迷Link-Cut tree无法自拔之:[BZOJ2049]洞穴勘探(蒟蒻的LCT板子)

    来自蒟蒻 \(Hero \_of \_Someone\) 的 \(LCT\) 学习笔记 最近学了一波 \(LCT\) , 于是怒刷 \(LCT\) 合集...... $ $ 学的时候借鉴了 Clove ...

  3. 一堆LCT板子

    搞了一上午LCT,真是累死了-- 以前总觉得LCT高大上不好学不好打,今天打了几遍感觉还可以嘛= =反正现在的水平应付不太难的LCT题也够用了,就这样好了,接下来专心搞网络流. 话说以前一直YY不出来 ...

  4. 好的LCT板子和一句话

    typedef long long ll; const int maxn = 400050; struct lct { int ch[maxn][2], fa[maxn], w[maxn]; bool ...

  5. BZOJ 2002 LCT板子题

    思路: LCT啊... (分块也行) 不过YOUSIKI出了一道“弹飞大爷” 就不能用分块水过去了 //By SiriusRen #include <cstdio> #include &l ...

  6. 洛谷P3203 [HNOI2010]弹飞绵羊(LCT,Splay)

    洛谷题目传送门 关于LCT的问题详见我的LCT总结 思路分析 首先分析一下题意.对于每个弹力装置,有且仅有一个位置可以弹到.把这样的一种关系可以视作边. 然后,每个装置一定会往后弹,这不就代表不存在环 ...

  7. bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2049 题面: 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 ...

  8. ☆ [NOI2014] 魔法森林 「LCT动态维护最小生成树」

    题目类型:\(LCT\)动态维护最小生成树 传送门:>Here< 题意:带权无向图,每条边有权值\(a[i],b[i]\).要求一条从\(1\)到\(N\)的路径,使得这条路径上的\(Ma ...

  9. LCT模板(指针版)

    本来是想做THUWC2017的泰勒展开xLCT题的-- 然后觉得数组写很麻烦-- 然后就决定挑战指针版-- 然后写得全是BUG-- 与BUG鏖战三千年后,有了这个指针版LCT板子! #include ...

随机推荐

  1. zabbix-agentd;客户端开启多个端口。

    学习标杆:https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/8/html/configuring_basic ...

  2. 运行EXE文件,系统提示缺少api-ms-win-crt-runtime-l1-1-0.dll,解决方法!!

    api-ms-win-crt-runtime-l1-1-0.dll是一个Windows系统的应用程序拓展文件,也叫动态链接库文件.这个文件能帮助绝大多数用户解决在安装或启动部分游戏软件时出现计算机中丢 ...

  3. SPYEYE手机远程监控和官方SPYEYE间谍软件最新下载方式

    听起来远程控制手机好像很高级的样子,但是实现起来其实非常简单.实现原理如下: 运行程序,让程序不停地读取数据 用手机给手机发送邮件 判断是否读取到指定主题的手机,如果有,则获取手机内容 根据邮件内容, ...

  4. System.Console.WriteLine() 调用原理

    1.System.Console.WriteLine(类的实例)默认调用类的Tostring()方法.如果自定义的新类未override ToString()方法.那么调用Object.ToStrin ...

  5. 网络类型PAN、LAN、WLAN、CAN、MAN、WAN简单介绍

    转至:https://www.cnblogs.com/itmacy/p/12290036.html PAN:personal area network 个人区域网络 无线连接:蓝牙,红外,NFC 有线 ...

  6. Git如何使用,操作流程

    官方示例 git config --global user.name "sanqianll" git config --global user.email "224001 ...

  7. Python:matplotlib.cm 色表

    官网:Choosing Colormaps in Matplotlib - Matplotlib 3.5.0 documentation Colormap与matplotlib.cm 我们以等高区域函 ...

  8. MySql绿色版的安装

    MySql绿色版的安装 1.下载 2.配置环境变量,执行命令 mysqld --initialize-insecure --user=mysql 会创建出data文件夹 3.创建my.ini文件 [c ...

  9. gdb调试快速入门

    编译指令 gcc test.c -o test -g -g是加入调试信息,加入源码信息 启动gdb调试 gdb test 进入gdb中 设置参数 set args 10 20 显示参数show age ...

  10. VirtualBox虚拟机--桥接模式

    问题概述:VirtualBox虚拟机设置桥接模式,与宿主机互相ping通. 注:如果按照以下方式设置了还是ping不通,查看虚拟机防火墙是否已关. 公司电脑拿去维修了,在自己家里电脑上部署项目开发环境 ...