回顾一下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的更多相关文章

  1. BZOJ 2002 && BZOJ 2409 LCT && BZOJ 3282 初步练习

    #include <cstdio> ; inline void Get_Int(int & x) { ; ') ch=getchar(); +ch-'; ch=getchar(); ...

  2. [BZOJ 3282] Tree 【LCT】

    题目链接:BZOJ - 3282 题目分析 这道题是裸的LCT,包含 Link , Cut 和询问两点之间的路径信息. 写代码时出现的错误:Access(x) 的循环中应该切断的是原来的 Son[x] ...

  3. BZOJ 3282: Tree( LCT )

    LCT.. -------------------------------------------------------------------------------- #include<c ...

  4. bzoj 3282: Tree (Link Cut Tree)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3282 题面: 3282: Tree Time Limit: 30 Sec  Memory L ...

  5. BZOJ 3282: Tree

    3282: Tree Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1714  Solved: 765[Submit][Status][Discuss ...

  6. BZOJ 3282 Tree(动态树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3282 [题目大意] 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的x ...

  7. BZOJ 3282 Tree ——KD-Tree

    [题目分析] 明显的LCT维护连通性的题目. access的操作是比较巧妙的,可以把结点到根变成偏爱路径,而且保证了该点是链上深度最深的点. 而且需边的思想也很巧妙,保证了复杂度. 但是只能用于修改路 ...

  8. 【BZOJ 3282】Tree Link Cut Tree模板题

    知道了为什么要换根(changeroot),access后为什么有时要splay,以及LCT的其他操作,算是比较全面的啦吧,,, 现在才知道这些,,,真心弱,,, #include<cstdio ...

  9. BZOJ 3282 Tree Link-Cut-Tree(LCT)

    题目大意: 给定N个点以及每一个点的权值,要你处理接下来的M个操作.操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y ...

随机推荐

  1. 【算法学习】【洛谷】cdq分治 & P3810 三维偏序

    cdq是何许人也?请参看这篇:https://wenku.baidu.com/view/3b913556fd0a79563d1e7245.html. 在这篇论文中,cdq提出了对修改/询问型问题(Mo ...

  2. Wannacry样本取证特征与清除

    一.取证特征 1)网络域名特征 http://www.iuqerfsodp9ifjaposdfjhgosurijfaewrwergwea.com 2)文件特征 母体文件 mssecsvc.exe c: ...

  3. mysql远程连接数据库

    配置mysql允许远程连接的方法. (1)查看3306端口状态 netstat -an | grep 3306 (2)修改mysql配置文件 ubuntu系统:vim /etc/mysql/mysql ...

  4. connect系统调用

    /* * Attempt to connect to a socket with the server address. The address * is in user space so we ve ...

  5. listen系统调用

    /* * Perform a listen. Basically, we allow the protocol to do anything * necessary for a listen, and ...

  6. shell 指令分析nginx 日志qps

    实时分析 tail -f points.api.speiyou.cn.access.log|awk 'BEGIN{key="";cnt=0}{if(key==$5){cnt++}e ...

  7. bzoj 1034 泡泡堂BNB

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1034 题解: 很明显的贪心,读过田忌赛马的典故就很容易能想出来,分成三种情况讨论: < ...

  8. mysql高可用架构 -> MHA部署-04

    MHA架构图 本次MHA的部署基于GTID复制成功构建,普通主从复制也可以构建MHA架构. 下载所需的软件包 mkdir /server/tools -p //创建存放包的目录 [root@db01 ...

  9. java基础25 线程的常用方法、线程安全问题、死锁现象

    一.线程的常用方法 1.Thread(String name):初始化线程的名字2. setName(String name):设置线程的名字3. getName():返回线程的名字4. sleep( ...

  10. eclipse导入/导出项目要注意三个地方

    这个三个地方的jdk必须保持一致,不报错