bzoj2002 [Hnoi2010]Bounce 弹飞绵羊【LCT】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2002
第一道LCT,调了3天,发现是智障bug,我的青春。。。
主要参考了黄学长的代码,也没啥好说的,反正就是LCT,就当存一份模版好了。
#include <cstdio> const int maxn = 200005; int n, m, t1, t2, t3, ori_tree_fa[maxn];
int left[maxn], right[maxn], fa[maxn], siz[maxn], root;
int stk[maxn], top, pushdown_tem;
bool rev[maxn]; inline void pushup(int x) {
siz[x] = siz[left[x]] + siz[right[x]] + 1;
}
inline void pushdown(int x) {
if (rev[x]) {
rev[x] = false;
rev[left[x]] = !rev[left[x]];
rev[right[x]] = !rev[right[x]];
pushdown_tem = left[x];
left[x] = right[x];
right[x] = pushdown_tem;
}
}
inline bool isroot(int x) {
return left[fa[x]] != x && right[fa[x]] != x;
}
inline void leftrotate(int x) {
int y = fa[x];
if (!isroot(y)) {
if (y == left[fa[y]]) {
left[fa[y]] = x;
}
else {
right[fa[y]] = x;
}
}
fa[x] = fa[y];
right[y] = left[x];
fa[left[x]] = y;
fa[y] = x;
left[x] = y;
pushup(y);
pushup(x);
}
inline void rightrotate(int x) {
int y = fa[x];
if (!isroot(y)) {
if (y == left[fa[y]]) {
left[fa[y]] = x;
}
else {
right[fa[y]] = x;
}
}
fa[x] = fa[y];
left[y] = right[x];
fa[right[x]] = y;
fa[y] = x;
right[x] = y;
pushup(y);
pushup(x);
}
inline void splay(int x) {
int p;
top = 0;
stk[top++] = x;
for (int i = x; !isroot(i); i = fa[i]) {
stk[top++] = fa[i];
}
for (int i = top - 1; i >= 0; --i) {
pushdown(stk[i]);
}
while (!isroot(x)) {
p = fa[x];
if (isroot(p)) {
if (x == right[p]) {
leftrotate(x);
}
else {
rightrotate(x);
}
}
else {
if (p == right[fa[p]]) {
if (x == right[p]) {
leftrotate(p);
leftrotate(x);
}
else {
rightrotate(x);
leftrotate(x);
}
}
else {
if (x == right[p]) {
leftrotate(x);
rightrotate(x);
}
else {
rightrotate(p);
rightrotate(x);
}
}
}
}
}
inline void acc(int x) {
int y = 0;
while (x) {
splay(x);
right[x] = y;
pushup(x);
y = x;
x = fa[x];
}
}
inline void make_root(int x) {
acc(x);
splay(x);
rev[x] = !rev[x];
}
inline void joyn(int x, int y) {
make_root(x);
fa[x] = y;
splay(x);
}
inline void cutt(int x, int y) {
make_root(x);
acc(y);
splay(y);
left[y] = fa[x] = 0;
pushup(y);
} int main(void) {
//freopen("in.txt", "r", stdin);
scanf("%d", &n);
root = n + 1;
for (int i = 1; i <= n; ++i) {
scanf("%d", &t2);
t2 = i + t2 < root? i + t2: root;
siz[i] = 1;
fa[i] = ori_tree_fa[i] = t2;
}
siz[root] = 1;
scanf("%d", &m);
while (m--) {
scanf("%d%d", &t1, &t2);
++t2;
if (t1 == 1) {
make_root(root);
acc(t2);
splay(t2);
printf("%d\n", siz[left[t2]]);
}
else {
scanf("%d", &t3);
t3 = t2 + t3 < root? t2 + t3: root;
cutt(t2, ori_tree_fa[t2]);
joyn(t2, t3);
ori_tree_fa[t2] = t3;
}
}
return 0;
}
我说的那个智障bug是指,splay开时前,pushdown那里,我错写成了
for (int i = x; !isroot(i); i = fa[i]) {
stk[top++] = i;
}
被这个弄了好久。
bzoj2002 [Hnoi2010]Bounce 弹飞绵羊【LCT】的更多相关文章
- [BZOJ2002] [Hnoi2010] Bounce 弹飞绵羊 (LCT)
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
- [BZOJ2002][Hnoi2010]Bounce弹飞绵羊 LCT
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 建图,每次往后面跳就往目标位置连边,将跳出界的点设为同一个点.对于修改操作发现可以用 ...
- BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 【LCT】【分块】
BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始, ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 LCT
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...
- [bzoj2002][Hnoi2010]Bounce弹飞绵羊_LCT
Bounce弹飞绵羊 bzoj-2002 Hnoi-2010 题目大意:n个格子,每一个格子有一个弹簧,第i个格子会将经过的绵羊往后弹k[i]个,达到i+k[i].如果i+k[i]不存在,就表示这只绵 ...
- bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 [分块][LCT]
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
- BZOJ2002: [Hnoi2010]Bounce 弹飞绵羊(LCT)
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在 他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装 ...
- [bzoj2002][Hnoi2010]Bounce弹飞绵羊——分块
Brief description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装 ...
- bzoj2002 [Hnoi2010]Bounce 弹飞绵羊【分块】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 这一题除了LCT解法,还有一种更巧妙,代码量更少的解法,就是分块.先想,如果仅仅记录每 ...
- bzoj2002 [Hnoi2010]Bounce 弹飞绵羊——分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 第一次用分块,感觉超方便啊: 如果记录每个点的弹力系数,那么是O(1)修改O(n)查询 ...
随机推荐
- ArcGIS Engine 中的绘制与编辑
1.线段绘制 基本步骤 构建形状 1. 创建 IPoint IPoint m_Point = new PointClass(); m_Point.PutCoords(x, y); 2. 创建 IPoi ...
- Go -- 多个go文件包名都是main
用go run *.go 或 go run one.go two.go main.go
- 系统安全攻防战:DLL注入技术详解
DLL注入是一种允许攻击者在另一个进程的地址空间的上下文中运行任意代码的技术.攻击者使用DLL注入的过程中如果被赋予过多的运行特权,那么攻击者就很有可能会在DLL文件中嵌入自己的恶意攻击代码以获取更高 ...
- 从PRISM开始学WPF(一)WPF?
从PRISM开始学WPF(一)WPF? 我最近打算学习WPF ,在寻找MVVM框架的时候发现了PRISM,在此之前还从一些博客上了解了其他的MVVM框架,比如浅谈WPF中的MVVM框架--MVVM ...
- Tcl学习之--列表|字典
[列表|字典] Tcl使用列表来处理各种集合,比方一个目录中的全部文件,以及一个组件的全部选项.最简单的列表就是包括由随意个空格.制表符.换行符.分隔的随意多个元素的字符串.比方: JerryAlic ...
- NS3网络仿真(9): 构建以太网帧
快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载,但请保留作者信息 在NS3使用了一个叫Packet的类来表示一个数据帧,本节尝试用它构造一个以太网帧. 以下是一个典 ...
- [办公应用]从美国带回来的兄弟牌brother打印机如何处理
周末帮朋友去调一台国外带回来的brother一体机,型号MFC-J630W.这是她单位老板从国外带回来的,说是便宜:不过她说只有英文说明书,她不太会操作.我想这还不容易么,就满口答应下来了.我先到br ...
- oracle 10g 实例用localhost无法访问的处理
我在笔记本上安装了一个Oracle10g,安装好了之后,查看E:\oracle\product\10.2.0\db_1\network\ADMIN\tnsnames.ora文件,发现SID对应的IP地 ...
- QmlWinExtras
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/zhengtianzuo06/article/details/78404961QtWinExtras是 ...
- BZOJ1016 && JSOI2008] 最小生成树计数
题目链接:id=1016">点击打开链接 裸题 #pragma comment(linker, "/STACK:1024000000,1024000000") #i ...