[BZOJ 3282] Tree 【LCT】
题目链接:BZOJ - 3282
题目分析
这道题是裸的LCT,包含 Link , Cut 和询问两点之间的路径信息。
写代码时出现的错误:Access(x) 的循环中应该切断的是原来的 Son[x][1] ,然而我写成了 Son[x][0] !
代码
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; inline void Read(int &Num)
{
char c = getchar();
bool Neg = false;
while (c < '0' || c > '9')
{
if (c == '-') Neg = true;
c = getchar();
}
Num = c - '0'; c = getchar();
while (c >= '0' && c <= '9')
{
Num = Num * 10 + c - '0';
c = getchar();
}
if (Neg) Num = -Num;
} const int MaxN = 300000 + 5; int n, m;
int A[MaxN], T[MaxN], Father[MaxN], Son[MaxN][2]; bool isRoot[MaxN], Rev[MaxN]; inline void Update(int x)
{
T[x] = T[Son[x][0]] ^ T[Son[x][1]] ^ A[x];
} inline void Reverse(int x)
{
Rev[x] = !Rev[x];
swap(Son[x][0], Son[x][1]);
} inline void PushDown(int x)
{
if (!Rev[x]) return;
Rev[x] = false;
if (Son[x][0]) Reverse(Son[x][0]);
if (Son[x][1]) Reverse(Son[x][1]);
} void Rotate(int x)
{
int y = Father[x], f;
PushDown(y); PushDown(x);
if (x == Son[y][0]) f = 1;
else f = 0;
if (isRoot[y])
{
isRoot[y] = false;
isRoot[x] = true;
}
else
{
if (y == Son[Father[y]][0]) Son[Father[y]][0] = x;
else Son[Father[y]][1] = x;
}
Father[x] = Father[y];
Son[y][f ^ 1] = Son[x][f];
if (Son[x][f]) Father[Son[x][f]] = y;
Son[x][f] = y;
Father[y] = x;
Update(y);
Update(x);
} void Splay(int x)
{
int y;
while (!isRoot[x])
{
y = Father[x];
if (isRoot[y])
{
Rotate(x);
break;
}
if (y == Son[Father[y]][0])
{
if (x == Son[y][0])
{
Rotate(y);
Rotate(x);
}
else
{
Rotate(x);
Rotate(x);
}
}
else
{
if (x == Son[y][1])
{
Rotate(y);
Rotate(x);
}
else
{
Rotate(x);
Rotate(x);
}
}
}
} int Access(int x)
{
int y = 0;
while (x != 0)
{
Splay(x);
PushDown(x);
if (Son[x][1]) isRoot[Son[x][1]] = true;
Son[x][1] = y;
if (y) isRoot[y] = false;
Update(x);
y = x;
x = Father[x];
}
return y;
} void Make_Root(int x)
{
int t = Access(x);
Reverse(t);
} int Find_Root(int x)
{
int t = Access(x);
while (Son[t][0] != 0) t = Son[t][0];
return t;
} int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i)
{
Read(A[i]);
T[i] = A[i];
isRoot[i] = true;
Father[i] = 0;
}
int f, x, y, t, fx, fy;
for (int i = 1; i <= m; ++i)
{
Read(f); Read(x); Read(y);
switch (f)
{
case 0 :
Make_Root(x);
t = Access(y);
printf("%d\n", T[t]);
break; case 1 :
fx = Find_Root(x);
fy = Find_Root(y);
if (fx != fy)
{
Make_Root(x);
Splay(x);
Father[x] = y;
}
break; case 2 :
Make_Root(x);
Access(y);
Splay(y);
PushDown(y);
if (Son[y][0] == x)
{
isRoot[Son[y][0]] = true;
Father[Son[y][0]] = 0;
Son[y][0] = 0;
Update(y);
}
break; case 3 :
Splay(x);
A[x] = y;
Update(x);
break;
}
}
return 0;
}
[BZOJ 3282] Tree 【LCT】的更多相关文章
- [BZOJ - 2631] tree 【LCT】
题目链接:BZOJ - 2631 题目分析 LCT,像线段树区间乘,区间加那样打标记. 这道题我调了一下午. 提交之后TLE了,我一直以为是写错了导致了死循环. 于是一直在排查错误.直到.. 直到我看 ...
- 3282. Tree【LCT】
Description 给定N个点以及每个点的权值,要你处理接下来的M个操作. 操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和 ...
- BZOJ2631 tree 【LCT】
题目 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的边( ...
- BZOJ 3282 Tree Link-Cut-Tree(LCT)
题目大意: 给定N个点以及每一个点的权值,要你处理接下来的M个操作.操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y ...
- bzoj 3159: 决战【LCT】
只是想复健一下LCT没想到做了不得了的题--调了两天QAQ 题解是这么说的: 但是果然还不太理解--因为swap的前后问题调了好久,(所以一开始养成的习惯后面就不要再改啦-- 总之大概就是把对位置lc ...
- BZOJ 1468 Tree 【模板】树上点分治
#include<cstdio> #include<algorithm> #define N 50010 #define M 500010 #define rg registe ...
- 606. Construct String from Binary Tree 【easy】
606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...
- BZOJ 3282: Tree( LCT )
LCT.. -------------------------------------------------------------------------------- #include<c ...
- [BZOJ 2049] [Sdoi2008] Cave 洞穴勘测 【LCT】
题目链接:BZOJ - 2049 题目分析 LCT的基本模型,包括 Link ,Cut 操作和判断两个点是否在同一棵树内. Link(x, y) : Make_Root(x); Splay(x); F ...
随机推荐
- android intent 隐式意图和显示意图(activity跳转)
android中的意图有显示意图和隐式意图两种, 显示意图要求必须知道被激活组件的包和class 隐式意图只需要知道跳转activity的动作和数据,就可以激活对应的组件 A 主activity B ...
- android 66 sharedperference的使用
package com.itheima.qqlogin; import java.io.BufferedReader; import java.io.File; import java.io.File ...
- C语言实现双向链表删除节点、插入节点、双向输出等操作
#include<cstdio> #include<cstdlib> typedef struct DoubleLinkedList { int data; struct Do ...
- iOS AFNetWorking源码详解(一)
来源:Yuzeyang 链接:http://zeeyang.com/2016/02/21/AFNetWorking-one/ 首先来介绍下AFNetWorking,官方介绍如下: AFNetworki ...
- hdu2015java
偶数求和 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissio ...
- iOS UIKit:TableView之表格创建(1)
Table View是UITableView类的实例对象,其是使用节(section)来描述信息的一种滚动列表.但与普通的表格不同,tableView只有一行,且只能在垂直方向进行滚动.tableVi ...
- as3 打开窗口类
package FlashCode.utils{ import flash.display.Sprite; import flash.net.URLRequest; import flash.net. ...
- HTML+CSS基础学习笔记(7)
CSS布局模型 在网页中,元素有三种布局模型: 1.流动模型(Flow) 2.浮动模型(Float) 3.层模型(Layer) 一.流动模型 流动(Flow)是默认的网页布局模型,网页在默认状态下的H ...
- bootstrap3.0 模态框显示的文字超出怎么办
版本:bootstrap3.3 模态框文字超出 解决方案: 在html中控制自动换行 其实只要在表格控制中添加一句<div style="word-break:break-all& ...
- [网络] C# NetHelper网络通信编程类教程与源码下载
点击下载 NetHelper.zip 主要功能如下所示 检查设置的IP地址是否正确,返回正确的IP地址 检查设置的端口号是否正确,返回正确的端口号 将字符串形式的IP地址转换成IPAddress对象 ...