LCT...略麻烦...

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

#include<cstdio>
#include<algorithm>
#include<cstring>
#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 = 100000 + 5;
const int maxnode = maxn + 100;
const int MOD = 51061;
const int maxedge = maxn << 1;
 
#define mod( x ) ( ( x ) %= MOD )
 
int n;
 
struct edge {
int to;
edge* next;
};
 
edge* head[ maxn ];
edge EDGE[ maxedge ];
edge* e_pt;
 
void add( int u , int v ) {
e_pt -> to = v;
e_pt -> next = head[ u ];
head[ u ] = e_pt++;
}
 
#define add_edge( u , v ) add( u , v ) , add( v , u )
 
void edge_init() {
e_pt = EDGE;
clr( head , 0 );
}
 
struct Node *pt , *null;
 
struct Node {
Node *ch[ 2 ] , *p , *fa;
int add , mul , sum , v , s;
bool isRoot , rev;
Node() : v( 1 ) , add( 0 ) , mul( 1 ) , sum( 1 ) , s( 1 ) {
ch[ 0 ] = ch[ 1 ] = p = fa = null;
isRoot = true;
rev = false;
}
inline void setc( Node* c , int d ) {
ch[ d ] = c;
c -> p = this;
}
inline bool d() {
return this == p -> ch[ 1 ];
}
inline void relax() {
if( rev ) {
rev = false;
rep( i , 2 ) if( ch[ i ] != null )
   ch[ i ] -> Rev();
}
if( mul != 1 ) {
rep( i , 2 ) if( ch[ i ] != null ) 
   ch[ i ] -> Mul( mul );
mul = 1;
}
if( add ) {
rep( i , 2 ) if( ch[ i ] != null )
   ch[ i ] -> Add( add );
add = 0;
}
}
inline void upd() {
s = ch[ 0 ] -> s + ch[ 1 ] -> s + 1;
mod( sum = ch[ 0 ] -> sum + v + ch[ 1 ] -> sum );
}
inline void Rev() {
rev ^= 1;
swap( ch[ 0 ] , ch[ 1 ] );
}
inline void Add( int ad ) {
mod( add += ad );
mod( sum += 1LL * ad * s % MOD );
mod( v += ad );
}
inline void Mul( int mu ) {
add = 1LL * add * mu % MOD;
mul = 1LL * mul * mu % MOD;
sum = 1LL * sum * mu % MOD;
v = 1LL * v * mu % MOD;
}
inline void setRoot() {
fa = p;
p = null;
isRoot = true;
}
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 cut( Node* x , Node* y ) {
makeRoot( x );
access( y );
splay( x );
x -> setc( null , 1 );
x -> upd();
y -> p = null;
y -> setRoot();
}
 
void join( Node* x , Node* y ) {
makeRoot( x );
x -> fa = y;
}
 
Node* get( Node* x , Node* y ) {
makeRoot( x );
access( y );
splay( y );
return y;
}
 
void node_init() {
pt = NODE;
null = new( Node );
null -> s = null -> v = null -> sum = 0;
}
 
Node* V[ maxn ];
 
void dfs( int x , int par ) {
for( edge* e = head[ x ] ; e ; e = e -> next ) {
int to = e -> to;
if( to == par ) continue;
dfs( to , x );
V[ to ] -> fa = V[ x ];
}
}
 
void build_tree() {
rep( i , n ) V[ i ] = new( Node );
dfs( 0 , -1 );
}
 
int main() {
freopen( "test.in" , "r" , stdin );
freopen( "test.out" , "w" , stdout );
edge_init();
node_init();
int q , u , v;
cin >> n >> q;
rep( i , n - 1 ) {
scanf( "%d%d" , &u , &v );
u-- , v--;
add_edge( u , v );
}
build_tree();
char op;
while( q-- ) {
scanf( " %c%d%d" , &op , &u , &v );
u-- , v--;
if( op == '-' ) {
cut( V[ u ] , V[ v ] );
scanf( "%d%d" , &u , &v );
u-- , v--;
join( V[ u ] , V[ v ] );
} else if( op == '/' ) {
printf( "%d\n" , get( V[ u ] , V[ v ] ) -> sum );
} else {
int c;
scanf( "%d" , &c );
mod( c );
op != '+' ? get( V[ u ] , V[ v ] ) -> Mul( c ) : get( V[ u ] , V[ v ] ) -> Add( c );
}
}
return 0;
}

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

2631: tree

Time Limit: 30 Sec  Memory Limit: 128 MB
Submit: 2454  Solved: 819
[Submit][Status][Discuss]

Description

 一棵n个点的树,每个点的初始权值为1。对于这棵树有q个操作,每个操作为以下四种操作之一:
+ u v c:将u到v的路径上的点的权值都加上自然数c;
- u1 v1 u2 v2:将树中原有的边(u1,v1)删除,加入一条新边(u2,v2),保证操作完之后仍然是一棵树;
* u v c:将u到v的路径上的点的权值都乘上自然数c;
/ u v:询问u到v的路径上的点的权值和,求出答案对于51061的余数。

Input

  第一行两个整数n,q
接下来n-1行每行两个正整数u,v,描述这棵树
接下来q行,每行描述一个操作

Output

  对于每个/对应的答案输出一行

Sample Input

3 2
1 2
2 3
* 1 3 4
/ 1 1

Sample Output

4

HINT

数据规模和约定

10%的数据保证,1<=n,q<=2000

另外15%的数据保证,1<=n,q<=5*10^4,没有-操作,并且初始树为一条链

另外35%的数据保证,1<=n,q<=5*10^4,没有-操作

100%的数据保证,1<=n,q<=10^5,0<=c<=10^4

Source

BZOJ 2631: tree( LCT )的更多相关文章

  1. BZOJ 2631: tree [LCT splay区间]

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

  2. [BZOJ - 2631] tree 【LCT】

    题目链接:BZOJ - 2631 题目分析 LCT,像线段树区间乘,区间加那样打标记. 这道题我调了一下午. 提交之后TLE了,我一直以为是写错了导致了死循环. 于是一直在排查错误.直到.. 直到我看 ...

  3. bzoj 2631: tree 动态树+常数优化

    2631: tree Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 1716  Solved: 576[Submit][Status] Descrip ...

  4. 【BZOJ】2631: tree LCT

    [题意]给定n个点的树,每个点初始权值为1,m次操作:1.x到y的点加值,2.断一条边并连一条边,保证仍是树,3.x到y的点乘值,4.x到y的点权值和取模.n,m<=10^5. [算法]Link ...

  5. BZOJ 2631 tree / Luogu P1501 [国家集训队]Tree II (LCT,多重标记)

    题意 一棵树,有删边加边,有一条链加/乘一个数,有询问一条链的和 分析 LCT,像线段树一样维护两个标记(再加上翻转标记就是三个),维护size,就行了 CODE #include<bits/s ...

  6. BZOJ 2631 tree | Luogu P1501 [国家集训队]Tree II (LCT 多重标记下放)

    链接:https://www.luogu.org/problemnew/show/P1501 题面: 题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: ...

  7. BZOJ 3282: Tree( LCT )

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

  8. BZOJ 2631 tree(动态树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2631 [题目大意] 要求支持链修改,链查询,边断开,连接操作 [题解] 链修改分乘和加 ...

  9. BZOJ 2631 tree 动态树(Link-Cut-Tree)

    题目大意:维护一种树形数据结构.支持下面操作: 1.树上两点之间的点权值+k. 2.删除一条边.添加一条边,保证加边之后还是一棵树. 3.树上两点之间点权值*k. 4.询问树上两点时间点的权值和. 思 ...

随机推荐

  1. Hadoop学习笔记(1)概述

    写在学习笔记之前的话: 寒假已经开始好几天了,似乎按现在的时间算,明天就要过年了.在家的这几天,该忙的也都差不多了,其实也都是瞎忙.接下来的几点,哪里也不去了,静静的呆在家里学点东西.所以学习一下Ha ...

  2. java的IO流包装不当导致从网页获取的数据出现乱码

    从网页上获取数据时必须要注意字符集的问题.处理不慎确实苦不堪言. 例如通过URL连接时,将字节流InputStream包装成字符流(以便直接存为String)时,一定要注意加上charsetName这 ...

  3. synchronized和vilatile

    第一个程序 public class Test06 implements Runnable{ public int a = 0; public static void main(String[] ar ...

  4. css 一些事

    1.两个div左边的固定宽度右边的自动填充的css <div id="main" style="width:98%;"> <div id=&q ...

  5. 国内外主流BI厂商对比

    BI(Business Intelligence),即商业智能或者商务智能,它是一套完整的解决方案,用来将企业中现有的数据进行有效的整合,快速准确的提供报表并提出决策依据,帮助企业做出明智的业务经营决 ...

  6. 给内置对象或自定义对象添加存取器属性(getter setter)的方法总结

    funct = { get: function() { return this._x }, set: function(value) { this._x = value } } function Ob ...

  7. Qt官方开发环境生成的exe发布方式--使用windeployqt(windeployqt是单独的程序,放在低版本qt4目录下也可以运行的)

    Qt 官方开发环境使用的动态链接库方式,在发布生成的exe程序时,需要复制一大堆 dll,如果自己去复制dll,很可能丢三落四,导致exe在别的电脑里无法正常运行.因此 Qt 官方开发环境里自带了一个 ...

  8. VS前台设计中的缺陷

    程序的前台界面设计我一般都是Blend中完成的,因为在VS中前台比较难控制.而效果之内的就更不用说了.今天写了个小程序用来启动其他程序的.运行界面如下: 这是尺寸和距离刚刚好的,我们去Blend当中看 ...

  9. 基于Visual C++2013拆解世界五百强面试题--题10-找出N个数种最大的K个数

    有一亿个整数,请找出最大的 1000 个,要求时间越短越好, 空间占用越好越好. 如果不考虑时间效率,很容易想到解决方法,我们只需存储前一千个数, 然后依次读入后面的数和这一千个数组比较,替换其中比较 ...

  10. uva311 - Packets(贪心)

    题目:311 - Packets 题目大意:给出1*1, 2*2,3 *3, 4*4, 5*5, 6*6的箱子的个数,如今有若干个6*6的箱子,问最少用多少个箱子能够将给定的箱子都装进去. 解题思路: ...