我的第一道LCT题(居然1A,O(∩_∩)O哈哈~)

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2002

大概题意:

给一颗有根树,维护每个节点的深度(到根节点的边数),支持断开子树并把它连接到任意节点。

题解:

Link Cut Tree

 /**************************************************************
Problem: 2002
User: idy002
Language: C++
Result: Accepted
Time:1644 ms
Memory:5984 kb
****************************************************************/ #include <cstdio>
#include <iostream>
#define maxn 200010
using namespace std; struct LCT {
int pre[maxn], son[maxn][], siz[maxn];
int pnt[maxn]; void init( int n ) {
for( int i=; i<=n; i++ ) {
int nd = i;
pre[nd] = son[nd][] = son[nd][] = ;
pnt[nd] = ;
siz[nd] = ;
}
}
void update( int nd ) {
siz[nd] = siz[son[nd][]]+siz[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; pre[nd] = s;
pre[s] = p;
if( ss ) pre[ss] = nd; if( pnt[nd] ) {
pnt[s] = pnt[nd];
pnt[nd] = ;
} update( nd );
update( s );
}
void splay( int nd, int top ) {
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 cut( int fa ) {
int s = son[fa][];
if( s ) {
son[fa][] = ;
pre[s] = ;
pnt[s] = fa;
}
}
void con( int sn ) {
int fa = pnt[sn];
pnt[sn] = ;
son[fa][] = sn;
pre[sn] = fa;
}
void access( int nd ) {
splay( nd, );
cut( nd );
while( pnt[nd] ) {
splay( pnt[nd], );
cut(pnt[nd]);
con(nd);
splay( nd, );
}
}
void cuttree( int nd ) {
access( nd );
pre[ son[nd][] ] = ;
son[nd][] = ;
}
void contree( int nd, int fa ) {
access( nd );
pnt[nd] = fa;
access( nd );
}
int getdeep( int nd ) {
access( nd );
return siz[son[nd][]];
}
}; int n, m;
int a[maxn];
LCT LT;
int main() {
scanf( "%d", &n );
LT.init(n+);
for( int i=,w; i<=n; i++ ) {
scanf( "%d", &w );
int fa = min( i+w, n+ );
LT.contree( i, fa );
}
scanf( "%d", &m );
for( int i=,opt,x,y; i<=m; i++ ) {
scanf( "%d", &opt );
if( opt== ) { // query
scanf( "%d", &x );
x++;
printf( "%d\n", LT.getdeep( x ) );
} else {
scanf( "%d%d", &x, &y );
x++;
int oldf = min( x+a[x], n+ );
int newf = min( x+y, n+ );
if( oldf==newf ) continue;
a[x] = y;
LT.cuttree( x );
LT.contree( x, newf );
}
}
}

bzoj 2002 LinkCutTree的更多相关文章

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

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

  2. 以 BZOJ 2002 为例学习有根树LCT(Link-Cut Tree)

    以BZOJ 2002 弹飞绵羊为例学习有根树LCT(Link-Cut Tree) 注:本文非常简单,只涉及有根树LCT,对于无根树,LCT还有几个本文没有提到的操作,以后慢慢更新 =v= 知识储备 [ ...

  3. bzoj 2002 Bounce 弹飞绵羊

    bzoj 2002 Bounce 弹飞绵羊 设一个虚拟节点表示被弹飞,则每个点的后继点是唯一确定的,每个点向它的后继点连边,就形成了一颗树. 询问就是问某个节点到虚拟节点的路径长度,修改就删除原来向后 ...

  4. [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)

    [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...

  5. lct 模版题 bzoj 2002 2049

    很早就有人给我推荐的模版题,然后我最近才刷的(' '    ) 昨天的tree 不知道比他们高到哪里去了,我和他谈笑风生啊! bzoj 2002 弹飞绵羊 重点:这道题的cut和link 由于这道题链 ...

  6. bzoj 2002: 弹飞绵羊 Link-Cut-Tree

    题目: Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置, ...

  7. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 9071  Solved: 4652[Submi ...

  8. bzoj 2002 [Hnoi2010]Bounce 弹飞绵羊(LCT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2002 [题意] 给定n个数的序列,i可以跳到i+k[i],需要能够修改k并可以查询跳出 ...

  9. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 分块

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...

随机推荐

  1. low逼三人组、nb二人组、归并、希尔排序----小结

  2. Freemaker如何遍历key为non-string类型的map?

    (一) 前置知识 Freemaker默认配置下会使用SimpleHash去包装后台传递的hashmap,下段摘抄自官方reference 同样,当你传递进去一个hashmap实例时,会替换为一个sim ...

  3. 非交互式shell脚本案例-实现自主从oracle数据库获取相关数据,并在制定目录生成相应规则的文件脚本

    get_task_id 脚本内容 #!/usr/bin/expect#配置登陆数据库的端口set port 22#配置登陆数据库的ip地址set oracleip 10.0.4.41#配置数据库实例名 ...

  4. C#子线程中更新ui

    本文实例总结了C#子线程更新UI控件的方法,对于桌面应用程序设计的UI界面控制来说非常有实用价值.分享给大家供大家参考之用.具体分析如下: 一般在winform C/S程序中经常会在子线程中更新控件的 ...

  5. /bin、/sbin、/usr/bin、/usr/sbin目录Linux执行文档的区别

    /bin./sbin./usr/bin./usr/sbin目录的区别   在linux下我们经常用到的四个应用程序的目录是/bin./sbin./usr/bin./usr/sbin .而四者存放的文件 ...

  6. leetcode 之Search in Rotated Sorted Array(四)

    描述 Follow up for ”Search in Rotated Sorted Array”: What if duplicates are allowed?    Would this aff ...

  7. tomcat已启动,使用maven的deploy发布后,根据路径打开浏览器访问时报错HTTP Status 500 - Error instantiating servlet class

    web项目中请求出现错误,如下: HTTP Status 500 - Error instantiating servlet class XXXX类 type Exception report mes ...

  8. linux中$的各种含义

    我们先写一个简单的脚本,执行以后再解释各个变量的意义   # touch variable # vi variable   脚本内容如下:   #!/bin/sh echo "number: ...

  9. 《数据结构与STL-第二章 线性表》读书笔记

    线性表 定义 线性表(linear list)是由零个或多个相同类型的数据元素构成的有限序列. 存储结构 顺序存储 最简单的存储方法是顺序存储法,即把线性表的数据元素按照逻辑次序顺序地放在一组地址连续 ...

  10. nginx配置tomcat集群

    显示nginx的核心配置 #user nobody;worker_processes 1; events { worker_connections 1024;    #并发连接数} http { in ...