LCT..

--------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
 
#define rep( i , n ) for( int i = 0 ; i < n ; ++i )
#define clr( x , c ) memset( x , c , sizeof( x ) )
 
using namespace std;
 
const int maxn = 300000 + 5;
const int maxnode = maxn + 100;
 
int n;
 
struct Node *pt , *null; 
 
struct Node {
Node *ch[ 2 ] , *p , *fa;
int v , x;
bool rev , isRoot;
Node( int _v = 0 ) : v( _v ) , x( _v ) {
ch[ 0 ] = ch[ 1 ] = p = fa = null;
rev = false;
isRoot = true;
}
inline bool d() {
return this == p -> ch[ 1 ];
}
inline void setc( Node* c , int d ) {
ch[ d ] = c;
c -> p = this;
}
inline void relax() {
if( rev ) {
rev = false;
rep( i , 2 ) if( ch[ i ] != null )
   ch[ i ] -> Rev();
}
}
inline void Rev() {
rev ^= 1;
swap( ch[ 0 ] , ch[ 1 ] );
}
inline void setRoot() {
fa = p;
p = null;
isRoot = true;
}
inline void upd() {
x = ch[ 0 ] -> x ^ v ^ ch[ 1 ] -> x;
}
void* operator new( size_t ) {
return pt++;
}
};
 
Node NODE[ maxnode ];
 
void rot( Node* t ) {
Node* p = t -> p;
p -> relax();
t -> relax();
int d = t -> d();
p -> p -> setc( t , p -> d() );
p -> setc( t -> ch[ d ^ 1 ] , d );
t -> setc( p , d ^ 1 );
p -> upd();
if( p -> isRoot ) {
p -> isRoot = false;
t -> isRoot = true;
t -> fa = p -> fa;
}
}
 
void splay( Node* t , Node* f = null ) {
static Node* S[ maxn ];
int top = 0;
for( Node* o = t ; o != null ; o = o -> p ) 
   S[ ++top ] = o;
while( top ) 
   S[ top-- ] -> relax();
for( Node* p = t -> p ; p != f ; p = t -> p ) {
if( p -> p != f )
   p -> d() != t -> d() ? rot( t ) : rot( p );
rot( t );
}
t -> upd();
}
void access( Node* t ) {
for( Node* o = null ; t != null ; o = t , t = t -> fa ) {
splay( t );
t -> ch[ 1 ] -> setRoot();
t -> setc( o , 1 );
}
}
 
void makeRoot( Node* t ) {
access( t );
splay( t );
t -> Rev();
}
 
Node* findRoot( Node* t ) {
access( t );
splay( t );
for( ; t -> ch[ 0 ] != null ; t = t -> ch[ 0 ] ) 
   t -> relax();
splay( t );
return t;
}
 
void join( Node* x , Node* y ) {
makeRoot( x );
x -> fa= y;
splay( y );
}
 
Node* get( Node* x , Node* y ) {
makeRoot( x );
access( y );
splay( y );
return y;
}
 
void cut( Node* x , Node* y ) {
makeRoot( x );
access( y );
splay( x );
x -> setc( null , 1 );
x -> upd();
y -> p = null;
y -> setRoot();
}
 
void init() {
pt = NODE;
null = new Node( 0 );
}
 
Node* V[ maxn ];
 
int main() {
freopen( "test.in" , "r" , stdin );
init();
int m;
cin >> n >> m;
rep( i , n ) {
int v;
scanf( "%d" , &v );
V[ i ] = new Node( v );
}
int x , y , op;
while( m-- ) {
scanf( "%d%d%d" , &op , &x , &y );
x-- , y--;
if( op == 3 ) {
y++;
access( V[ x ] );
splay( V[ x ] );
V[ x ] -> v = y;
V[ x ] -> upd();
} else if( ! op ) {
Node* t = get( V[ x ] , V[ y ] );
printf( "%d\n" , get( V[ x ] , V[ y ] ) -> x );
} else if( op == 1 ) {
if( findRoot( V[ x ] ) != findRoot( V[ y ] ) )
   join( V[ x ] , V[ y ] );
} else {
if( findRoot( V[ x ] ) == findRoot( V[ y ] ) )
   cut( V[ x ] , V[ y ] );
}
}
return 0;
}

--------------------------------------------------------------------------------

3282: Tree

Time Limit: 30 Sec  Memory Limit: 512 MB
Submit: 827  Solved: 341
[Submit][Status][Discuss]

Description

给定N个点以及每个点的权值,要你处理接下来的M个操作。操作有4种。操作从0到3编号。点从1到N编号。

0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和。保证x到y是联通的。

1:后接两个整数(x,y),代表连接x到y,若x到Y已经联通则无需连接。

2:后接两个整数(x,y),代表删除边(x,y),不保证边(x,y)存在。

3:后接两个整数(x,y),代表将点X上的权值变成Y。

Input

第1行两个整数,分别为N和M,代表点数和操作数。

第2行到第N+1行,每行一个整数,整数在[1,10^9]内,代表每个点的权值。

第N+2行到第N+M+1行,每行三个整数,分别代表操作类型和操作所需的量。

Output

对于每一个0号操作,你须输出X到Y的路径上点权的Xor和。

Sample Input

3 3
1
2
3
1 1 2
0 1 2
0 1 1

Sample Output

3
1

HINT

1<=N,M<=300000

Source

BZOJ 3282: Tree( LCT )的更多相关文章

  1. [BZOJ 3282] Tree 【LCT】

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

  2. BZOJ 3282: Tree

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

  3. bzoj 3282: Tree (Link Cut Tree)

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

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

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

  5. BZOJ 2631: tree( LCT )

    LCT...略麻烦... -------------------------------------------------------------------------------- #inclu ...

  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 2631: tree [LCT splay区间]

    2631: tree Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 3854  Solved: 1292[Submit][Status][Discus ...

  9. BZOJ 3282 Tree ——Link-Cut Tree

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

随机推荐

  1. HDU1171-Big Event in HDU

    描述: Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don ...

  2. [LeetCode]题解(python):067-Add Binary

    题目来源: https://leetcode.com/problems/add-binary/ 题意分析: 这题是要将二进制相加,比如“11”,“1”,那么就返回“100”. 题目思路: 模拟加法的过 ...

  3. python中3个帮助函数help、dir、type的使用

    1.help函数:查看模块.函数.变量的详细说明: 查看模块 help("modules") 查看包  help("json") 查看类 help(json.J ...

  4. Myeclipse安装Activiti

    1.将压缩包内activiti文件夹放入Myeclipse\dropins文件夹内并修改activiti文件夹内Link文件指向自己的目录重启Myeclipse(这时打开bpmn文件仍会报错).2.将 ...

  5. perl5 第六章 模式匹配

    第六章 模式匹配 by flamephoenix 一.简介二.匹配操作符三.模式中的特殊字符  1.字符+  2.字符 []和[^]  3.字符 *和?  4.转义字符  5.匹配任意字母或数字  6 ...

  6. BZOJ 3402: [Usaco2009 Open]Hide and Seek 捉迷藏

    题目 3402: [Usaco2009 Open]Hide and Seek 捉迷藏 Time Limit: 3 Sec  Memory Limit: 128 MB Description     贝 ...

  7. hdu 5269 ZYB loves Xor I(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5269 思路分析:当lowbit(AxorB)=2p 时,表示A与B的二进制表示的0-p-1位相等,第p ...

  8. [Django实战] 第8篇 - 分页列表

    当用户登录成功后,首先看到的是他自己之前提交的任务列表,本篇将实现该页面. 视图(views.py)里定义如下: from django.core.paginator import Paginator ...

  9. 变脸不变质的桥梁模式(Bridge Pattern)

    有一哥们是搞山寨货的,什么流行就搞什么.自己有个厂子,前些时间服装挣钱,就生产衣服,如今搞手机挣钱,搞手机,这哥们非常聪明,就换了个产品,工人,厂房都不变.他是怎么做到的?用类图来模拟一下: 由类图能 ...

  10. SQL Server 中 RAISERROR 的用法(转)

    在存储过程中进程会处理一些逻辑性的错误,如:将RMB转换为USD时,没有查询到想要的汇率 这个时候最好在存储过程中抛个异常,方便自己查找错误信息... 其语法如下: RAISERROR ( { msg ...