[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自动关机代码
http://www.open-open.com/lib/view/open1409209890713.html 要实现自动关机的功能需要手机有root权限,如果手机有root权限,运行该程序时,会提 ...
- Activiti5.16.4数据库表结构
一.ACTIVITI 数据库E-R图(5.16.4) Activiti 5.16.4 总共有24张表,增加act_evt_log(事件日志),以及增加了对SasS的支持. 在流程定义.运行实例和历史的 ...
- PureMVC(JS版)源码解析(十二):Facade类
MVC设计模式的核心元素在PureMVC中体现为Model类.View类和Controller类.为了简化程序开发,PureMVC应用Facade模式. Facade是Model\View\Co ...
- CentOS6.5编译安装最新MySQL 5.7.11
安装前工作:1,从官方网址下载MySQL5.7.11源码包,大概49M2,安装好CentOS6.5 64位操作系统.建议update操作系统,以便是此版本最新的3. yum -y install g ...
- SpringMVC注解@RequestMapping
/** * GET 查询 * * @return 视图路径 */ @RequestMapping(value = {"/index&q ...
- 3G/4G网卡使用
整体架构: pppd call option & ----------↓---------- option脚本(设置PPP连接) ----------↓---------- chat脚本(进行 ...
- SELECT [Code] ,[AlarmID] ,[ItemName] ,[isDeleted] ,[Remark] FROM [LjlData].[dbo].[T_BaseDetail] union select 0--
SELECT [id] ,[AlarmID] ,[ItemName] ,[isDeleted] ,[Remark] FROM [LjlData]. ...
- C#中方法的参数修饰符
做项目久了,有的时候真的需要静下心来认真的总结一下自己所用到的技术,而不是每天依葫芦画瓢,每天忙忙碌碌,到头来不知道自己忙了个啥,学了什么,自己到底掌握了多少知识.所以我想回顾一下C#的基础知识,把重 ...
- 将应用程序中的一些参数写到xml配置文件中
最近碰到一个问题,需要将程序中的一些基本参数写到xml文件中,虽然网上有好多现成的代码,但是觉得对xml不熟悉,果断就研究了一下.先说一下大体思路吧,我设计了一个用来读取和回填的类,然后定义了一个接口 ...
- IE6 中的最大最小寬度和高度 css 高度 控制(兼容版本)
/* 最小寬度 */.min_width{min-width:300px; /* sets max-width for IE */ _width:expression(document.body.cl ...