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(); ...
随机推荐
- 在python脚本中设置环境变量,并运行相关应用
1. 问题 在自动化应用的时候 ,有时候环境变量与运行需要不一致.这时候有两种选择: 改变节点环境变量,使得其和运行需求保持一致: 在自动化脚本中设置环境变量,其范围只在脚本运行环境中有效. 显然,当 ...
- 前端开发利器 livereload -- 从此告别浏览器F5键
各位从事前端开发的童鞋们,大家每天coding && coding,然后F5 && F5,今天推荐一个静态文件在浏览器中自动更新的扩展 livereload,不同手动刷 ...
- Hyperledger Fabric chaincode 开发(疑难解答)
Q&A Q1: 使用fabric release 1.2 进行golang chaincode开发时报错: ..\..\hyperledger\fabric\vendor\github.com ...
- Openstack逻辑架构
一. Keystone -身份认证管理 提供了认证和授权的服务,openstack不同的组件通信都要经过授权,确保正确的用户和服务是经过认证的.并且它集成了大量的认证机制,比如用户名/密码和令牌/基 ...
- 软件工程-东北师大站-第十次作业(PSP)
1.本周PSP 2.本周进度条 3.本周累计进度图 代码累计折线图 博文字数累计折线图 4.本周PSP饼状图
- bootstrap table的展开行问题
照着网上与api里说的添加detailView属性设置为true,detailFormatter属性为展开后的内容,但是设置之后发现,在表格每一行最前面是多出一列正常该显示"+"的 ...
- Data truncation: Truncated incorrect DOUBLE value:
在写sql查询语句queryRunner.update(connection,"update account set balance=? where name=?",account ...
- NEWBEE软件团队 人员分配情况及分数获得方式
人员分配: PM:李桐 王骜 dev:王骜 刘垚鹏 安康 林旭鹏 黄新越 test:黄伟龙 李桐 马佐霖 黄新越 注:黄新越为女生,不方便平时的交流,所以任务分配较为灵活,特分在两个组里. 评分 ...
- 1001.A+B Format (20) 解题
代码入口(https://github.com/NSDie/object-oriented) 这题的解题思路我有两个: 第一个是两个数字相加然后判断位数,因为题目限制了范围1000000的绝对值以内嘛 ...
- 内网php项目访问(切换在线解决)
之前内网访问出现过问题: 可参考手机访问本地php项目遇到的问题及解决(2015-06-20 09:41) 后来重装wamp之后,要访问还是出现问题 即http://192.168.191.1/mui ...