洛谷P3690 (动态树模板)
一位大佬写的代码。(加上我自己的一些习惯性写法)
存个模板。
1 #include<bits/stdc++.h>
2 using namespace std;
3 const int N=1e5+5;
4 int n,m,a[N],w[N];
5 struct node{
6 int fa,lc,rc,rv;
7 #define lc(x) t[x].lc
8 #define rc(x) t[x].rc
9 #define fa(x) t[x].fa
10 #define rv(x) t[x].rv
11 }t[N];
12
13 void up(int x){
14 w[x]=a[x]^w[lc(x)]^w[rc(x)];
15 }
16
17 bool isr(int x)//isroot
18 {
19 return lc(fa(x))!=x&&rc(fa(x))!=x;
20 }
21 bool wrt(int x){
22 return rc(fa(x))==x;
23 }
24
25 void rev(int x){//做标记
26 if(!x) return;
27 swap(lc(x),rc(x));
28 rv(x)^=1;
29 }
30
31 void down(int x){//下传标记
32 if(rv(x)){
33 rev(lc(x)),rev(rc(x));
34 }
35 rv(x)=0;
36 }
37
38 void rot(int x){//旋转
39 int y=fa(x),z=fa(y),b=(lc(y)==x)?rc(x):lc(x);
40 if(z&&!isr(y)) (lc(z)==y?lc(z):rc(z))=x;
41 fa(x)=z,fa(y)=x;
42 if(b) fa(b)=y;
43 if(lc(y)==x) rc(x)=y,lc(y)=b;
44 else lc(x)=y,rc(y)=b;
45 up(y),up(x);
46 }
47
48 void path(int x){//根到节点上的点都下传标记
49 if(!isr(x)) path(fa(x));
50 down(x);
51 }
52
53 void spl(int x){//splay
54 path(x);
55 while(!isr(x)){
56 if(!isr(fa(x))) wrt(x)==wrt(fa(x))?rot(fa(x)):rot(x);
57 rot(x);
58 }
59 }
60
61 void acs(int x){
62 for(int y=0;x;y=x,x=fa(x)){
63 spl(x),rc(x)=y,up(x);
64 }
65 }
66
67 void mrt(int x){//makeroot
68 acs(x),spl(x),rev(x);
69 }
70
71 int fnd(int x){//findroot
72 acs(x),spl(x),down(x);
73 while(lc(x)) x=lc(x),down(x);
74 spl(x);return x;
75 }
76
77 void lk(int x,int y){//link
78 mrt(x);
79 if(fnd(y)==x) return ;
80 fa(x)=y;
81 }
82
83 void cut(int x,int y){//删边
84 mrt(x),acs(y),spl(y);
85 if(lc(y)!=x) return ;
86 lc(y)=fa(x)=0;
87 }
88
89 int main(){
90 scanf("%d%d",&n,&m);
91 for(int i=1;i<=n;i++){
92 scanf("%d",&a[i]),w[i]=a[i];
93 }
94 for(int i=1;i<=m;i++){
95 int o,x,y;
96 scanf("%d%d%d",&o,&x,&y);
97 if(o==0){
98 mrt(x),acs(y),spl(y);//提取x到y的路径
99 cout<<w[y]<<endl;
100 }
101 if(o==1) lk(x,y);
102 if(o==2) cut(x,y);
103 if(o==3) acs(x),spl(x),a[x]=y,up(x);
104 }
105 return 0;
106 }
洛谷P3690 (动态树模板)的更多相关文章
- 洛谷P3372线段树模板1——线段树
题目:https://www.luogu.org/problemnew/show/P3372 线段树模板. 代码如下: #include<iostream> #include<cst ...
- 【洛谷 P3690】 【模板】Link Cut Tree (动态树)
题目链接 \(RT\). FlashHu巨佬的博客 #include <cstdio> #define R register int #define I inline void #defi ...
- 洛谷P3373线段树模板2
题目:https://www.luogu.org/problemnew/show/P3373 带乘的线段树,更新时把加的标记也乘一下,然后取值时先乘后加. 代码如下: #include<iost ...
- 洛谷3372线段树模板题 对区间+k或者查询区间和
#include<bits/stdc++.h> using namespace std; typedef unsigned int ui; typedef long long ll; ty ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)
题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...
- 【洛谷P3369】【模板】普通平衡树题解
[洛谷P3369][模板]普通平衡树题解 题目链接 题意: 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3 ...
- 洛谷1087 FBI树 解题报告
洛谷1087 FBI树 本题地址:http://www.luogu.org/problem/show?pid=1087 题目描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全 ...
- 洛谷P3018 [USACO11MAR]树装饰Tree Decoration
洛谷P3018 [USACO11MAR]树装饰Tree Decoration树形DP 因为要求最小,我们就贪心地用每个子树中的最小cost来支付就行了 #include <bits/stdc++ ...
- 洛谷P3690 [模板] Link Cut Tree [LCT]
题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...
随机推荐
- 2539-SpringSecurity系列--在有安全验证的情况下做单元测试Test
在有安全验证的情况下做单元测试Test 版本信息 <parent> <groupId>org.springframework.boot</groupId> < ...
- 金瓯无缺江河一统|Win10系统基于Docker和Python3搭建并维护统一认证系统OpenLdap
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_180 OpenLdap(Lightweight Directory Access Protocol)是什么?它其实是一个开源的.具 ...
- 解决 Vue 部署在域名子路由 问题
我们先看下官方说明 默认情况下,Vue CLI 会假设你的应用是被部署在一个域名的根路径上,例如 https://www.my-app.com/ .如果应用被部署在一个子路径上,你就需要用这个选项指定 ...
- odoo 14 python 单元测试步骤
# odoo 14 python 单元测试步骤 # 一.在模块根目录创建tests目录 # 二.在tests目录下创建__init__.py文件 # 三.继承TransactionCase(Singl ...
- LuoguP5322 [BJOI2019]排兵布阵(DP)
城为物,人为容,价值?排序后,一切都明了 #include <iostream> #include <cstdio> #include <cstring> #inc ...
- Luogu1879 [USACO06NOV]玉米田Corn Fields (状压DP)
曾经放弃的坑,都是坑 #include <iostream> #include <cstdio> #include <cstring> #include <a ...
- React报错之Expected `onClick` listener to be a function
正文从这开始~ 总览 当我们为元素的onClick属性传递一个值,但是该值却不是函数时,会产生"Expected onClick listener to be a function" ...
- 744. 寻找比目标字母大的最小字母--LeetCode
来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/find-smallest-letter-greater-than-target 著作权归领扣网络所有. ...
- Excel 查找函数(一):LOOKUP
序号 员工姓名 部门 职务 1 苏霞 法务部 法律顾问 2 包志林 财务部 财务总监 3 林娥云 安监部 部长 4 石少卿 质检部 质检员 5 于炳福 生产部 生产部 6 蒋琼志 仓储部 保管员 7 ...
- 历时2月,动态线程池 DynamicTp 发布里程碑版本 V1.0.8
关于 DynamicTp DynamicTp 是一个基于配置中心实现的轻量级动态线程池管理工具,主要功能可以总结为动态调参.通知报警.运行监控.三方包线程池管理等几大类. 经过多个版本迭代,目前最新版 ...