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. Webx pull service

    1.概述 pull service的功能是将对象置入模板中.被pull service放到模板中的对象,不需要应用程序的干预即可直接使用.如果模板没有用到某个对象,则不会产生创建该对象的开销.看起来, ...

  2. ASP.NET jQuery 随笔 使用allValidator插件简单实现客户端验证功能

    首先放出该插件的下载地址:http://pan.baidu.com/s/1Aa3yD,里面有帮助文档,详细了解可以自行下载学习,本章只讲解一些基本的验证功能,页面代码如下: <%@ Page L ...

  3. 使用django+celery+RabbitMQ实现异步执行

    http://www.yu180.com/group/view/259 推荐一个解决框架 https://github.com/maccman/juggernaut Realtime server p ...

  4. Xamarin几十篇博客,roslyn和dotnet也开源了

    .Net 基金会 http://www.dotnetfoundation.org/ 更新的真快,刚打完2的补丁包,3就粗来了............ https://www.visualstudio. ...

  5. Android系统设置— android.provider.Settings

    android.provider.Settings Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS); sta ...

  6. android天气查询(一)websevice之ksoap2软件包的使用

    对于用到天气信息,首先我想: 第一:数据不可能是我测得的,必须是网上的信息. 第二:网上的信息分为好多种,具体哪种比较好一点,这里我总结了两种. 第三:数据JSON怎么解析. 第四:如何提出数据与显示 ...

  7. java实现冒泡排序,选择排序,插入排序,快速排序(简洁版)及性能测试

    1.冒泡排序是排序里面最简单的了,但性能也最差,数量小的时候还可以,数量一多,是非常慢的. 它的时间复杂度是O(n*n),空间复杂度是O(1) 代码如下,很好理解. public void bubbl ...

  8. Failed to upgrade Oracle Cluster Registry configuration(root.sh)

        近期在给客户基于Suse 11 sp3安装Oracle 10g RAC,在安装完clusterware运行/u01/app/crs/root.sh时收到错误提示.Failed to upgra ...

  9. DMA(STM32)

     1.DMA:data memory access //实际的内存存储 注:DMA干活的时候是不须要CPU干涉的 2. ①内存(定义的变量)---外设(寄存器). ②内存---内存 ③外设---外 ...

  10. 实现AJAX局部刷新以及PageMethod方法的使用

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...