【模板】动态树(Link Cut Tree)
\(\text{Code}\)
#include <cstdio>
#include <iostream>
#define IN inline
#define RE register
using namespace std;
const int N = 1e5 + 5;
int n, m;
struct LCT{
int st[N], c[N][2], fa[N], v[N], s[N], tg[N];
IN void pushup(int x){s[x] = s[c[x][0]] ^ s[c[x][1]] ^ v[x];}
IN void reverse(int x){tg[x] ^= 1, swap(c[x][0], c[x][1]);}
IN void pushdown(int x)
{
if (!tg[x]) return; tg[x] = 0;
if (c[x][0]) reverse(c[x][0]);
if (c[x][1]) reverse(c[x][1]);
}
IN int isroot(int x){return c[fa[x]][0] != x && c[fa[x]][1] != x;}
IN void rotate(int x)
{
int y = fa[x], z = fa[y], k = (c[y][1] == x), w = c[x][!k];
if (!isroot(y)) c[z][c[z][1] == y] = x; c[x][!k] = y, c[y][k] = w;
if (w) fa[w] = y; fa[y] = x, fa[x] = z, pushup(y);
}
IN void splay(int x)
{
int y = x, z = 0; st[++z] = y;
while (!isroot(y)) st[++z] = y = fa[y];
while (z) pushdown(st[z--]);
while (!isroot(x)){y=fa[x],z=fa[y]; if (!isroot(y)) rotate((c[y][0]==x)^(c[z][0]==y)?x:y); rotate(x);}
pushup(x);
}
IN void access(int x){for(RE int y=0; x; y=x, x=fa[x]) splay(x), c[x][1] = y, pushup(x);}
IN void makeroot(int x){access(x), splay(x), reverse(x);}
IN int findroot(int x){access(x), splay(x); while (c[x][0]) pushdown(x),x=c[x][0]; splay(x); return x;}
IN void split(int x, int y){makeroot(x), access(y), splay(y);}
IN void link(int x, int y){makeroot(x); if (findroot(y)^x) fa[x] = y;}
IN void cut(int x, int y){makeroot(x); if (findroot(y)==x&&fa[y]==x&&!c[y][0]) fa[y]=c[x][1]=0,pushup(x);}
}T;
int main()
{
scanf("%d%d", &n, &m);
for(RE int i = 1; i <= n; i++) scanf("%d", &T.v[i]);
for(int op, x, y; m; --m)
{
scanf("%d%d%d", &op, &x, &y);
if (op == 0) T.split(x, y), printf("%d\n", T.s[y]);
else if (op == 1) T.link(x, y);
else if (op == 2) T.cut(x, y);
else T.splay(x), T.v[x] = y, T.pushup(x);
}
}
【模板】动态树(Link Cut Tree)的更多相关文章
- 动态树(Link Cut Tree) :SPOJ 375 Query on a tree
QTREE - Query on a tree #number-theory You are given a tree (an acyclic undirected connected graph) ...
- 【模板篇】Link Cut Tree模板(指针)
网上一片一片的LCT都是数组写的 orz 用指针写splay的人想用指针写LCT找板子都不好找QAQ 所以能A题了之后自然要来回报社会, 把自己的板子丢上来(然而根本没有人会看) LCT讲解就省省吧, ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)
题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...
- LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板
P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...
- LG3690 【模板】Link Cut Tree (动态树)
题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...
- 洛谷P3690 [模板] Link Cut Tree [LCT]
题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...
- AC日记——【模板】Link Cut Tree 洛谷 P3690
[模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ...
- LG3690 【模板】Link Cut Tree 和 SDOI2008 洞穴勘测
UPD:更新了写法. [模板]Link Cut Tree 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 后接两个整数(x,y),代表询问从x到y ...
随机推荐
- 乐维监控与Zabbix对比分析(一)——架构、性能
近年来,Zabbix凭借其近乎无所不能的监控及优越的性能一路高歌猛进,在开源监控领域独占鳌头:而作为后起的新锐监控平台--乐维监控,则不断吸收Zabbix,Prometheus等优秀开源平台的优点,兼 ...
- org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException: The supplied data appears to be in the OLE2 Format. You are calling the part of POI that deals with OOXML (Office Open XML) Documents
异常:org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException: The supplied data appears to b ...
- JAVA里Map的一些常用方法
Map的常用方法 案例1 场景:一张建行用户体验金信息大表(百万级别),里面存在一个字段对多条数据,需要统计某个字段的多条数据累加值以供于别的服务调用. 优化前解决:直接查出来一个大list给到另一个 ...
- Hexo博客搭建和简单部署
title: Hexo博客搭建和简单部署 date: 2020-03-02 12:00:00 categories: - [IT,博客] - [IT,软件,程序] - [IT,软件,搭建与配置] ta ...
- TypeError: __str__ returned non-string (type WebStepInfo)
错误代码: class CaseStep(models.Model): id = models.AutoField(primary_key=True) casetep = models.Foreign ...
- 学习.NET MAUI Blazor(一)、Blazor是个啥?
先把Blazor放一边,先来看看目前Web开发的技术栈. 注:上图只是为了说明问题,没有任何语言歧视! 这是目前最常用的前后端分离开发模式,这个开发模式需要配备前端工程师和后端工程师.当然了,全栈工程 ...
- Docker 搭建 Wordpress 个人博客
Docker安装 更新软件库(可选),将所用到的yum软件更新到最新 yum -y update docker一键安装命令: curl -fsSL https://get.docker.com | b ...
- SQL Server登录初次提示状态码233,再次登录提示状态码18456
解决方案: 1.使用windows方式登录数据库,修改安全性属性为SQL Server 和Windows身份验证模式 2.打开SQL Server配置管理器,启动MSSQLSERVER协议 3.修改s ...
- 一份前端够用的 Linux 命令
前言 你好,我是悦创.我用 VuePress 搭建博客,又实现了 GitHub 和 Gitee Pages 的自动部署,但我最终还是决定自己建站,而在建站的过程中,必不可少会用到 Linux 命令,所 ...
- [LeetCode]数组拆分 I
题目 代码 class Solution { public: int arrayPairSum(vector<int>& nums) { std::sort(nums.begin( ...