P3690 【模板】Link Cut Tree (动态树)

题目背景

动态树

题目描述

给定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。

输入输出格式

输入格式:

第1行两个整数,分别为n和m,代表点数和操作数。

第2行到第n+1行,每行一个整数,整数在[1,10^9]内,代表每个点的权值。

第n+2行到第n+m+1行,每行三个整数,分别代表操作类型和操作所需的量。

输出格式:

对于每一个0号操作,你须输出x到y的路径上点权的xor和。

输入输出样例

输入样例/#1:

3 3

1

2

3

1 1 2

0 1 2

0 1 1

输出样例/#1:

3

1

说明

数据范围: \(1\ \leq\ N\ \leq\ 3\ \times\ 10^5\)

LCT模板题。洛谷上有点卡常。。神TM插入的时候要按顺序插。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <cmath>
#include <sstream>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
template<class T>
inline void swap(T &a, T &b)
{
T tmp = a;a = b;b = tmp;
}
inline void read(int &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const int INF = 0x3f3f3f3f;
const int MAXN = 300000 + 10;
int ch[MAXN][2], fa[MAXN], data[MAXN], sum[MAXN], lazy[MAXN];
inline void pushup(int x){sum[x] = data[x] ^ sum[ch[x][0]] ^ sum[ch[x][1]];}
inline int son(int x){return x == ch[fa[x]][1];}
inline void rever(int x){lazy[x] ^= 1, swap(ch[x][0], ch[x][1]);}
inline void pushdown(int x){if(lazy[x])lazy[x] = 0, rever(ch[x][0]), rever(ch[x][1]);}
inline int isroot(int x){return ch[fa[x]][1] != x && ch[fa[x]][0] != x;}
void dfs(int x){if(x)dfs(fa[x]),pushdown(x);}
void rotate(int x)
{
int y = fa[x], z = fa[y], b = son(x), c = son(y), a = ch[x][!b];
if(!isroot(y) && z) ch[z][c] = x; fa[x] = z;
if(a) fa[a] = y;ch[y][b] = a;
ch[x][!b] = y, fa[y] = x;
pushup(y), pushup(x);
}
void splay(int x)
{
dfs(x);
while(!isroot(x))
{
int y = fa[x], z = fa[y];
if(!isroot(y))
if(son(x) == son(y)) rotate(y);
else rotate(x);
rotate(x);
}
}
inline void access(int x){for(int y = 0;x;y = x, x = fa[x]) splay(x), ch[x][1] = y,pushup(x);}
inline void makeroot(int x){access(x), splay(x), rever(x);}
inline int findroot(int x){access(x), splay(x);while(ch[x][0])x = ch[x][0];splay(x);return x;}
inline void link(int x, int y){makeroot(y), fa[y] = x;}
inline void path(int x, int y){makeroot(x), access(y), splay(y);}
inline void cut(int x, int y){path(x, y);if(ch[y][0] == x) fa[x] = 0, ch[y][0] = 0;pushup(y);}
int n,m,tmp1,tmp2,tmp3;
int main()
{
read(n), read(m);
register int i;
for(i = 1;i + 3 <= n;i += 4)
{
read(data[i]), sum[i] = data[i];
read(data[i + 1]), sum[i + 1] = data[i + 1];
read(data[i + 2]), sum[i + 2] = data[i + 2];
read(data[i + 3]), sum[i + 3] = data[i + 3];
}
for(;i <= n;++ i) read(data[i]), sum[i] = data[i];
for(int i = 1;i <= m;++ i)
{
read(tmp1), read(tmp2), read(tmp3);
if(tmp1 == 0) path(tmp2, tmp3), printf("%d\n", sum[tmp3]);
else if(tmp1 == 1)
{
int f1 = findroot(tmp2), f2 = findroot(tmp3);
if(f1 != f2) link(tmp2, tmp3);
}
else if(tmp1 == 2)
{
int f1 = findroot(tmp2), f2 = findroot(tmp3);
if(f1 == f2) cut(tmp2, tmp3);
}
else access(tmp2), splay(tmp2), data[tmp2] = tmp3, pushup(tmp2);
}
return 0;
}

LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板的更多相关文章

  1. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  2. 洛谷.3690.[模板]Link Cut Tree(动态树)

    题目链接 LCT(良心总结) #include <cstdio> #include <cctype> #include <algorithm> #define gc ...

  3. LCT(link cut tree) 动态树

    模板参考:https://blog.csdn.net/saramanda/article/details/55253627 综合各位大大博客后整理的模板: #include<iostream&g ...

  4. Link Cut Tree 动态树 小结

    动态树有些类似 树链剖分+并查集 的思想,是用splay维护的 lct的根是动态的,"轻重链"也是动态的,所以并没有真正的轻重链 动态树的操作核心是把你要把 修改/询问/... 等 ...

  5. 洛谷P3690 Link Cut Tree (动态树)

    干脆整个LCT模板吧. 缺个链上修改和子树操作,链上修改的话join(u,v)然后把v splay到树根再打个标记就好. 至于子树操作...以后有空的话再学(咕咕咕警告) #include<bi ...

  6. 洛谷P3690 [模板] Link Cut Tree [LCT]

    题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...

  7. bzoj2049-洞穴勘测(动态树lct模板题)

    Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...

  8. 模板Link Cut Tree (动态树)

    题目描述 给定N个点以及每个点的权值,要你处理接下来的M个操作.操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联 ...

  9. [BZOJ2631]tree 动态树lct

    2631: tree Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 5171  Solved: 1754[Submit][Status][Discus ...

  10. 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)

    题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...

随机推荐

  1. SWT图形用户界面之配置

    1.在eclipse的plugins目录下找到org.eclipse.swt.win32.win32.x86_64_3.111.0.v20190605-1801.jar文件 其中3.111.0是ecl ...

  2. Python自学--part1

    概要 Python介绍 Python安装 Hello World程序 变量 字符编码 用户输入 pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语句 表达式while 循环 表达 ...

  3. angularJs FileUpload插件上传同一文件无效问题记录

    参考:https://blog.csdn.net/qq_34829447/article/details/83780392 问题:使用FileUpload插件进行文件上传时,发现无法上传与上个文件相同 ...

  4. READING | 我是一只IT小小鸟

    “世界是如此的熙熙攘攘,让年轻的心找不到方向,但这些人是不能小看的啊,如果他们开始敲打自己的命令行.” “知之者不如好知者,好之者不如乐之者”,很多IT界的优秀人才都对计算机技术或者IT技术有着浓厚的 ...

  5. 2016.10.7初中部上午NOIP普及组比赛总结

    2016.10.7初中部上午NOIP普及组比赛总结 这次的题还可以,重新入了比赛的前十. 进度: 比赛:90+10+70+30=200 改题:AC+AC+AC+AC=AK 找试场 这题很简单,但是被欺 ...

  6. 在python2中的编码

    在python2中的编码 #_author:star#date:2019/10/29'''字符编码:ASCII:只能存英文和拉丁字符,gb2312:只能6700中文,1980年gbk1.0:存了200 ...

  7. python dict 实现swich

    使用dict实现swich,通过不同的键映射不同的函数. swich = { 'hour1':pred.getper1htable, 'hour6':pred.getper6htable, 'hour ...

  8. 组件component

    <!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...

  9. CentOS 编译golang

    CentOS 安装Mercurial http://hi.baidu.com/lang2858/item/cda8f6026cd522e0f45ba67f 获取代码 $ hg clone -u rel ...

  10. 06_springmvc之参数绑定(pojo和集合)

    一.包装类型pojo参数绑定 实现方法: 第一种方法:在形参中 添加HttpServletRequest request参数,通过request接收查询条件参数. 第二种方法:在形参中让包装类型的po ...