洛谷3690:【模板】Link Cut Tree——题解
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——题解的更多相关文章
- 洛谷.3690.[模板]Link Cut Tree(动态树)
题目链接 LCT(良心总结) #include <cstdio> #include <cctype> #include <algorithm> #define gc ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- 洛谷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是联 ...
- 洛谷 P2633 Count on a tree 题解
题面 对于每个点建立一颗主席树: 然后按照树上差分的思想统计主席树的前缀和: lca+主席树+前向星存表就可以了: #include <bits/stdc++.h> #define inc ...
- 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)
题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...
- AC日记——【模板】Link Cut Tree 洛谷 P3690
[模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ...
- 洛谷P3690 Link Cut Tree (模板)
Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...
- LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板
P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...
随机推荐
- redhat防火墙管理
systemctl status firewalldsystemctl stop firewalldsystemctl start firewalldsystemctl enable firewall ...
- WeTest功能优化第2期:云真机智能投屏,调试告别鼠标
第2期功能优化目录 [云真机视频映射]云真机画面本地映射[兼容性测试报告]新增问题机型聚类功能[新增Android9.0]同步上线最新安卓系统 本期介绍的云测产品功能优化,既有重磅级技术突破,也有报告 ...
- Selenium 入门到精通系列:五
Selenium 入门到精通系列 PS:显式等待.隐式等待.强制等待方法 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019 ...
- maven 教程二 深入
一:编写POM <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...
- HDU 1403 Longest Common Substring(后缀自动机——附讲解 or 后缀数组)
Description Given two strings, you have to tell the length of the Longest Common Substring of them. ...
- Prime Matrix(暴力出奇迹)
Description You've got an n × m matrix. The matrix consists of integers. In one move, you can apply ...
- Thunder团队第六周 - Scrum会议6
Scrum会议6 小组名称:Thunder 项目名称:i阅app Scrum Master:邹双黛 工作照片: 邹双黛同学在拍照,所以不在照片内. 参会成员: 王航:http://www.cnblog ...
- android平台蓝牙编程(转)
http://blog.csdn.net/pwei007/article/details/6015907 Android平台支持蓝牙网络协议栈,实现蓝牙设备之间数据的无线传输. 本文档描述了怎样利用a ...
- Spring中Controller和RequestMapping的详解
先看一个简单的实例: @Controller @RequestMapping("/hello") public class anyTypeController{ @RequestM ...
- win7 64位在线编辑dsoframer控件的安装和使用配置
经历了两天的折磨,查阅了网上的资料,按网上的操作试了n种方法结果还是不行,开始以为是dsoframer 是32位控件问题,结果不是(经历了更改解决方案cpu,发布基于x86的网站:以为是操作系统问题, ...