https://www.luogu.org/problemnew/show/P3690

给定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。

模板题就不说什么了。

我们多维护一个节点的key值(权值)和val值(splay中子树的异或和),每次update即可。

对于3操作我们把该节点access然后splay,更新key之后update即可。

剩下就是LCT基本操作。

UPT:18.4.2更新:

数据被加强了,多了删掉一个不存在的边导致的bug。

所以对于cut函数要多处理一遍,具体的处理方法就是打通x和y,这样x作为重链末端没有儿子,x的爸爸为y,y只有x一个儿子。

凡是不符合的即为没有该边。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cctype>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
const int N=3e5+;
int n,m,r,fa[N],tr[N][],rev[N],q[N],key[N],val[N];
inline int read(){
int X=,w=;char ch=;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=(X<<)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
inline bool get(int x){
return tr[fa[x]][]==x;
}
inline bool isroot(int x){
if(!fa[x])return ;
return tr[fa[x]][]!=x&&tr[fa[x]][]!=x;
}
inline void upt(int x){
int ans=;
if(tr[x][])ans^=val[tr[x][]];
if(tr[x][])ans^=val[tr[x][]];
val[x]=key[x]^ans;
}
inline void pushrev(int x){
if(!rev[x])return;
swap(tr[x][],tr[x][]);
if(tr[x][])rev[tr[x][]]^=;
if(tr[x][])rev[tr[x][]]^=;
rev[x]=;
}
inline void rotate(int x){
int y=fa[x],z=fa[y],which=get(x);
if(z&&!isroot(y))tr[z][tr[z][]==y]=x;
tr[y][which]=tr[x][which^];fa[tr[y][which]]=y;
fa[y]=x;tr[x][which^]=y;fa[x]=z;
upt(y);upt(x);
}
inline void splay(int x){
q[r=]=x;
for(int y=x;!isroot(y);y=fa[y])q[++r]=fa[y];
for(int i=r;i>=;i--)pushrev(q[i]);
while(!isroot(x)){
if(!isroot(fa[x]))
rotate((get(x)==get(fa[x])?fa[x]:x));
rotate(x);
}
upt(x);
}
inline void access(int x){
for(int y=;x;y=x,x=fa[x]){
splay(x);tr[x][]=y;
if(y)fa[y]=x;
}
}
inline int findroot(int x){
access(x);splay(x);
while(pushrev(x),tr[x][])x=tr[x][];
splay(x);
return x;
}
inline void makeroot(int x){
access(x);splay(x);
rev[x]^=;
}
inline void link(int x,int y){
makeroot(x);fa[x]=y;
}
inline void cut(int x,int y){
makeroot(x);
access(y);splay(y);
if(tr[x][]||tr[x][]||fa[x]!=y||tr[y][get(x)^])return;
tr[y][]=;fa[x]=;
}
inline void split(int u,int v){
makeroot(u);access(v);splay(v);
}
int main(){
n=read(),m=read();
for(int i=;i<=n;i++)key[i]=read();
for(int i=;i<=m;i++){
int op=read(),u=read(),v=read();
if(op==){
split(u,v);
printf("%d\n",val[v]);
}
if(op==){
if(findroot(u)!=findroot(v))link(u,v);
}
if(op==){
if(findroot(u)==findroot(v))cut(u,v);
}
if(op==){
access(u);splay(u);
key[u]=v;upt(u);
}
}
return ;
}

+++++++++++++++++++++++++++++++++++++++++++

+本文作者:luyouqi233。               +

+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+

+++++++++++++++++++++++++++++++++++++++++++

洛谷3690:【模板】Link Cut Tree——题解的更多相关文章

  1. 洛谷.3690.[模板]Link Cut Tree(动态树)

    题目链接 LCT(良心总结) #include <cstdio> #include <cctype> #include <algorithm> #define gc ...

  2. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  3. 洛谷P3690 [模板] Link Cut Tree [LCT]

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

  4. 模板Link Cut Tree (动态树)

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

  5. 洛谷 P2633 Count on a tree 题解

    题面 对于每个点建立一颗主席树: 然后按照树上差分的思想统计主席树的前缀和: lca+主席树+前向星存表就可以了: #include <bits/stdc++.h> #define inc ...

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

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

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

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

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

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

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

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

随机推荐

  1. 第十五届北京师范大学程序设计竞赛现场决赛题解&源码(A.思维,C,模拟,水,坑,E,几何,思维,K,字符串处理)

    #include <bits/stdc++.h> using namespace std; int main() { int T,n,a,b; while(cin>>T) { ...

  2. Shader-水流效果

    效果图:(贴图类似于泥石流) 代码: Shader "CookbookShaders/Chapter02/ScrollingUVs" { Properties { _MainTin ...

  3. python 终极篇 --- django 视图系统

    Django的View(视图) 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误, ...

  4. lintcode 二叉树中序遍历

    /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * Tr ...

  5. SSH:远程登陆

    SSH用于计算机之间的加密登录的前提是公钥为真,所以存在中间人攻击中间人攻击:与https协议不同,SSH协议的公钥是没有CA公证的,当对公钥的请求被中间截获时,中间人可以发出伪造公钥干坏事而不被识破 ...

  6. [2018 ACL Long] 对话系统

    [NLG - E2E - knowledge guide generation] 1. Knowledge Diffusion for Neural Dialogue Generation ( ‎Ci ...

  7. Python3 深浅拷贝

    一 定义 在Python中对象的赋值其实就是对象的引用.当创建一个对象,把它赋值给另一个变量的时候,python并没有拷贝这个对象,只是拷贝了这个对象的引用而已. 浅拷贝: 浅拷贝值只拷贝一层,具有自 ...

  8. 20145214 《Java程序设计》第10周学习总结

    20145214 <Java程序设计>第10周学习总结 学习内容总结 计算机网络概述 在计算机网络中,现在命名IP地址的规定是IPv4协议,该协议规定每个IP地址由4个0-255之间的数字 ...

  9. android AndroidManifest.xml uses-feature 详解

    如果你是一个Android用户,而且你有一个老旧的安装有android 1.5 的android设备,你可 能会注意到一些高版本的应用没有在手机上的Android Market 中显示.这必定是应用使 ...

  10. <Android>对话框的使用

    Android系统提供四种对话框:警告对话框(AlertDialog),进度对话框(ProgressDialog),日期选择对话框(DatePickerDialog)和时间选择对话框(TimePick ...