LCT模板(无讲解)
怎么说呢,照着打一遍就自然理解了,再打一遍就会背了,再打一遍就会推了。
// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
const int maxn=3E5+;
int fa[maxn],son[maxn][],val[maxn],ans[maxn],n,m,opt,x,y,z;
bool tag[maxn];
bool notroot(int x){return son[fa[x]][]==x||son[fa[x]][]==x;}
void update(int x){ans[x]=ans[son[x][]]^ans[son[x][]]^val[x];}
void pushr(int x)
{
swap(son[x][],son[x][]);
tag[x]^=;
}
void pushdown(int x)
{
if(tag[x])
{
if(son[x][])pushr(son[x][]);
if(son[x][])pushr(son[x][]);
tag[x]=;
}
}
void rotate(int x,int c)
{
int y=fa[x];
fa[x]=fa[y];
son[y][!c]=son[x][c];
if(son[x][c])fa[son[x][c]]=y;
if(notroot(y)&&son[fa[y]][]==y)son[fa[y]][]=x;
else if(notroot(y))son[fa[y]][]=x;
son[x][c]=y;
fa[y]=x;
update(y);
update(x);
}
void down(int x)
{
if(!notroot(x)){pushdown(x);return;}
down(fa[x]);
pushdown(x);
}
void splay(int x)
{
down(x);
while(true)
{
int y=fa[x];
if(!notroot(x))break;
if(!notroot(y))
{
if(son[y][]==x)rotate(x,);
else rotate(x,);
break;
}
if(son[fa[y]][]==y)
{
if(son[y][]==x)rotate(y,),rotate(x,);
else rotate(x,),rotate(x,);
}
else
{
if(son[y][]==x)rotate(y,),rotate(x,);
else rotate(x,),rotate(x,);
}
}
}
void access(int x)
{
for(int y=;x;y=x,x=fa[x])
splay(x),son[x][]=y,update(x);
}
void makeroot(int x)
{
access(x);
splay(x);
pushr(x);
}
int findroot(int x)
{
access(x);
splay(x);
while(son[x][])pushdown(x),x=son[x][];
splay(x);
return x;
}
void split(int x,int y)
{
makeroot(x);
access(y);
splay(y);
}
void link(int x,int y)
{
makeroot(x);
if(findroot(y)!=x)fa[x]=y;
}
void cut(int x,int y)
{
makeroot(x);
if(findroot(y)==x&&fa[y]==x&&!son[y][])
{
fa[y]=son[x][]=;
update(x);
update(y);
}
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=;i<=n;++i)cin>>val[i];
while(m--)
{
cin>>opt>>x>>y;
if(opt==)
{
split(x,y);
cout<<ans[y]<<endl;
}
else if(opt==)link(x,y);
else if(opt==)cut(x,y);
else
{
splay(x);
val[x]=y;
}
}
return ;
}
LCT模板(无讲解)的更多相关文章
- LCT模板
之前一直用的LCT模板,因为其实个人对LCT和Splay不是很熟,所以用起来总觉得略略的坑爹,过了一段时间就忘了,但事实上很多裸的LCT要改的东西是不多的,所以今天写了些注释,以后可能套起模板来会得心 ...
- LCT 模板及套路总结
这一个月貌似已经考了无数次\(LCT\)了..... 保险起见还是来一发总结吧..... A. LCT 模板 \(LCT\) 是由大名鼎鼎的 \(Tarjan\) 老爷发明的. 主要是用来维护树上路径 ...
- [洛谷P1501] [国家集训队]Tree II(LCT模板)
传送门 这是一道LCT的板子题,说白了就是在LCT上支持线段树2的操作. 所以我只是来存一个板子,并不会讲什么(再说我也不会,只能误人子弟2333). 不过代码里的注释可以参考一下. Code #in ...
- LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板
P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...
- Miller Robbin测试模板(无讲解)
想着费马定理和二次探测定理就能随手推了. 做一次是log2n的. #include<bits/stdc++.h> using namespace std; typedef long lon ...
- NTT模板(无讲解)
#include<bits/stdc++.h>//只是在虚数部分改了一下 using namespace std; typedef long long int ll; ; ; ; ; ll ...
- FFT模板(无讲解)
#include<bits/stdc++.h> using namespace std; ; const double pi=3.1415926535898; ],len; struct ...
- Luogu 3690 LCT - 模板
推荐几篇比较好的博客: FlashHu 的 讲解比较好 : 传送门 Candy 的 代码~ : 传送门 以及神犇Angel_Kitty的 学习笔记: 传送门 Code V 模板 #include< ...
- BZOJ3282: Tree (LCT模板)
Description 给定N个点以及每个点的权值,要你处理接下来的M个操作. 操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和 ...
随机推荐
- 16: mint-ui移动端
1.1 mint-ui安装与介绍 官网:http://mint-ui.github.io/docs/#/zh-cn2/loadmore 1.安装与引用 // 安装Vue 2.0 npm instal ...
- Domain logic approachs
1.transaction script(事务脚本) 概述: 很多企业应用可以看成一系列的事务,每一个事务可以通过使用一个Transaction Script来处理. 用法: 使用Transactio ...
- Navicat Premium 12
1.win 客户端软件下载: https://www.navicat.com.cn/download/navicat-premium 2.安装 双击安装--点击下一步 我同意--下一步 选择安装路径- ...
- JS实现打开本地文件或文件夹 ActiveXObject
IE浏览器打开C盘,测试可用. 如果浏览器报错提示:ActiveXObject is not defined Internet 选项 -> 安全 - >安全级别,调低级别 function ...
- sqlmap学习笔记
SQLmap 神仙工具Orz.需要安装python2.7环境. 语法 -u 指定url --is-dba 查询是否为数据库管理员(database administrator) --dbs 查询数据库 ...
- react生命周期和组件生命周期
React的组件在第一次挂在的时候首先获取父组件传递的props,接着获取初始的state值,接着经历挂载阶段的三个生命周期函数,也就是ComponentWillMount,render,Compon ...
- showdoc 自动脚本安装
========================== showdoc 简介==========================在线文档管理系统很多, 比如阿里的语雀.腾讯的 TAPD 平台也包括文档管 ...
- Linux下“/”和“~”的区别
在linux中,”/“代表根目录,”~“是代表目录.Linux存储是以挂载的方式,相当于是树状的,源头就是”/“,也就是根目录. 而每个用户都有”家“目录,也就是用户的个人目录,比如root用户的”家 ...
- react props与render成员函数
props是组件固有的属性集合,其数据由外部传入,一般在整个组件的生命周期中都是只读的,React的API顶层设计也决定了这一点.属性初值通常由React.createElement函数或者JSX中标 ...
- 自动生成CHANGELOG.md
$ npm install -g conventional-changelog-cli $ cd my-project $ conventional-changelog -p angular -i C ...