bzoj 3282
回顾一下LCT,容易写错的地方:
1、每次断掉Splay中的边,必须update一下父亲节点,再根据具体情况是否splay父亲节点。
2、养成没有用的值(比如当pre[u]不为0时的pnt[u])不去乱修改的习惯。
/**************************************************************
Problem: 3282
User: idy002
Language: C++
Result: Accepted
Time:2968 ms
Memory:10712 kb
****************************************************************/ #include <cstdio>
#include <utility>
#include <set>
#define N 300010
using namespace std; typedef pair<int,int> dpr; struct Lct {
int pnt[N], pre[N], son[N][], val[N], xsum[N], rtg[N]; void update( int nd ) {
xsum[nd] = val[nd];
if( son[nd][] ) xsum[nd] ^= xsum[son[nd][]];
if( son[nd][] ) xsum[nd] ^= xsum[son[nd][]];
}
void rotate( int nd, int d ) {
int p=pre[nd];
int s=son[nd][!d];
int ss=son[s][d]; son[nd][!d] = ss;
son[s][d] = nd;
if( p ) son[p][ nd==son[p][] ] = s;
else {
pnt[s]=pnt[nd];
pnt[nd] = ;
} pre[nd] = s;
pre[s] = p;
if( ss ) pre[ss] = nd; update( nd );
update( s );
}
void pushdown( int nd ) {
if( rtg[nd] ) {
int &ls=son[nd][], &rs=son[nd][];
swap(ls,rs);
if( ls ) rtg[ls] ^= ;
if( rs ) rtg[rs] ^= ;
rtg[nd] = ;
}
}
void big_push( int nd ) {
if( pre[nd] ) big_push(pre[nd]);
pushdown(nd);
}
void splay( int nd, int top= ) {
big_push( nd );
while( pre[nd]!=top ) {
int p=pre[nd];
int nl=nd==son[p][];
if( pre[p]==top ) {
rotate( p, nl );
} else {
int pp=pre[p];
int pl=p==son[pp][];
if( nl==pl ) {
rotate( pp, pl );
rotate( p, nl );
} else {
rotate( p, nl );
rotate( pp, pl );
}
}
}
}
void access( int nd ) {
int u=nd;
int v=;
while( u ) {
splay(u);
int s=son[u][];
if( s ) {
pre[s] = ;
pnt[s] = u;
}
if( v ) {
pre[v] = u;
pnt[v] = ;
}
son[u][] = v;
update(u);
v = u;
u = pnt[u];
}
splay(nd);
}
int findroot( int u ) {
while( pre[u] ) u=pre[u];
while( pnt[u] ) {
u=pnt[u];
while( pre[u] ) u=pre[u];
}
return u;
}
void makeroot( int u ) {
access(u);
rtg[u] ^= ;
}
void cut( int u, int v ) {
makeroot(u);
access(v);
pnt[u] = pre[u] = ;
son[v][] = ;
update(v);
}
void link( int u, int v ) {
makeroot(u);
makeroot(v);
pnt[u] = v;
}
int query( int u, int v ) {
makeroot(u);
access(v);
return xsum[v];
}
void modify( int u, int v ) {
access(u);
xsum[u] ^= val[u];
val[u] = v;
xsum[u] ^= val[u];
}
}T; int n, m;
set<dpr> st; int main() {
scanf( "%d%d", &n, &m );
for( int i=,w; i<=n; i++ ) {
scanf( "%d", &w );
T.modify( i, w );
}
for( int i=,opt,u,v; i<=m; i++ ) {
scanf( "%d%d%d", &opt, &u, &v );
if( opt== ) {
printf( "%d\n", T.query(u,v) );
} else if( opt== ) {
if( u>v ) swap(u,v);
if( T.findroot(u)!=T.findroot(v) ) {
st.insert( dpr(u,v) );
T.link(u,v);
}
} else if( opt== ) {
if( u>v ) swap(u,v);
if( st.count( dpr(u,v) ) ) {
st.erase( dpr(u,v) );
T.cut(u,v);
}
} else {
T.modify( u, v );
}
}
}
bzoj 3282的更多相关文章
- BZOJ 2002 && BZOJ 2409 LCT && BZOJ 3282 初步练习
#include <cstdio> ; inline void Get_Int(int & x) { ; ') ch=getchar(); +ch-'; ch=getchar(); ...
- [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
3282: Tree Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1714 Solved: 765[Submit][Status][Discuss ...
- 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模板题
知道了为什么要换根(changeroot),access后为什么有时要splay,以及LCT的其他操作,算是比较全面的啦吧,,, 现在才知道这些,,,真心弱,,, #include<cstdio ...
- BZOJ 3282 Tree Link-Cut-Tree(LCT)
题目大意: 给定N个点以及每一个点的权值,要你处理接下来的M个操作.操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y ...
随机推荐
- elasticsearch-head插件安装的一些坑!es6.5.4版本
折腾了一晚上,总算成功了!,大部分坑都记录了下来,版本升级太快真实个大坑,每个版本都不一样,学的心累!! 这坑太多了!主要就是以下几点最主要的: 因为我这里只使用hear安装,不使用哪个打包工具,所以 ...
- Mysql_Learning_Notes_mysql系统结构_2
Mysql_Learning_Notes_mysql系统结构_2 三层体系结构,启动方式,日志类型及解析方法,mysql 升级 连接层 通信协议处理\线程处理\账号认证(用户名和密码认证)\安全检查等 ...
- 如何提高单片机Flash的擦写次数
所谓提高flash的擦写次数,并不是真正的提高flash擦写次数,而是通过以"空间换时间"概念,在软件上实现“操作的次数大于其寿命”.详见链接: http://bbs.eeworl ...
- 【Andorid开发框架学习】之Volley入门
Volley是Android平台上的网络通信库,能使网络通信更快,更简单,更健壮.Volley特别适合数据量不大但是通信频繁的场景.在listView显示图片这方面,使用volley也是比较好的,不必 ...
- Linux 获取网关地址
route命令的用法:操作或者显示IP路由表route:DESCRIPTION Route manipulates the kernel's IP routing tables. Its primar ...
- scala中“_”的用法
参见链接 http://blog.csdn.net/i6448038/article/details/50017427
- node项目中用到的一些模块
1.http模块,用来搭建服务器 代码,简单服务器实现 var http = require('http'); http.createServer(function (request, respons ...
- file.getPath() getAbsolutePath() getCanonicalPath()区别
package file; import java.io.File; import java.io.IOException; public class getFilePath { public sta ...
- yum命令安装软件时,出现--centos 7 安装apache 出现 Could not resolve host: mirrorlist.centos.org; 未知的错误"--CentOS网络设置 couldn't resolve host 'mirrorlist.centos.org问题解决
CentOS网络设置 couldn't resolve host 'mirrorlist.centos.org问题解决 今天在虚拟机上安装完CentOS6.5之后,首次使用时yum命令安装软件时,出现 ...
- 使用插件实现Jenkins参数化构建
一.插件安装 1.打开插件管理,在此界面可以安装插件 二.参数化 1.在“可选插件”中查找如下两个插件然后安装,安装后重启Jenkins Build With Parameters 输入框式的参数 P ...