[模板]Link-Cut-Tree动态树
做法以后再补,先写一些注意事项。
做法以后也不补了,直接看这个吧。https://www.cnblogs.com/candy99/p/6271344.html
1.rotate其实是最容易写错的地方(对于丝毫没有掌握splay蒟蒻我来说),一定要仔细检查
2.splay之前需要先从根开始往下pushdown一遍
3.注意getRoot(findRoot)和setRoot(makeRoot)对节点位置带来的影响
4.access的结束条件是x==0!!不是fa[x]!!(这个zz才会写错吧) (没错就是我写错了还查了半天)
5.pushdown的位置、是否splay(x)所带来的玄学问题(我哪想的明白啊)
luogu3690(连边、断边、改点值、查询链上异或和)
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<ctime>
#include<set>
#define pa pair<int,int>
#define lowb(x) ((x)&(-(x)))
#define REP(i,n0,n) for(i=n0;i<=n;i++)
#define PER(i,n0,n) for(i=n;i>=n0;i--)
#define MAX(a,b) ((a>b)?a:b)
#define MIN(a,b) ((a<b)?a:b)
#define CLR(a,x) memset(a,x,sizeof(a))
#define rei register int
using namespace std;
typedef long long ll;
const int maxn=; ll rd(){
ll x=;char c=getchar();int neg=;
while(c<''||c>''){if(c=='-') neg=-;c=getchar();}
while(c>=''&&c<='') x=x*+c-'',c=getchar();
return x*neg;
} int N,M;
int ch[maxn][],v[maxn],xs[maxn],fa[maxn];
bool rev[maxn]; inline void pushrev(int x){swap(ch[x][],ch[x][]);rev[x]^=;}
inline void update(int x){xs[x]=xs[ch[x][]]^xs[ch[x][]]^v[x];}
inline void pushdown(int x){
if(!rev[x]) return;
if(ch[x][]) pushrev(ch[x][]);
if(ch[x][]) pushrev(ch[x][]);
rev[x]=;
}
inline bool isRoot(int x){return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;}
inline bool isRson(int x){return x==ch[fa[x]][];}
inline void rotate(int x){
int f=fa[x],ff=fa[f];bool b=isRson(x);
if(ch[x][b^]) fa[ch[x][b^]]=f;ch[f][b]=ch[x][b^];
if(!isRoot(f)) ch[ff][isRson(f)]=x;fa[x]=ff;
fa[f]=x;ch[x][b^]=f;update(f);update(x);
}
void pushall(int x){
if(!isRoot(x)) pushall(fa[x]);
pushdown(x);
}
inline void splay(int x){
pushall(x);
while(!isRoot(x)&&!isRoot(fa[x])){
if(isRson(x)==isRson(fa[x])) rotate(fa[x]);rotate(x);
}if(!isRoot(x)) rotate(x);
}
inline void access(int x){
for(int lst=;x;lst=x,x=fa[x]){
splay(x);ch[x][]=lst;update(x);
}
}
inline int getRoot(int x){
access(x);splay(x);
while(ch[x][]){pushdown(x);x=ch[x][];}return x;
}
inline void setRoot(int x){
access(x);splay(x);pushrev(x);
}
inline void link(int x,int y){
setRoot(x);
if(getRoot(y)!=x) fa[x]=y;
}
inline void cut(int x,int y){
setRoot(x);
if(getRoot(y)!=x||fa[x]!=y||ch[x][]) return;
ch[y][]=fa[x]=;update(y);
}
inline void change(int x,int k){
splay(x);v[x]=k;update(x);
}
inline int query(int x,int y){
setRoot(x);access(y);splay(y);
return xs[y];
} int main(){
//freopen("testdata.in","r",stdin);
rei i,j,k;
N=rd(),M=rd();//printf("%d %d\n",N,M);
for(i=;i<=N;i++) v[i]=xs[i]=rd();
while(M--){
int a=rd(),x=rd(),y=rd();
if(a==) printf("%d\n",query(x,y));
else if(a==) link(x,y);
else if(a==) cut(x,y);
else if(a==) change(x,y);
}
return ;
}
luogu3690
luogu3203 弹飞绵羊(连边、断边、查询链长)
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<ctime>
#include<set>
#define pa pair<int,int>
#define lowb(x) ((x)&(-(x)))
#define REP(i,n0,n) for(i=n0;i<=n;i++)
#define PER(i,n0,n) for(i=n;i>=n0;i--)
#define MAX(a,b) ((a>b)?a:b)
#define MIN(a,b) ((a<b)?a:b)
#define CLR(a,x) memset(a,x,sizeof(a))
#define rei register int
#define lt ch[x][0]
#define rt ch[x][1]
using namespace std;
const int maxn=;
typedef long long ll; ll rd(){
ll x=;char c=getchar();int neg=;
while(c<''||c>''){if(c=='-') neg=-;c=getchar();}
while(c>=''&&c<='') x=x*+c-'',c=getchar();
return x*neg;
} int N,M;
int fa[maxn],ch[maxn][],siz[maxn],to[maxn];
bool rev[maxn]; inline void update(int x){siz[x]=(lt?siz[lt]:)+(rt?siz[rt]:)+;}
inline void pushrev(int x){
rev[x]^=;swap(lt,rt);
}
inline void pushdown(int x){
if(!rev[x]) return;
if(lt) pushrev(lt);
if(rt) pushrev(rt);
rev[x]=;
}
inline bool isRoot(int x){return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;}
inline bool isRson(int x){return x==ch[fa[x]][];}
inline void rotate(int x){
int f=fa[x],ff=fa[f];bool b=isRson(x);
if(!isRoot(f)) ch[ff][isRson(f)]=x;fa[x]=ff;
if(ch[x][b^]) fa[ch[x][b^]]=f;ch[f][b]=ch[x][b^];
ch[x][b^]=f;fa[f]=x;update(f);update(x);
}
inline void pushdall(int x){
if(!isRoot(x)) pushdall(fa[x]);pushdown(x);
}
inline void splay(int x){
pushdall(x);
while(!isRoot(x)&&!isRoot(fa[x])){
if(isRson(x)==isRson(fa[x])) rotate(fa[x]);
else rotate(x);rotate(x);
}if(!isRoot(x)) rotate(x);
}
inline void access(int x){
for(int y=;x;y=x,x=fa[x]){
splay(x);rt=y;update(x);
}
}
inline void setRoot(int x){
access(x);splay(x);
pushrev(x);
}
inline int getRoot(int x){
access(x);splay(x);
while(lt){
pushdown(x);x=lt;
}return x;
}
inline void link(int x,int y){//x->y
setRoot(x);
access(y);splay(y);fa[x]=y;
}
inline void cut(int x,int y){
setRoot(x);access(y);splay(y);
ch[y][]=fa[x]=;update(y);
}
inline int query(int x,int y){
setRoot(x);
access(y);splay(y);
//printf("%d %d\n",ch[y][0],ch[y][1]);
return siz[y];
}
inline void change(int x,int k){
if(x+k>N&&x+to[x]>N) return;
if(to[x]) cut(x,MIN(x+to[x],N+));
link(x,MIN(x+k,N+));
to[x]=k;
} int main(){
//freopen("3203.in","r",stdin);
rei i,j,k;N=rd();
REP(i,,N) change(i,rd()),siz[i]=;siz[N+]=;
M=rd();REP(i,,M){
j=rd(),k=rd()+;
if(j==) printf("%d\n",query(N+,k)-);
else change(k,rd());
}
return ;
}
luogu3203
[模板]Link-Cut-Tree动态树的更多相关文章
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- 洛谷.3690.[模板]Link Cut Tree(动态树)
题目链接 LCT(良心总结) #include <cstdio> #include <cctype> #include <algorithm> #define gc ...
- 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:后接两 ...
随机推荐
- Luogu P3388 【模板】割点(割顶)
一道求割点的板子题.还是采用经典的Tarjan算法. 首先大致和Tarjan求强连通分量相似,都是用\(dfn_x\)表示访问到\(x\)的时间(时间戳),\(low_x\)表示通过\(x\)回边能走 ...
- 网易2018.03.27算法岗,三道编程题100%样例AC题解
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/8660814.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- Daily Scrumming* 2015.12.19(Day 11)
一.团队scrum meeting照片 二.成员工作总结 姓名 任务ID 迁入记录 江昊 任务1090 https://github.com/buaaclubs-team/temp-front/com ...
- 无限级结构SQL查询所有的下级和所有的下级
Id,PId无限级结构,查询某个Id的所有下级或所有上级,使用WITH AS查询 查找Id为1所有的下级 /*查找Id为1所有的下级*/ WITH T AS( SELECT Id,PId,Name,0 ...
- The Golden Age CodeForces - 813B (数学+枚举)
Unlucky year in Berland is such a year that its number n can be represented as n = xa + yb, where a ...
- 剑指offer:变态跳台阶
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 思路 首先想到的解决方案是根据普通跳台阶题目改编,因为可以跳任意级,所以要 ...
- MyBatis 返回类型resultType为map时的null值不返回问题
问题一: 查询结果集中 某字段 的值为null,在map中不包含该字段的key-value对 解决:在mybatis.xml中添加setting参数 <!-- 在null时也调用 sett ...
- mongoDB的配置和使用
如何启动mongodb? mongod --dbpath C:\appStore\mongodata //数据库地址 再开一个cmder窗口 进入C:\Program Files\MongoDB\Se ...
- [转载]Linux目录说明
原作者博客: http://blog.51cto.com/yangrong/1288072 将文字部分转移到自己的目录下便于学习记录 感谢~ 2./目录 目录 描述 / 第一层次结构的根.整个文件系统 ...
- iphone 与 PC端电脑投屏设置
1. iphone端安装: 屏幕投影助手 下载地址 https://itunes.apple.com/cn/app/ping-mu-tou-ying-zhu-shou/id1152332174?mt= ...