回顾一下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. MeasureSpec介绍及使用详解

    一个MeasureSpec封装了父布局传递给子布局的布局要求,每个MeasureSpec代表了一组宽度和高度的要求.一个MeasureSpec有大小和模式组成.他有三种模式: UNSPECIFIED ...

  2. libevent简介和使用【转】

    转自:http://www.open-open.com/lib/view/open1386510630330.html libevent是一个基于事件触发的网络库,memcached底层也是使用lib ...

  3. ubuntu git 简单入门【转】

    转自:http://blog.chinaunix.net/uid-20718384-id-3334859.html 1. 安装 sudo apt-get install git-core 2.  初始 ...

  4. ClientDataset 三层 var and out arguments must match parameter

    ​​​将Delphi升级到10.1.2后,从客户端传ClientDataset的Delta数据到服务端程序时,出现var and out arguments must match parameter错 ...

  5. PHP获得用户的真实IP地址

    <?php /** * 获得用户的真实IP地址 * * @access public * @return string */ function real_ip() { static $reali ...

  6. linux 下用户组、文件权限详解

    参考资料:http://www.cnblogs.com/123-/p/4189072.html

  7. 【Android开发日记】之入门篇(七)——Android数据存储(上)

    在讲解Android的数据源组件——ContentProvider之前我觉得很有必要先弄清楚Android的数据结构. 数据和程序是应用构成的两个核心要素,数据存储永远是应用开发中最重要的主题之一,也 ...

  8. tensorflow高级库

    1.tf.app.flags tf定义了tf.app.flags,用于支持接受命令行传递参数,相当于接受argv.tf.app.flags.DEFINE_xxx()就是添加命令行的optional a ...

  9. (二)Jsoup 查找 DOM 元素

    第一节: Jsoup 查找 DOM 元素 getElementById(String id) 根据 id 来查询 DOM getElementsByTag(String tagName) 根据 tag ...

  10. Nginx 虚拟主机 VirtualHost 配置

    Nginx 是一个轻量级高性能的 Web 服务器, 并发处理能力强, 对资源消耗小, 无论是静态服务器还是小网站, Nginx 表现更加出色, 作为 Apache 的补充和替代使用率越来越高. 我在& ...