3282: Tree

Time Limit: 30 Sec  Memory Limit: 512 MB
Submit: 2249  Solved: 1042
[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

code

 #include<cstdio>
#include<algorithm> using namespace std; const int N = ; int val[N],fa[N],ch[N][],rev[N],sum[N],st[N],top; inline char nc() {
static char buf[],*p1 = buf,*p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2) ? EOF :*p1++;
}
inline int read() {
int x = ,f = ;char ch=nc();
for (; ch<''||ch>''; ch = nc()) if (ch == '-') f = -;
for (; ch>=''&&ch<=''; ch = nc()) x = x*+ch-'';
return x * f;
}
void pushup(int x) {
sum[x] = sum[ch[x][]] ^ sum[ch[x][]] ^ val[x];
}
void pushdown(int x) {
int l = ch[x][],r = ch[x][];
if (rev[x]) {
rev[l] ^= ;rev[r] ^= ;
swap(ch[x][],ch[x][]);
rev[x] ^= ;
}
}
bool isroot(int x) {
return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;
}
int son(int x) {
return ch[fa[x]][]==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)) ch[z][c] = x;fa[x] = z;
ch[x][!b] = y;fa[y] = x;
ch[y][b] = a;if (a) fa[a] = y;
pushup(y);pushup(x);
}
void splay(int x) {
top = ;st[++top] = x;
for (int i=x; !isroot(i); i=fa[i]) st[++top] = fa[i];
while (top) pushdown(st[top--]);
while (!isroot(x)) {
int y = fa[x];
if (!isroot(y)) {
if (son(x)==son(y)) rotate(y);
else rotate(x);
}
rotate(x);
}
}
void access(int x) {
for (int t=; x; t=x,x=fa[x]) {
splay(x);ch[x][] = t;pushup(x);
}
}
void makeroot(int x) {
access(x);
splay(x);
rev[x] ^= ;
}
void link(int x,int y) {
makeroot(x);
fa[x] = y;
}
void cut(int x,int y) {
makeroot(x);access(y);splay(y);
ch[y][] = fa[ch[y][]] = ;
}
void update(int x,int y) {
makeroot(x);val[x] = y;pushup(x);
}
int query(int x,int y) {
makeroot(x);access(y);splay(y);
return sum[y];
}
int find(int x) {
access(x);splay(x);
while (ch[x][]) x = ch[x][];
return x;
}
int main() {
int n = read(),m = read(),opt,x,y;
for (int i=; i<=n; ++i) sum[i] = val[i] = read();
while (m--) {
opt = read(),x = read(),y = read();
if (opt==) printf("%d\n",query(x,y));
else if (opt==) {
if (find(x)!=find(y)) link(x,y);
}
else if (opt==) {
if (find(x)==find(y)) cut(x,y);
}
else update(x,y);
}
return ;
}

3282: Tree(LCT)的更多相关文章

  1. 【BZOJ】3282: Tree(lct)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3282 复习了下lct,发现两个问题.. 1:一开始我以为splay那里直接全部rot(x)就好了,然 ...

  2. Link-Cut Tree(LCT) 教程

    目录 前置知识 介绍 Access FindRoot MakeRoot Split Link Cut 关于Splay中操作的一点说明: 模板 前置知识 请先对树链剖分和Splay有所了解.LCT基于树 ...

  3. 洛谷P3690 【模板】Link Cut Tree (LCT)

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

  4. BZOJ2631: tree(LCT)

    Description 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2 ...

  5. LUOGU P3690 【模板】Link Cut Tree (lct)

    传送门 解题思路 \(lct\)就是基于实链剖分,用\(splay\)来维护每一条实链,\(lct\)的维护对象是一棵森林.\(lct\)支持很多神奇的操作: \(1.\) \(access\):这是 ...

  6. LuoguP3690 【模板】Link Cut Tree (LCT)

    勉强算是结了个大坑吧或者才开始 #include <cstdio> #include <iostream> #include <cstring> #include ...

  7. 从ZOJ2114(Transportation Network)到Link-cut-tree(LCT)

    [热烈庆祝ZOJ回归] [首先声明:LCT≠动态树,前者是一种数据结构,而后者是一类问题,即:LCT—解决—>动态树] Link-cut-tree(下文统称LCT)是一种强大的数据结构,不仅可以 ...

  8. Device Tree(三):代码分析【转】

    转自:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html Device Tree(三):代码分析 作者:linuxer 发布于:201 ...

  9. 「ZJOI2018」历史(LCT)

    「ZJOI2018」历史(LCT) \(ZJOI\) 也就数据结构可做了-- 题意:给定每个点 \(access\) 次数,使轻重链切换次数最大,带修改. \(30pts:\) 挺好想的.发现切换次数 ...

随机推荐

  1. 什么是SpringBoot

    随着动态语言的流行(Ruby,Groovy,Scala,Node.js),Java的开发显得格外的笨重;繁多的配置,低下的开发效率,复杂的部署流程以及第三方技术集成难度大. 在上述环境 下,Sprin ...

  2. 一个简单的EventEmitter

    用JS写了一个简单的EventEmitter: class EventEmitter { /** * 事件名/回调列表 字典 * @type {Map<string, Array<func ...

  3. Unity调用外部摄像头,全屏显示摄像头画面

    有两种方法,常用的是GUI方法,代码如下: public class CameraTest : MonoBehaviour { WebCamTexture camTexture; void Start ...

  4. 美国L1签证面谈的时候一般VO会问到什么问题?

    L签证:L签证签发给被其中国公司调派到美国分公司或合资公司工作的人员.申请人必须将在美国担任经理级职务或具有专业知识,且在申请签证前的三年中至少为同一雇主或公司连续工作至少一年.签证签发费将因签证的入 ...

  5. js实现排序去重计算字符次数

    /*去重*/ var arr=[1,4,4,7,3,9,0,3,2,1,"你好","你","你好","你 "]; var ...

  6. 从暴力匹配到KMP算法

    前言 现在有两个字符串:\(s1\)和\(s2\),现在要你输出\(s2\)在\(s1\)当中每一次出现的位置,你会怎么做? 暴力匹配算法 基本思路 用两个指针分别指向当前匹配到的位置,并对当前状态进 ...

  7. tableViewcell上放定时器

    tableviewcell上的定时器: 1.创建一个管理定时器的TimerManger类, TimerManger.h #import <Foundation/Foundation.h> ...

  8. python_9_for

    #1 for i in range(10):#默认从0开始,步长为1 print("loop",i) #2 for i in range(0,10,1):#步长为1 print(& ...

  9. python读取excel中的数据

    import numpy as np import matplotlib.pyplot as plt import pandas as pd #df = pd.read_excel('/Users/N ...

  10. Java自带工具包StringUtils包含方法

    //导入包 import org.apache.commons.lang3.StringUtils //判断不为空 不包含空格 StringUtils.isNotEmpty(" " ...