Luogu P3690【模板】Link Cut Tree (LCT板题)
省选前刷道LCT板题(话说之前没做这道题…)
CODE
#include<bits/stdc++.h>
using namespace std;
inline void read(int &num) {
char ch; int flg = 1; while(!isdigit(ch=getchar()))if(ch=='-')flg = -flg;
for(num=0; isdigit(ch); num=num*10+ch-'0', ch=getchar()); num*=flg;
}
const int MAXN = 100005;
const int mod = 51061;
namespace LCT {
#define ls ch[x][0]
#define rs ch[x][1]
int ch[MAXN][2], fa[MAXN], w[MAXN], sum[MAXN];
bool rev[MAXN];
inline bool isr(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
inline bool get(int x) { return ch[fa[x]][1] == x; }
inline void upd(int x) { sum[x] = sum[ls] ^ sum[rs] ^ w[x]; }
inline void mt(int x) { if(rev[x]) rev[ls]^=1, rev[rs]^=1, rev[x]^=1, swap(ls, rs); }
void mtpath(int x) { if(!isr(x)) mtpath(fa[x]); mt(x); }
inline void rot(int x) {
int y = fa[x], z = fa[y]; bool l = get(x), r = l^1;
if(!isr(y)) ch[z][get(y)] = x;
fa[ch[x][r]] = y; fa[y] = x; fa[x] = z;
ch[y][l] = ch[x][r]; ch[x][r] = y;
upd(y), upd(x);
}
inline void splay(int x) {
mtpath(x);
for(; !isr(x); rot(x))
if(!isr(fa[x])) rot(get(fa[x])==get(x)?fa[x]:x);
}
inline int access(int x) { int y = 0;
for(; x; x=fa[y=x]) splay(x), ch[x][1] = y, upd(x);
return y;
}
inline void bert(int x) { access(x), splay(x), rev[x]^=1; }
inline int sert(int x) { access(x), splay(x); int y = x; for(; ch[x][0]; x=ch[x][0]); splay(x), splay(y); return x; }
inline void Link(int x, int y) { bert(x); if(sert(y) != x) fa[x] = y; }
inline void Cut(int x, int y) { bert(x); if(sert(y) == x && fa[x] == y && !ch[x][1]) fa[x] = ch[y][0] = 0, upd(y); }
inline int split(int x, int y) { bert(x), access(y), splay(y); return y; }
inline void Modify(int x, int val) { bert(x), w[x] = val, upd(x); }
inline int Query(int x, int y) { return sum[split(x, y)]; }
#undef ls
#undef rs
}
using namespace LCT;
int n, q;
int main() {
//freopen("sample.txt", "r", stdin);
read(n), read(q);
for(int i = 1; i <= n; ++i)
read(w[i]), sum[i] = w[i];
int op, x, y;
while(q--) {
read(op), read(x), read(y);
switch(op) {
case 0: printf("%d\n", Query(x, y)); break;
case 1: Link(x, y); break;
case 2: Cut(x, y); break;
case 3: Modify(x, y); break;
}
}
}
Luogu P3690【模板】Link Cut Tree (LCT板题)的更多相关文章
- 洛谷P3690 [模板] Link Cut Tree [LCT]
题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- BZOJ 3282 Link Cut Tree (LCT)
题目大意:维护一个森林,支持边的断,连,修改某个点的权值,求树链所有点点权的异或和 洛谷P3690传送门 搞了一个下午终于明白了LCT的原理 #include <cstdio> #incl ...
- 模板Link Cut Tree (动态树)
题目描述 给定N个点以及每个点的权值,要你处理接下来的M个操作.操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联 ...
- 洛谷.3690.[模板]Link Cut Tree(动态树)
题目链接 LCT(良心总结) #include <cstdio> #include <cctype> #include <algorithm> #define gc ...
- 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)
题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...
- Luogu 3690 Link Cut Tree
Luogu 3690 Link Cut Tree \(LCT\) 模板题.可以参考讲解和这份码风(个人认为)良好的代码. 注意用 \(set\) 来维护实际图中两点是否有直接连边,否则无脑 \(Lin ...
- (RE) luogu P3690 【模板】Link Cut Tree
二次联通门 : luogu P3690 [模板]Link Cut Tree 莫名RE第8个点....如果有dalao帮忙查错的话万分感激 #include <cstdio> #includ ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
随机推荐
- [转帖]8个优秀Docker容器监控工具,收藏了
8个优秀Docker容器监控工具,收藏了 Docker是目前使用最广泛的容器之一,但它并不总是像物理硬件一样可见.而使用docker容器监控工具有助于消除这种透明度的缺失.以下介绍8种优秀Docker ...
- python 脚本备份mssql数据库并删除数据库
一.实现脚本 # -*- coding=utf-8 -*- import pyodbc from datetime import datetime import pymssql import os i ...
- Mathtype 问题汇总(3)
1. 调整行间距 2. 调整字号 在 MathType 中,选择「大小 > 定义」将对话框中「完整」所对应的值改为和文字大小匹配的pt(磅值),这样便可以解决在文字编写的 Word 文档中某一行 ...
- 开源定时任务框架Quartz(一)
简介 Quartz是OpenSymphony开源组织的一个开源项目,定时任务框架,纯Java语言实现,最新版本为2.3.0. 设计模式 Quartz中使用的设计模式如下: 1.Builder模式 2. ...
- js时间戳与日期格式之间相互转换
###js时间戳与日期格式之间相互转换 将时间戳转换成日期格式 // 简单的一句代码 var date = new Date(时间戳); //获取一个时间对象 /** 1. 下面是获取时间日期的方法, ...
- wordpress 后台添加 快速编辑 栏目
前两篇其实是同一篇,都是讲在后台添加菜单类型的http://www.ashuwp.com/courses/highgrade/664.htmlhttps://shibashake.com/wordpr ...
- Unity中的Character Controller
Unity中默认提供了一个Character Controller的组件用于实现角色控制,一个3D的游戏物体,可以直接添加.Character Controller会自动模拟出Capsule Coll ...
- android 项目集成 微信支付
0.环境 下载 libammsdk.jar 1.需要的常量值 public class Constant { /** 微信中用到的常量值 */ public static final class WX ...
- Java RadixSort
Java RadixSort /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternational ...
- Form key length limit 2048 exceeded ,提交数据时,数据的键过长 或者是上传文件过大
在ASP.NET Core MVC中,文件的key 默认最大为2048,文件上传的最大上传文件默认为20MB,如果我们想上传一些比较大的文件,就不知道怎么去设置了,没有了Web.Config我们应该如 ...