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. docker 一篇文章学习容器化

      什么是镜像?什么是容器?   一句话回答:镜像是类,容器是实例   docker 基本操作命令:   删除所有container: docker rm $(docker ps -a -q)   删 ...

  2. 廖雪峰git教程学习笔记3

    commit是一串不便记忆的数字,为了方便记忆,引入tag,tag就跟HEAD一样,就像一个指针,指向commit,且指向是不能变得,一个commit就有一个tag 给当前分支下的当前commit打上 ...

  3. 用 Delphi 7 实现基于 FFMS2 的视频转 GIF 工具 [原创]

    儿子经常要把自拍的视频(ts格式)转成表情包,下载了几个工具都不大好用,更多的还要收费.那就想自己写一个吧,没想到这一下断断续续地,居然 3 个月过去了.现在总算弄出个作品来了,结个贴吧.唉,天资愚钝 ...

  4. .NET处理Json的几种方式

    序列化里的xml,soap,binary在上一篇文章里面已经说过了,这篇主要说json. json是目前非常流行的一种序列化数据的方式,很多api都采用的是json,结构简洁,容易理解,适用性强,逐渐 ...

  5. Kubernetes探索学习002--Kubernetes的基本使用

    Kubernetes 的基本使用方法 原则:使用YAML文件描述你要部署的API对象! 以部署nginx静态站点为例,具体操作及内容如下 1.编写YAML文件 [root@kubernetes01 ~ ...

  6. Python操作数据库之 MySQL

    Python操作数据库之MySQL 一.安装Python-MySQLdb模块 Python-MySQLdb是一个操作数据库的模块,Python 通过它对 mysql 数据实现各种操作. 如果要源码安装 ...

  7. XSS攻击防御篇

    前言   上篇文章中提到了 XSS 攻击,而且,也从几个方面介绍了 XSS 攻击带来的严重影响.那么,这篇文章中,主要是针对 XSS 攻击做一个基本的防御,看看可以通过几种方式来修复这个特别常见的安全 ...

  8. 为什么你学过Java却忘光了——记第一次助教同学见面会

    大约两周之前,主讲老师刘志勇老师和我约定,让我上周四到课堂上和同学们认识.交流一下.一开始我不太明了去和大家见面要说些什么,也不太理解这么做的必要性是什么.但随着日子临近,我请教了周筠老师,周筠老师和 ...

  9. 微信小程序——音阶练耳 宣传页面

    音阶练耳是什么? 音阶练耳小程序是一款听音练习音阶,拥有简介界面的交互式小程序,以虚拟钢琴为辅助乐器,应用于日常练习,涵盖了五个八度内26种调式.以及下行中的所有调式与和声小调式的衍生,提高辨认音阶的 ...

  10. ### Error building SqlSession.

    org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession.### The error may e ...