[bzoj3282]Tree_LCT
Tree bzoj-3282
题目大意:给你n个点m个操作。更改单点权值,加边,删边;查询路径异或和。
注释:$1\le n,m\le 10^5$
想法:看到了加边删边,果断想到LCT维护。至于路径异或和,我们只需要维护每个点对应splay的子树异或和,然后查询的时候直接access+splay,makeroot之后就行了。
最后,附上丑陋的代码... ...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 300050
#define ls ch[p][0]
#define rs ch[p][1]
#define get(x) (ch[f[x]][1]==x)
int ch[N][2],f[N],sum[N],n,m,val[N],rev[N];
inline bool isrt(int p)
{
return ch[f[p]][1]!=p&&ch[f[p]][0]!=p;
}
inline void pushdown(int p)
{
if(rev[p])
{
swap(ch[ls][0],ch[ls][1]);
swap(ch[rs][0],ch[rs][1]);
rev[ls]^=1; rev[rs]^=1;
rev[p]=0;
}
}
inline void pushup(int p)
{
sum[p]=sum[ls]^sum[rs]^val[p];
}
inline void update(int p)
{
if(!isrt(p)) update(f[p]);
pushdown(p);
}
void rotate(int x)
{
int y=f[x],z=f[y],k=get(x);
if(!isrt(y)) ch[z][ch[z][1]==y]=x;
ch[y][k]=ch[x][!k]; f[ch[y][k]]=y;
ch[x][!k]=y; f[y]=x; f[x]=z;
pushup(y); pushup(x);
}
void splay(int x)
{
update(x);
for(int fa;fa=f[x],!isrt(x);rotate(x))
if(!isrt(fa))
rotate(get(fa)==get(x)?fa:x);
}
void access(int p)
{
int t=0;
while(p) splay(p),rs=t,pushup(p),t=p,p=f[p];
}
void makeroot(int p)
{
access(p); splay(p);
swap(ls,rs); rev[p]^=1;
}
void link(int x,int p)
{
makeroot(x); f[x]=p;
}
void cut(int x,int p)
{
makeroot(x); access(p); splay(p); ls=f[x]=0;
}
int find(int p)
{
access(p); splay(p);
while(ls) pushdown(p),p=ls;
return p;
}
void fix(int x,int v)
{
splay(x);
sum[x]^=val[x];
val[x]=v;
sum[x]^=val[x];
}
int main()
{
scanf("%d%d",&n,&m);
int i,x,y,opt;
for(i=1;i<=n;i++) scanf("%d",&val[i]);
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&opt,&x,&y);
if(opt==0)
{
makeroot(x); access(y); splay(y);
printf("%d\n",sum[y]);
}else if(opt==1)
{
int t1=find(x),t2=find(y);
if(t1!=t2) link(x,y);
}else if(opt==2)
{
int t1=find(x),t2=find(y);
if(t1==t2) cut(x,y);
}else
{
fix(x,y);
}
}
}
小结:LCT的题有很明显的特点,就是动态维护加边删边,一般要是你能做上就上LCT吧,别的能维护动态树我也不会啊...
[bzoj3282]Tree_LCT的更多相关文章
- 【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
传送门 又是权限题= =,过了NOIp我就要去当一只权限狗! LCT裸题,get到了两个小姿势. 1.LCA操作应该在access中随时updata 2.Link操作可以更简单 void Link(i ...
- BZOJ3282——Tree
1.题目大意:动态树问题,点修改,链查询xor和 裸题一道.. #include <stack> #include <cstdio> #include <cstdlib& ...
- 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模板题: 话说xor和的意思是所有数xor一下: #include<iostream> #include<cstdio> #include<cstring> ...
- BZOJ3282:Tree(TCL基础题)
给定N个点以及每个点的权值,要你处理接下来的M个操作. 操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和. 保证x到y是联通的. ...
- [bzoj2631]tree_LCT
tree bzoj-2631 题目大意:给定一个n个点的树,每个点的初始权值为1,支持:删边加边(这两个操作同时进行,保证操作之后还是一棵树),路径加,路径乘,查询路径和. 注释:$1\le n,q\ ...
随机推荐
- c++ string 解析ip
比如输入是192.168.80.12-15,解析成192.168.80.12.192.168.80.13.192.168.80.14.192.168.80.15. #include <iostr ...
- openStack 主机流量计运行状态 随笔记录
root@ruiy-controller:~# ifconfigeth0 Link encap:Ethernet HWaddr 0c:c4:7a:0d:97:2c ine ...
- Tunnel Warfare(树状数组+二分)
http://poj.org/problem?id=2892 题意:输入n,m.n代表数轴的长度,m代表操作数. D x: 摧毁点x Q x: 询问村庄x最左与最右没有被摧毁的点的距离 R :恢复最 ...
- Cuckoo for Hashing
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2719 #include <stdio.h ...
- [Apple开发者帐户帮助]六、配置应用服务(3)创建地图标识符和私钥
要与MapKit JS通信,您将使用Maps私钥对一个或多个开发人员令牌进行签名. 首先注册地图标识符以识别您的应用.为使用MapKit JS的每个应用注册地图标识符.接下来创建并下载启用了MapKi ...
- input点击修改样式
<input id="geren" type="button" value="个人奖励" style="BORDER-TOP ...
- jdbc 接口学习笔记
一.JDBC概念 JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系型数据库提供统一访问,它由一组用Jav ...
- B - Taxi(贪心)
Problem description After the lessons n groups of schoolchildren went outside and decided to visit P ...
- Spring 的优秀工具类盘点---转
第 1 部分: 文件资源操作和 Web 相关工具类 http://www.ibm.com/developerworks/cn/java/j-lo-spring-utils1/ 文件资源操作 文件资源的 ...
- RPC与REST
RPC与REST (摘自网络,个人理解)