BZOJ3282——Tree
1、题目大意:动态树问题,点修改,链查询xor和
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
namespace LinkCutTree{
struct Node{
Node *ch[2], *fa;
int sum, num;
bool rev;
inline int which();
inline void reverse(){
if(this) rev ^= 1;
}
inline void pd(){
if(rev){
swap(ch[0], ch[1]);
ch[0] -> reverse();
ch[1] -> reverse();
rev = false;
}
}
inline void maintain(){
sum = num ^ ch[0] -> sum ^ ch[1] -> sum;
}
Node();
} *null = new Node, tree[300010], *pos[300010];
Node::Node(){
num = sum = 0;
rev = false;
ch[0] = ch[1] = fa = null;
}
inline int Node::which(){
if(fa == null || (this != fa -> ch[0] && this != fa -> ch[1])) return -1;
return this == fa -> ch[1];
}
inline void rotate(Node *o){
Node *p = o -> fa;
int l = o -> which(), r = l ^ 1;
o -> fa = p -> fa;
if(p -> which() != -1) p -> fa -> ch[p -> which()] = o;
p -> ch[l] = o -> ch[r];
if(o -> ch[r]) o -> ch[r] -> fa = p;
o -> ch[r] = p; p -> fa = o;
o -> ch[r] -> maintain();
o -> maintain();
}
inline void splay(Node *o){
static stack<Node*> st;
if(!o) return;
Node *p = o;
while(1){
st.push(p);
if(p -> which() == -1) break;
p = p -> fa;
}
while(!st.empty()){
st.top() -> pd(); st.pop();
}
while(o -> which() != -1){
p = o -> fa;
if(p -> which() != -1){
if(p -> which() ^ o -> which()) rotate(o);
else rotate(p);
}
rotate(o);
}
}
inline void Access(Node *o){
Node *y = null;
while(o != null){
splay(o);
o -> ch[1] = y;
o -> maintain();
y = o; o = o -> fa;
}
}
inline void MovetoRoot(Node *o){
Access(o);
splay(o);
o -> reverse();
}
inline Node* FindRoot(Node *o){
Access(o);
splay(o);
while(o -> ch[0] != null) o = o -> ch[0];
return o;
}
inline void Link(Node *x, Node *y){
MovetoRoot(x);
x -> fa = y;
}
inline void Cut(Node *x, Node *y){
MovetoRoot(x);
Access(y);
splay(y);
y -> ch[0] = x -> fa = null;
y -> maintain();
}
}
int main(){
using namespace LinkCutTree;
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++){
int x; scanf("%d", &x);
pos[i] = &tree[i];
pos[i] -> num = pos[i] -> sum = x;
}
int op, x, y;
while(m --){
scanf("%d%d%d", &op, &x, &y);
if(op == 0){
MovetoRoot(pos[x]);
Access(pos[y]);
splay(pos[y]);
printf("%d\n", pos[y] -> sum);
}
else if(op == 1){
if(FindRoot(pos[x]) != FindRoot(pos[y])) Link(pos[x], pos[y]);
}
else if(op == 2){
if(FindRoot(pos[x]) == FindRoot(pos[y])) Cut(pos[x], pos[y]);
}
else{
Access(pos[x]);
splay(pos[x]);
pos[x] -> num = y;
pos[x] -> maintain();
}
}
return 0;
}
BZOJ3282——Tree的更多相关文章
- BZOJ3282: Tree
传送门 又是权限题= =,过了NOIp我就要去当一只权限狗! LCT裸题,get到了两个小姿势. 1.LCA操作应该在access中随时updata 2.Link操作可以更简单 void Link(i ...
- BZOJ-3282 Tree Link-Cut-Tree(似乎树链剖分亦可)
蛋蛋用链剖A的,我写的LCT 3282: Tree Time Limit: 30 Sec Memory Limit: 512 MB Submit: 1241 Solved: 542 [Submit][ ...
- [bzoj3282]Tree (lct)
昨天看了一天的lct..当然幸好最后看懂了(也许吧..) 论善良学长的重要性T_T,老司机带带我! 这题主要是删边的时候还要判断一下..蒟蒻一开始天真的以为存在的边才能删结果吃了一发wa... 事实是 ...
- BZOJ3282: Tree (LCT模板)
Description 给定N个点以及每个点的权值,要你处理接下来的M个操作. 操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和 ...
- 【学习心得】Link-cut Tree
Link-cut Tree是一种支持改变树(森林)的形态(link和cut),同时维护树的路径上节点信息的数据结构.lct通过splay来维护每次的perferred path,说白了就是一个动态的树 ...
- AHOI2018训练日程(3.10~4.12)
(总计:共90题) 3.10~3.16:17题 3.17~3.23:6题 3.24~3.30:17题 3.31~4.6:21题 4.7~4.12:29题 ZJOI&&FJOI(6题) ...
- 【BZOJ3282】Tree (Link-Cut Tree)
[BZOJ3282]Tree (Link-Cut Tree) 题面 BZOJ权限题呀,良心luogu上有 题解 Link-Cut Tree班子提 最近因为NOIP考炸了 学科也炸了 时间显然没有 以后 ...
- 【BZOJ3282】Tree LCT
1A爽,感觉又对指针重怀信心了呢= =,模板题,注意单点修改时splay就好,其实按吾本意是没写的也A了,不过应该加上能更好维护平衡性. ..还是得加上好= = #include <iostre ...
- 【bzoj3282】Tree
LCT模板题: 话说xor和的意思是所有数xor一下: #include<iostream> #include<cstdio> #include<cstring> ...
随机推荐
- 【原】gulp快速入门
今天刚入职了一家新公司,结果明天就要开始项目了.上级说要用gulp来打包代码,所以今晚花了一晚来看这个gulp, 可以说已经入门了.所以做一个小小的总结 : 首先全局安装gulp npm instal ...
- python学习笔记-(三)条件判断和循环
1.条件判断语句 Python中条件选择语句的关键字为:if .elif .else这三个.其基本形式如下: age_of_cc = 27 age = int(input("guessage ...
- Axure7.0汉化方法
下载汉化包 AxureRP7CN_汉化包.rar 首先退出正在运行中的 Axure (如果您正在使用). 将 汉化包.rar 文件解压, 得到 lang 文件夹, 然后将其复制到 Axure 安装目 ...
- easyUI数据表格datagrid之笔记
1.用ajax获取数据库数据 /**========================================= * 读取数据库信息,使用ajax的load方法 */function getMe ...
- 基于Python实现对PDF文件的OCR识别
http://www.jb51.net/article/89955.htm https://pythontips.com/2016/02/25/ocr-on-pdf-files-using-pytho ...
- js随机从数组中取出几个元素
JS如何从一个数组中随机取出一个元素或者几个元素. 假如数组为 var items = ['1','2','4','5','6','7','8','9','10']; 1.从数组items中随机取出一 ...
- 重启nginx
在env/nginx/sbin目录下输入:nginx,即可重启
- Python 系列:1 - Tuples and Sequences
5.3 Tuples and Sequences We saw that lists and strings have many common properties, e.g., indexing a ...
- php 多条数据更新
mysql更新语句很简单,更新一条数据的某个字段,一般这样写: 1 UPDATE mytable SET myfield = 'value' WHERE other_field = 'other_va ...
- C#中事件的使用
C#中事件的使用 http://www.cnblogs.com/wayfarer/archive/2004/04/20/6712.html 用一个例子来说明事件的使用. 创建一个简单的类,名为Fil ...