BZOJ 3282: Tree
3282: Tree
Time Limit: 30 Sec Memory Limit: 512 MB
Submit: 1714 Solved: 765
[Submit][Status][Discuss]
Description
给定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。
Input
第1行两个整数,分别为N和M,代表点数和操作数。
第2行到第N+1行,每行一个整数,整数在[1,10^9]内,代表每个点的权值。
第N+2行到第N+M+1行,每行三个整数,分别代表操作类型和操作所需的量。
Output
对于每一个0号操作,你须输出X到Y的路径上点权的Xor和。
Sample Input
1
2
3
1 1 2
0 1 2
0 1 1
Sample Output
1
HINT
1<=N,M<=300000
Source
莫名其妙的红色,LCT模板题
#include <bits/stdc++.h>
inline int nextChar(void) {
static const int siz = 1 << 20;
static char buffer[siz];
static char *head = buffer + siz;
static char *tail = buffer + siz;
if (head == tail)fread(head = buffer, 1, siz, stdin);
return int(*head++);
}
inline int nextInt(void) {
register int ret = 0;
register int neg = false;
register int bit = nextChar();
for (; bit < 48; bit = nextChar())
if (bit == '-')neg ^= true;
for (; bit > 47; bit = nextChar())
ret = ret * 10 + bit - '0';
return neg ? -ret : ret;
}
const int mxn = 300005;
int n, m, top;
int stk[mxn];
int val[mxn];
int sum[mxn];
int fat[mxn];
int rev[mxn];
int son[mxn][2];
inline bool isroot(int t) {
int f = fat[t];
if (!f)return true;
if (son[f][0] == t)return false;
if (son[f][1] == t)return false;
return true;
}
inline void update(int t) {
sum[t] = val[t];
if (son[t][0])sum[t] ^= sum[son[t][0]];
if (son[t][1])sum[t] ^= sum[son[t][1]];
}
inline void push(int t) {
rev[t] = 0;
std::swap(son[t][0], son[t][1]);
if (son[t][0])rev[son[t][0]] ^= 1;
if (son[t][1])rev[son[t][1]] ^= 1;
}
inline void pushdown(int t) {
for (stk[++top] = t; t; )
stk[++top] = t = fat[t];
for (; top; --top)
if (rev[stk[top]])
push(stk[top]);
}
inline void connect(int t, int f, int k) {
if (t)fat[t] = f;
if (f)son[f][k] = t;
}
inline void rotate(int t) {
int f = fat[t];
int g = fat[f];
int s = son[f][1] == t;
connect(son[t][!s], f, s);
connect(f, t, !s);
fat[t] = g;
if (g && son[g][0] == f)son[g][0] = t;
if (g && son[g][1] == f)son[g][1] = t;
update(f);
update(t);
}
inline void splay(int t) {
pushdown(t);
while (!isroot(t)) {
int f = fat[t];
int g = fat[f];
if (isroot(f))
rotate(t);
else {
int a = f && son[f][1] == t;
int b = g && son[g][1] == f;
if (a == b)
rotate(f), rotate(t);
else
rotate(t), rotate(t);
}
}
}
inline void access(int t) {
for (int p = 0; t; p = t, t = fat[t])
splay(t), son[t][1] = p, update(t);
}
inline void makeroot(int t) {
access(t), splay(t), rev[t] ^= 1;
}
inline void cut(int a, int b) {
makeroot(a), access(b), splay(b);
if (son[b][0] == a)son[b][0] = fat[a] = 0;
}
inline void link(int t, int f) {
makeroot(t), fat[t] = f;
}
inline int find(int t) {
access(t), splay(t);
while (son[t][0])
t = son[t][0];
return t;
}
signed main(void) {
n = nextInt();
m = nextInt();
for (int i = 1; i <= n; ++i)
val[i] = sum[i] = nextInt();
for (int i = 1; i <= m; ++i) {
int k = nextInt();
int x = nextInt();
int y = nextInt();
switch (k) {
case 0 :
makeroot(x);
access(y);
splay(y);
printf("%d\n", sum[y]);
break;
case 1:
if (find(x) != find(y))
link(x, y);
break;
case 2:
if (find(x) == find(y))
cut(x, y);
break;
case 3:
access(x);
splay(x);
val[x] = y;
update(x);
break;
}
}
}
@Author: YouSiki
BZOJ 3282: Tree的更多相关文章
- [BZOJ 3282] Tree 【LCT】
题目链接:BZOJ - 3282 题目分析 这道题是裸的LCT,包含 Link , Cut 和询问两点之间的路径信息. 写代码时出现的错误:Access(x) 的循环中应该切断的是原来的 Son[x] ...
- BZOJ 3282: Tree( LCT )
LCT.. -------------------------------------------------------------------------------- #include<c ...
- bzoj 3282: Tree (Link Cut Tree)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3282 题面: 3282: Tree Time Limit: 30 Sec Memory L ...
- BZOJ 3282 Tree Link-Cut-Tree(LCT)
题目大意: 给定N个点以及每一个点的权值,要你处理接下来的M个操作.操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y ...
- BZOJ 3282 Tree(动态树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3282 [题目大意] 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的x ...
- BZOJ 3282 Tree ——KD-Tree
[题目分析] 明显的LCT维护连通性的题目. access的操作是比较巧妙的,可以把结点到根变成偏爱路径,而且保证了该点是链上深度最深的点. 而且需边的思想也很巧妙,保证了复杂度. 但是只能用于修改路 ...
- BZOJ 3282 Tree ——Link-Cut Tree
[题目分析] 明显的LCT维护连通性的题目. access的操作是比较巧妙的,可以把结点到根变成偏爱路径,而且保证了该点是链上深度最深的点. 而且需边的思想也很巧妙,保证了复杂度. 但是只能用于修改路 ...
- 洛谷 P3690 【模板】Link Cut Tree (动态树) || bzoj 3282: Tree
https://blog.csdn.net/saramanda/article/details/55253627 https://blog.csdn.net/CHHNZ/article/details ...
- BZOJ 2002 && BZOJ 2409 LCT && BZOJ 3282 初步练习
#include <cstdio> ; inline void Get_Int(int & x) { ; ') ch=getchar(); +ch-'; ch=getchar(); ...
随机推荐
- tomcat9在centos7上启动慢问题
/opt/java/jdk1.8.0_162/jre/lib/security/java.security 将如下配置securerandom.source=file:/dev/random 改为se ...
- 论文阅读 | Formalizing Visualization Design Knowledge as Constraints: Actionable and Extensible Models in Draco
1. Introduction 程序员编写的可视化图表与专家眼中的设计标准总存在差距.我们无法每次都向可视化专家咨询设计上的意见,所以我们需求将设计标准,研究成果应用于自动化设计工具的正式框架,这些工 ...
- Netty源码分析第5章(ByteBuf)---->第1节: AbstractByteBuf
Netty源码分析第五章: ByteBuf 概述: 熟悉Nio的小伙伴应该对jdk底层byteBuffer不会陌生, 也就是字节缓冲区, 主要用于对网络底层io进行读写, 当channel中有数据时, ...
- md5sum命令详解
基础命令学习目录首页 原文链接:https://blog.csdn.net/cbbbc/article/details/48563023 前言 在网络传输.设备之间转存.复制大文件等时,可能会出现传输 ...
- pairwork(黄敬博12061156和黄伟龙12061172)
结对编程: 结对编程的优缺点: 优点: 1.相互督促,共同为了完成目标而努力: 2.节省时间,通过将疑难问题分开解决,共同讨论,实现了更高效的时间利用率: 3.能力互补,提高代码的质量,同时也提高了测 ...
- 20162319 实验二 Java面对对象程序设计 实验报告
实验二 Java面向对象程序设计 实验内容 1.初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装.继承.多态 3.初步掌握UML建模 4.熟悉S.O.L.I.D原则 5.了解设计模式 实验 ...
- Shell脚本 数据清洗
需要做的任务是将上图类似的格式的文件进行处理,将年月日小时分别提取出来放到每行的行尾(上图已清洗好) 自己的思路是先用cut命令将每行的年月日小时提取出来,分别给一个变量,然后再循环利用sed命令将年 ...
- Teamwork(The seventh day of the team)
做了很久,发现还是运行不了,很郁闷: 求大神指教这是什么错误?
- Web应用程序的基本安全实践
创建安全Web应用程序的主题非常广泛.它需要研究以了解安全漏洞.您还需要熟悉Windows..NET框架和ASP.NET的安全设施.最后,有必要了解如何使用这些安全特性来对付威胁. 即使您没有安全方面 ...
- 11_Java面向对象_第11天(接口、多态)_讲义
今日内容介绍 1.接口 2.多态 01接口的概念 * A:接口的概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的"类". 接口只描述所应该具备的方法,并没有具 ...