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

3 3
1
2
3
1 1 2
0 1 2
0 1 1

Sample Output

3
1

HINT

1<=N,M<=300000

Source

[Submit][Status][Discuss]

莫名其妙的红色,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的更多相关文章

  1. [BZOJ 3282] Tree 【LCT】

    题目链接:BZOJ - 3282 题目分析 这道题是裸的LCT,包含 Link , Cut 和询问两点之间的路径信息. 写代码时出现的错误:Access(x) 的循环中应该切断的是原来的 Son[x] ...

  2. BZOJ 3282: Tree( LCT )

    LCT.. -------------------------------------------------------------------------------- #include<c ...

  3. bzoj 3282: Tree (Link Cut Tree)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3282 题面: 3282: Tree Time Limit: 30 Sec  Memory L ...

  4. BZOJ 3282 Tree Link-Cut-Tree(LCT)

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

  5. BZOJ 3282 Tree(动态树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3282 [题目大意] 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的x ...

  6. BZOJ 3282 Tree ——KD-Tree

    [题目分析] 明显的LCT维护连通性的题目. access的操作是比较巧妙的,可以把结点到根变成偏爱路径,而且保证了该点是链上深度最深的点. 而且需边的思想也很巧妙,保证了复杂度. 但是只能用于修改路 ...

  7. BZOJ 3282 Tree ——Link-Cut Tree

    [题目分析] 明显的LCT维护连通性的题目. access的操作是比较巧妙的,可以把结点到根变成偏爱路径,而且保证了该点是链上深度最深的点. 而且需边的思想也很巧妙,保证了复杂度. 但是只能用于修改路 ...

  8. 洛谷 P3690 【模板】Link Cut Tree (动态树) || bzoj 3282: Tree

    https://blog.csdn.net/saramanda/article/details/55253627 https://blog.csdn.net/CHHNZ/article/details ...

  9. BZOJ 2002 && BZOJ 2409 LCT && BZOJ 3282 初步练习

    #include <cstdio> ; inline void Get_Int(int & x) { ; ') ch=getchar(); +ch-'; ch=getchar(); ...

随机推荐

  1. TensorFlow Python2.7环境下的源码编译(一)环境准备

    参考: https://blog.csdn.net/yhily2008/article/details/79967118 https://tensorflow.google.cn/install/in ...

  2. RabbitMQ入门:Hello RabbitMQ 代码实例

    在之前的一篇博客RabbitMQ入门:认识并安装RabbitMQ(以Windows系统为例)中,我们安装了RabbitMQ并且对其也有的初步的认识,今天就来写个入门小例子来加深概念理解并了解代码怎么实 ...

  3. thymeleaf 使用javascript定义数组报错

    js中免不了的要用的数组,一维的二维的三维的 但是当用到thymeleaf作为模版时候会有一些坑,导致数组不能用 org.thymeleaf.exceptions.TemplateProcessing ...

  4. Hands on Machine Learning with Sklearn and TensorFlow学习笔记——机器学习概览

    一.什么是机器学习? 计算机程序利用经验E(训练数据)学习任务T(要做什么,即目标),性能是P(性能指标),如果针对任务T的性能P随着经验E不断增长,成为机器学习.[这是汤姆米切尔在1997年定义] ...

  5. Python数据分析工具库-Numpy 数组支持库(一)

    1 Numpy数组 在Python中有类似数组功能的数据结构,比如list,但在数据量大时,list的运行速度便不尽如意,Numpy(Numerical Python)提供了真正的数组功能,以及对数据 ...

  6. js 基础拓展

    1.关于 try catch 的用法 <body> <div>请输出一个 5 到 10 之间的数字:</div> <input id="demo&q ...

  7. “北航Clubs”项目汇报

    一.项目展示 二.用户的痛点与需求 1.北航学生,在百团大战之后,很难再有渠道加入社团,了解社团活动,简直如蒙在鼓里! 2.当你周末想参加一些活动,充实一下枯燥的求学生活时,却发现不知道有哪些社团有活 ...

  8. 第一次作业——MathExam285

    MathExam285 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 • Estimate ...

  9. 团队作业4——第一次项目冲刺(Alpha版本)2017.11.18

    1.当天站立式会议照片 本次会议在5号公寓312召开,本次会议内容:①:熟悉每个人想做的模块.②:根据老师的要求将项目划分成一系列小任务.③:在上次会议内容完成的基础上增加新的任务. 2.每个人的工作 ...

  10. AWS EC2安装docker时的问题

    在AWS EC2的实例(Ubuntu)里面安装docker时,使用通常的安装步骤 :~$ sudo apt-get update :~$ sudo apt-get install docker 安装完 ...