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 ...
随机推荐
- U盘出现大量乱码文件,并且不能彻底删除
问题如图所示: 问题出现原因:不正常的插拔等情况造成的,导致U盘的文件分配表错乱了 解决方法:参考http://bbs.cfanclub.net/thread-405004-1-1.html 运行ch ...
- Unity MMO 参考数值
贴图格式: iOS :RGBA 32 (pvrtc 4 ) Android : RGB Compresed ETC 4 或 RGBA 32 . DrawCall: 总计Drawcall 平均 100 ...
- vc6列表框多选时,获取哪些项被选中
//vc6列表框多选时,获取哪些项被选中...... void CWebcyzDlg::OnButton2() { int n = m_mylist1.GetSelCount();//首先获取一共有多 ...
- C++ Class与Struct的区别
转自某楼层的回复http://bbs.csdn.net/topics/280085643 首先,讨论这个问题应该仅从语法上讨论,如果讨论不同人之间编程风格上的差异,那这个问题是没有答案的.毕竟不同的人 ...
- List基本用法
List最为Collection接口的子接口,当然可以使用Collection接口里的全部方法.而且由于List是有序集合,因此List集合里增加了一些根据索引来操作集合元素的方法: public c ...
- 4、GitLab 创建、删除、修改项目
一.gitLab创建项目 1.创建用户组 2.填写组信息后单击“Create group” 其中:“Group path”将显示在git路径中 3.选择需要加入该组的“用户”和“角色”后点击“Add ...
- 主机名/etc/hosts文件的作用
1,/etc/hosts,主机名ip配置文件. # Do not remove the following line, or various programs # that require netwo ...
- 在eclipse中使用Maven3(笔记二)
笔记本二 在Eclipse 中使用Maven 第一节:m2eclipse 插件安装 打开Eclipse,点击菜单Help - > Install New Software 点击Add 按钮N ...
- sad 关于一些html5新属性还需要用https才能支持
像我昨天在搞一个录音的小东西 在本地正常录音正常播放 但是放到线上环境http环境上就出现了如上的错误 功能都不能正常使用 然后就改成https线上环境 然后就正常了 如上 大家有什么赐教的欢迎留言 ...
- LeetCode679. 24 Game
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...