bzoj 2002 LinkCutTree
我的第一道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的更多相关文章
- BZOJ 2002 && BZOJ 2409 LCT && BZOJ 3282 初步练习
#include <cstdio> ; inline void Get_Int(int & x) { ; ') ch=getchar(); +ch-'; ch=getchar(); ...
- 以 BZOJ 2002 为例学习有根树LCT(Link-Cut Tree)
以BZOJ 2002 弹飞绵羊为例学习有根树LCT(Link-Cut Tree) 注:本文非常简单,只涉及有根树LCT,对于无根树,LCT还有几个本文没有提到的操作,以后慢慢更新 =v= 知识储备 [ ...
- bzoj 2002 Bounce 弹飞绵羊
bzoj 2002 Bounce 弹飞绵羊 设一个虚拟节点表示被弹飞,则每个点的后继点是唯一确定的,每个点向它的后继点连边,就形成了一颗树. 询问就是问某个节点到虚拟节点的路径长度,修改就删除原来向后 ...
- [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)
[BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...
- lct 模版题 bzoj 2002 2049
很早就有人给我推荐的模版题,然后我最近才刷的(' ' ) 昨天的tree 不知道比他们高到哪里去了,我和他谈笑风生啊! bzoj 2002 弹飞绵羊 重点:这道题的cut和link 由于这道题链 ...
- bzoj 2002: 弹飞绵羊 Link-Cut-Tree
题目: Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置, ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 9071 Solved: 4652[Submi ...
- bzoj 2002 [Hnoi2010]Bounce 弹飞绵羊(LCT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2002 [题意] 给定n个数的序列,i可以跳到i+k[i],需要能够修改k并可以查询跳出 ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 分块
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...
随机推荐
- 深入理解Spring系列之十:DispatcherServlet请求分发源码分析
转载 https://mp.weixin.qq.com/s/-kEjAeQFBYIGb0zRpST4UQ DispatcherServlet是SpringMVC的核心分发器,它实现了请求分发,是处理请 ...
- 【IDEA】与Eclipse "Link with Editor"等价功能设置
Link With Editor是Eclipse内置功能中十分小巧,但却异常实用的一个功能.这个开关按钮 (Toggle Button) 出现在各式导航器视图 ( 例如 Resource Explor ...
- python设计模式之单例模式(一)
前言 单例模式是创建模式中比较常见和常用的模式,在程序执行的整个生命周期只存在一个实例对象. 系列文章 python设计模式之单例模式(一) python设计模式之常用创建模式总结(二) python ...
- 宋牧春: Linux设备树文件结构与解析深度分析(2) 【转】
转自:https://mp.weixin.qq.com/s/WPZSElF3OQPMGqdoldm07A 作者简介 宋牧春,linux内核爱好者,喜欢阅读各种开源代码(uboot.linux.ucos ...
- UVALive 5760 Alice and Bob
题意是黑板上有n个数\(S_i\).每次操作可以把其中一个数减1或者将两个数合并为一个数.一个数变为0时,则不能再对其操作. 思路是发现最大的可操作次数为\( \sum S_i\)+(n - 1).\ ...
- 防范SQL注入
使用占位符的方式写查询语句hibernate
- 中文chrome font-size 10px,11px,12px,rem只为12px解决办法
问题来源: html { font-size: 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); } .form-signin { max-wi ...
- django时差8个小时问题
问题现象: 在用django做好的网站,上传图片后显示的发布时间比当前时间差了8小时 查找问题: 查看服务器系统时间,经查与当前时间一致,无问题 查看数据库中的时间也一样 最终原因: 在setting ...
- 在JAVA中生成RSA秘钥对实现SSH互信
https://blog.csdn.net/u014196729/article/details/51496262 https://blog.csdn.net/u013066244/article/d ...
- [实战]MVC5+EF6+MySql企业网盘实战(26)——音乐列表
写在前面 本篇文章将实现,音乐列表,同样和其他列表的不同之处,在于查询条件的不同. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网 ...