LCT板子
粘板子:
#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板子的更多相关文章
- BZOJ 2049 SDOI2008 洞穴勘测 LCT板子
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 题意概述:给出N个点,一开始不连通,M次操作,删边加边,保证图是一个森林,询问两点连 ...
- 沉迷Link-Cut tree无法自拔之:[BZOJ2049]洞穴勘探(蒟蒻的LCT板子)
来自蒟蒻 \(Hero \_of \_Someone\) 的 \(LCT\) 学习笔记 最近学了一波 \(LCT\) , 于是怒刷 \(LCT\) 合集...... $ $ 学的时候借鉴了 Clove ...
- 一堆LCT板子
搞了一上午LCT,真是累死了-- 以前总觉得LCT高大上不好学不好打,今天打了几遍感觉还可以嘛= =反正现在的水平应付不太难的LCT题也够用了,就这样好了,接下来专心搞网络流. 话说以前一直YY不出来 ...
- 好的LCT板子和一句话
typedef long long ll; const int maxn = 400050; struct lct { int ch[maxn][2], fa[maxn], w[maxn]; bool ...
- BZOJ 2002 LCT板子题
思路: LCT啊... (分块也行) 不过YOUSIKI出了一道“弹飞大爷” 就不能用分块水过去了 //By SiriusRen #include <cstdio> #include &l ...
- 洛谷P3203 [HNOI2010]弹飞绵羊(LCT,Splay)
洛谷题目传送门 关于LCT的问题详见我的LCT总结 思路分析 首先分析一下题意.对于每个弹力装置,有且仅有一个位置可以弹到.把这样的一种关系可以视作边. 然后,每个装置一定会往后弹,这不就代表不存在环 ...
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2049 题面: 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 ...
- ☆ [NOI2014] 魔法森林 「LCT动态维护最小生成树」
题目类型:\(LCT\)动态维护最小生成树 传送门:>Here< 题意:带权无向图,每条边有权值\(a[i],b[i]\).要求一条从\(1\)到\(N\)的路径,使得这条路径上的\(Ma ...
- LCT模板(指针版)
本来是想做THUWC2017的泰勒展开xLCT题的-- 然后觉得数组写很麻烦-- 然后就决定挑战指针版-- 然后写得全是BUG-- 与BUG鏖战三千年后,有了这个指针版LCT板子! #include ...
随机推荐
- Solution Set -「LOCAL」冲刺省选 Round XXIII
\(\mathscr{Summary}\) 有一说一,虽然我炸了,但这场锻炼心态的效果真的好.部分分聊胜于无,区分度一题制胜,可谓针对性强的好题. A 题,相对性签到题.这个建图确实巧妙,多见 ...
- 暑假撸系统6- Thymeleaf ajax交互!
本来用Thymeleaf也没想着深度使用ajax,就是用也是非常传统的ajax方式提交然后js控制修改下变量.闲来无事的时候看Thymeleaf的教程发现一哥们的实现方式,以及实现思路,堪称惊奇,先说 ...
- 利用 pip download 打包软件来提供离线安装
文章目录 1.通过 pip download 下载安装包 2.利用 pip install --no-index 离线安装 1.通过 pip download 下载安装包 linux-oz6w:~ # ...
- 什么是ETCD及其应用场景
源自公众号:BiggerBoy 一.什么是etcd? etcd 发音为/ˈɛtsiːdiː/,名字的由来,"distributed etc directory.",意思是&qu ...
- No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK 问题解决
1. 问题描述 使用idea对Java工程执行mvn compile命令进行编译,出现以下报错: [ERROR] Failed to execute goal org.apache.maven.plu ...
- 基于Redis分布式BitMap的应用
一.序言 在实际开发中常常遇到如下需求:判断当前元素是否存在于已知的集合中,将已知集合中的元素维护一个HashSet,使用时只需耗时O(1)的时间复杂度便可判断出结果,Java内部或者Redis均提供 ...
- 微信小程序里实现跑马灯效果
在微信小程序 里实现跑马灯效果,类似滚动字幕或者滚动广告之类的,使用简单的CSS样式控制,没用到JS wxml: <!-- 复制的跑马灯效果 --> <view class=&quo ...
- mapreduce类型对应
public class OrderBean implements WritableComparable<OrderBean> { private Integer order_id; // ...
- selenium+python自动化101-使用execute_script() 方法获取 JavaScript 返回值
前言 之前经常使用 execute_script() 方法执行 JavaScript 的来解决页面上一些 selenium 无法操作的元素,但是一直无法获取执行的返回值. 最近翻文档,发现 execu ...
- Tableau学习Step6一如何制作炫彩地图
Tableau学习Step6一如何制作炫彩地图 本文首发于博客冰山一树Sankey,去博客浏览效果更好. 一.统计地图概述 1.1 统计地图的基本概念 统计地图的本质:数据的正确对应 将数据信息和地理 ...