Description:

给定n个点以及每个点的权值,要你处理接下来的m个操作。操作有4种。操作从0到3编号。点从1到n编号。

0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和。保证x到y是联通的。

1:后接两个整数(x,y),代表连接x到y,若x到y已经联通则无需连接。

2:后接两个整数(x,y),代表删除边(x,y),不保证边(x,y)存在。

3:后接两个整数(x,y),代表将点x上的权值变成y。

模板,不解释,详见代码

#include<bits/stdc++.h>
using namespace std;
const int mxn=3e5+5;
int n,m,t[mxn],fa[mxn],st[mxn],rev[mxn],val[mxn],ch[mxn][2]; namespace lct {
int isnotrt(int x) {
return ch[fa[x]][0]==x||ch[fa[x]][1]==x;
};
void push_up(int x) {
t[x]=t[ch[x][0]]^t[ch[x][1]]^val[x];
};
void push_down(int x) {
if(rev[x]) {
rev[ch[x][0]]^=1,rev[ch[x][1]]^=1;
swap(ch[ch[x][0]][0],ch[ch[x][0]][1]);
swap(ch[ch[x][1]][0],ch[ch[x][1]][1]);
rev[x]=0;
}
}
void rotate(int x) {
int y=fa[x],z=fa[y],tp=ch[y][1]==x;
if(isnotrt(y)) ch[z][ch[z][1]==y]=x; fa[x]=z; //切记要判根
ch[y][tp]=ch[x][tp^1],fa[ch[x][tp^1]]=y;
ch[x][tp^1]=y,fa[y]=x;
push_up(y),push_up(x);
};
void splay(int x) {
int tp=x,s=0; st[++s]=tp;
while(isnotrt(tp)) st[++s]=tp=fa[tp]; //先下放标记,再splay
while(s) push_down(st[s--]);
while(isnotrt(x)) {
int y=fa[x],z=fa[y];
if(isnotrt(y))
(ch[y][1]==x)^(ch[z][1]==y)?rotate(x):rotate(y);
rotate(x);
}
};
void access(int x) {
for(int y=0;x;x=fa[y=x])
splay(x),ch[x][1]=y,push_up(x); //要push_up
};
void makert(int x) {
access(x); splay(x);
swap(ch[x][0],ch[x][1]);
rev[x]^=1;
};
int findrt(int x) {
access(x); splay(x);
while(ch[x][0]) push_down(x),x=ch[x][0]; //要push_down
splay(x); return x;
};
void split(int x,int y) {
makert(x); access(y); splay(y);
};
void link(int x,int y) {
makert(x);
if(findrt(y)!=x) fa[x]=y; //这里需要根据题意写合并条件
};
void cut(int x,int y) {
makert(x);
if(findrt(y)==x&&fa[y]==x&&!ch[y][0]) { //findrt()之后x成为根,在此基础上分类讨论
fa[y]=ch[x][1]=0;
push_up(x);
}
};
}
using namespace lct; int main()
{
scanf("%d%d",&n,&m); int opt,x,y;
for(int i=1;i<=n;++i) scanf("%d",val+i);
while(m--) {
scanf("%d%d%d",&opt,&x,&y);
if(opt==0) split(x,y),printf("%d\n",t[y]);
else if(opt==1) link(x,y);
else if(opt==2) cut(x,y);
else splay(x),val[x]=y;
}
return 0;
}

[模板][P3690]Link Cut Tree的更多相关文章

  1. P3690 【模板】Link Cut Tree (动态树)

    P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...

  2. (RE) luogu P3690 【模板】Link Cut Tree

    二次联通门 : luogu P3690 [模板]Link Cut Tree 莫名RE第8个点....如果有dalao帮忙查错的话万分感激 #include <cstdio> #includ ...

  3. AC日记——【模板】Link Cut Tree 洛谷 P3690

    [模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ...

  4. LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

    P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...

  5. LG3690 【模板】Link Cut Tree (动态树)

    题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...

  6. LG3690 【模板】Link Cut Tree 和 SDOI2008 洞穴勘测

    UPD:更新了写法. [模板]Link Cut Tree 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 后接两个整数(x,y),代表询问从x到y ...

  7. 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)

    题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...

  8. 洛谷P3690 Link Cut Tree (模板)

    Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...

  9. luogu P3690 【模板】Link Cut Tree (动态树)

    嘟嘟嘟 LCT竟然看了整整一天,但好歹是看懂了. 教程这里不写,强烈推荐 闪狐大佬的博客 . 但是还是有几句想说的. 1.尽管LCT和splay很像,但是有一些细节还是不一样的.首先是rotate,我 ...

随机推荐

  1. oracle巡检脚本备份

    重做日志生成情况,一天生成日志大小:select round(sum(blocks*block_size)/1024/1024/1024,2) BLOCK from v\$archived_log w ...

  2. 解决maven编译Java中的使用了未经检查或不安全的操作

    eclipse编译器找到右侧黄色点击可以定位到对应代码块位置 解决方法:鼠标悬浮在上方点击add即可 @SuppressWarnings("unchecked");给出的解决方案 ...

  3. Android动画分类

    动画分类 View动画(补间动画).帧动画.属性动画 View动画(补间动画)包括:平移.旋转.缩放.透明度,View动画是一种渐近式动画 帧动画:图片切换动画 属性动画:通过动态改变对象的属性达到动 ...

  4. 轻量架构ShuffleNet V2:从理论复杂度到实用设计准则

    转自:机器之心 近日,旷视科技提出针对移动端深度学习的第二代卷积神经网络 ShuffleNet V2.研究者指出过去在网络架构设计上仅注重间接指标 FLOPs 的不足,并提出两个基本原则和四项准则来指 ...

  5. 1、Appium安装

    1.安装node.js 访问node js官网 https://nodejs.org/en/ 下载并安装node js,找到你系统适合的node js一步步安装即可 2.安装Appium 在cmd中执 ...

  6. hdu1561 树形dp,依赖背包

    多重背包是某个物品可以选择多次,要把对物品数的枚举放在对w枚举外面 分组背包是某组的物品只能选一个,要把对每组物品的枚举放在对w枚举内侧 依赖背包是多层的分组背包,利用树形结构建立依赖关系,每个结点都 ...

  7. bootstrap之表格和按钮

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. python 防死锁机制

    https://www.cnblogs.com/wongbingming/p/9035575.html ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 在编写多线程程序时,可能无意中就会写 ...

  9. Nginx 响应状态

    ngx.status = ngx.HTTP_CONTINUE (100) (first added in the v0.9.20 release)ngx.status = ngx.HTTP_SWITC ...

  10. java:大小写字母转换

    public class Solution { public static void main(String args[]) { testSolutions.lowercaseToUppercase( ...