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 ...
随机推荐
- [转帖]linux常用命令大全(linux基础命令入门到精通+实例讲解+持续更新+命令备忘录+面试复习)
linux常用命令大全(linux基础命令入门到精通+实例讲解+持续更新+命令备忘录+面试复习) https://www.cnblogs.com/caozy/p/9261224.html 总结的挺好的 ...
- 前后端分离,如何防止api接口被恶意调用或攻击
无论网站,还是App目前基本都是基于api接口模式的开发,那么api的安全就尤为重要了.目前攻击最常见的就是“短信轰炸机”,由于短信接口验证是App,网站检验用户手机号最真实的途径,使用短信验证码在提 ...
- Oracle的varchar2如何比较大小
首先要说的是Oracle中字符类型的比较都是基于ASCII码表来实现的,我就简单做个总结. Oracle中varchar2类型的字符串使用的是非填充空格的标准来进行比较的(表格中右边的那列,注意空格的 ...
- (九)Javabean与Jsp(来自那些年的笔记)
目录 JavaBean 在JSP中使用JavaBean 标签用法 带标签体的 JavaBean 标签 setProperty 标签 getProperty 标签 JSP开发模式 案列:使用 模式一 编 ...
- html5 外链式实现加减乘除
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 使用内存地址点亮LED—跟51单片机一样写代码教学(初步入门)
51单片机点亮一个小灯 #include <rge52.h> sbit LED = P0^ void main(void) { P0 = 0XFE; // 总线操作 sfr P0 0X80 ...
- vi 使用系统剪贴板(clipboard)
ref : https://www.jianshu.com/p/771b95e34293 http://www.bubuko.com/infodetail-469867.html 在vi中,如果编译时 ...
- C# EF添加ADO.NET实体数据模型时,产生.Desiger.cs文件为空
// T4 code generation is enabled for model 'D:\DKX4003\work\VWFC_CCS\SrcCCG-branch\CCGSPBOCOne-FCA\C ...
- vue 集成jTopo 处理方法
jTopo 帮助说明网站 http://www.jtopo.com/index.html 使用例子: http://www.jtopo.com/demo/helloworld.html 不建议直接安装 ...
- ant Windows下环境变量配置 安装 编译
下载 官网:[http://ant.apache.org/] 其他版本:[http://archive.apache.org/dist/ant/binaries/] 点击这个进入下载页面 Window ...