洛谷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:后接两 ...
随机推荐
- 一步一步学习JNI
本文来自网易云社区 作者:孙有军 前言 本篇的主要目的就是JNI开发入门,使大家对JNI开发流程有一个大致的了解,后续再进行深入学习. JNI不是Android特有的,JNI是Java Native ...
- 关于 NPOI 导出的 Excel 出现“部分内容有问题” 的解决方法
近期发现使用 NPOI 导出的 Excel 文件,有部分用户反映在打开时报错,测试了一下,发现在低版本的 Office 中(2003版,配合2007格式兼容包)打开正常,但在高版本 Office 中, ...
- 下载Web微信视频
1. 用浏览器(我用Chrome)登录web微信(wx.qq.com) 2. 这个时候如果有人发视频,可以点开播放.用F12打开chrome的调试平台,查看视频源的URL(绿色框的src内容) 3. ...
- Siki_Unity_3-13_编程内功修炼-算法
Unity 3-13 编程内功修炼 -- 算法 任务1&2:课程介绍 主要算法: 分治法 堆排序 二叉树 动态规划 贪心算法 图 任务3:分治算法 -- Divide and Conquer ...
- CSP201609-1:最大波动
引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...
- Map Reduce Application(Join)
We are going to explain how join works in MR , we will focus on reduce side join and map side join. ...
- leetcode个人题解——#8 string to integer
第八题 class Solution { public: int myAtoi(string str) { ; ; ; while(str[i] == ' ')i++; if (str[i] == ' ...
- Python3 小工具-TCP发现
from scapy.all import * import optparse import threading import os def scan(ip): pkt=IP(dst=ip)/TCP( ...
- Python3 数据类型-元组
Python 的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 实例1(Python3.0+): tu ...
- canvas学习(一):线条,图像变换和状态保存
canvas学习(一):线条,图像变换和状态保存 一:绘制一条线段: var canvas = document.getElementById('canvas') var ctx = canvas.g ...