luogu 3690 【模板】 Link Cut Tree (动态树)
原来的代码有一些问题。
主要是对于不一定存在的边如何去判断,首先要保证在一个splay里,然后保证彼此之间直接联通且x的右儿子是空的
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define ll long long
#define inf 2139062143
#define MAXN 300100
#define MOD 998244353
#define rep(i,s,t) for(register int i=(s),i##__end=(t);i<=i##__end;++i)
#define dwn(i,s,t) for(register int i=(s),i##__end=(t);i>=i##__end;--i)
#define ren for(register int i=fst[x];i;i=nxt[i])
#define pb(i,x) vec[i].push_back(x)
#define pls(a,b) (a+b)%MOD
#define mns(a,b) (a-b+MOD)%MOD
#define mul(a,b) (1LL*(a)*(b))%MOD
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-;ch=getchar();}
while(isdigit(ch)) {x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m;
int ch[MAXN][],tag[MAXN],sum[MAXN],val[MAXN],fa[MAXN];
struct Lct
{
int which(int x) {return ch[fa[x]][]==x;}
int isroot(int x) {return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;}
void upd(int x) {sum[x]=val[x]^sum[ch[x][]]^sum[ch[x][]];}
void rev(int x) {tag[x]^=;swap(ch[x][],ch[x][]);}
void pshd(int x)
{
if(!tag[x]) return ;tag[x]^=;
if(ch[x][]) rev(ch[x][]);
if(ch[x][]) rev(ch[x][]);
}
void rotate(int x)
{
int f=fa[x],ff=fa[f],k=which(x);
if(!isroot(f)) ch[ff][ch[ff][]==f]=x;fa[x]=ff;
ch[f][k]=ch[x][k^],fa[ch[f][k]]=f,ch[x][k^]=f,fa[f]=x;
upd(f);upd(x);
}
int st[MAXN],top;
void splay(int x)
{
st[top=]=x;
for(int i=x;!isroot(i);i=fa[i]) st[++top]=fa[i];
dwn(i,top,) pshd(st[i]);
for(int f;!isroot(x);rotate(x))
if(!isroot(f=fa[x])) rotate(which(x)==which(f)?f:x);
}
void access(int x) {for(int t=;x;t=x,x=fa[x]){splay(x);ch[x][]=t;upd(x);}}
void mkrt(int x) {access(x);splay(x);rev(x);}
void link(int x,int y) {mkrt(x);fa[x]=y;}
void split(int x,int y) {mkrt(x);access(y);splay(y);}
int find(int x)
{
access(x);splay(x);
while(ch[x][]) pshd(x),x=ch[x][];return x;
}
void cut(int x,int y) {split(x,y);if(fa[x]==y&&ch[y][]==x&&!ch[x][]) ch[y][]=fa[x]=,upd(y);}
}lct;
int main()
{
n=read(),m=read();int t,a,b;rep(i,,n) val[i]=read();
while(m--)
{
t=read(),a=read(),b=read();
if(!t) {lct.split(a,b);printf("%d\n",sum[b]);}
if(t==&&lct.find(a)!=lct.find(b)) lct.link(a,b);
if(t==&&lct.find(a)==lct.find(b)) lct.cut(a,b);
if(t==) val[a]=b,splay(a);
}
}
luogu 3690 【模板】 Link Cut Tree (动态树)的更多相关文章
- 洛谷.3690.[模板]Link Cut Tree(动态树)
题目链接 LCT(良心总结) #include <cstdio> #include <cctype> #include <algorithm> #define gc ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- Link Cut Tree 动态树 小结
动态树有些类似 树链剖分+并查集 的思想,是用splay维护的 lct的根是动态的,"轻重链"也是动态的,所以并没有真正的轻重链 动态树的操作核心是把你要把 修改/询问/... 等 ...
- LCT(link cut tree) 动态树
模板参考:https://blog.csdn.net/saramanda/article/details/55253627 综合各位大大博客后整理的模板: #include<iostream&g ...
- 洛谷P3690 Link Cut Tree (动态树)
干脆整个LCT模板吧. 缺个链上修改和子树操作,链上修改的话join(u,v)然后把v splay到树根再打个标记就好. 至于子树操作...以后有空的话再学(咕咕咕警告) #include<bi ...
- 洛谷P3690 [模板] Link Cut Tree [LCT]
题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...
- 模板Link Cut Tree (动态树)
题目描述 给定N个点以及每个点的权值,要你处理接下来的M个操作.操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联 ...
- 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)
题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板
P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...
随机推荐
- MxCAD5.2 20180726更新
下载地址: http://www.mxdraw.com/ndetail_105.html 1. 增加属性匹配功能 2. 增加List命令 3. 增加CAD图纸审图批注功能 4. 环形阵列功能
- linux mysql设置远程访问
>mysql -u root -p 选择进入mysql数据库use `mysql`; 查看所有存在的账号和地址.SELECT `Host`,`User` FROM `user`; 现在决定让ro ...
- XGBoost参数中文翻译以及参数调优
XGBoost:参数解释:https://blog.csdn.net/zc02051126/article/details/46711047 机器学习系列(11)_Python中Gradient Bo ...
- 关于C++中字符串输入get与getline的区别
最近使用C++中自己老是忘记的一个点,get与getline的区别. 1.get与getline get和getline所属iostream类,作用是读取一整行,通过换行符确定读取结束,他们都可以读取 ...
- <MySQL>入门五 视图
-- 视图 /* 含义:虚拟表,和普通的表一样使用 mysql5.1版本的新特性,是通过表动态生成的数据,只保存了sql的逻辑,不保存查询的结果 应用场景: - 多个地方用到同样的查询结果 - 该查询 ...
- js之获取html标签的值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- BZOJ 5028 小Z的加油店
[题解] 本题要求求出区间内的各个元素通过加减之后能够得出的最小的数,那么根据裴蜀定理可知答案就是区间内各个元素的最大公约数. 那么本题题意化简成了维护一个序列,支持区间加上某个数以及查询区间元素的最 ...
- PAT 1143 Lowest Common Ancestor
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- js中细小点
昨天在写前端代码的时候,遇到一个神奇的问题,我判断序号 num 和数组 arr 的 length 是否相等,如果相等则做想要的处理,伪码如下; if (num === arr.length) { fu ...
- CentOS服务器上部署 oracle10gr2
1.下载Centos系统 Linux 镜像文件. 推荐使用 CentOS5.4,下载地址:http://isoredirect.centos.org/centos/5/isos/i38 ...