传送门: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】的更多相关文章

  1. [BZOJ2002] [Hnoi2010] Bounce 弹飞绵羊 (LCT)

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

  2. [BZOJ2002][Hnoi2010]Bounce弹飞绵羊 LCT

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 建图,每次往后面跳就往目标位置连边,将跳出界的点设为同一个点.对于修改操作发现可以用 ...

  3. BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 【LCT】【分块】

    BZOJ2002 Hnoi2010 Bounce 弹飞绵羊 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始, ...

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

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

  5. [bzoj2002][Hnoi2010]Bounce弹飞绵羊_LCT

    Bounce弹飞绵羊 bzoj-2002 Hnoi-2010 题目大意:n个格子,每一个格子有一个弹簧,第i个格子会将经过的绵羊往后弹k[i]个,达到i+k[i].如果i+k[i]不存在,就表示这只绵 ...

  6. bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 [分块][LCT]

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

  7. BZOJ2002: [Hnoi2010]Bounce 弹飞绵羊(LCT)

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

  8. [bzoj2002][Hnoi2010]Bounce弹飞绵羊——分块

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

  9. bzoj2002 [Hnoi2010]Bounce 弹飞绵羊【分块】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 这一题除了LCT解法,还有一种更巧妙,代码量更少的解法,就是分块.先想,如果仅仅记录每 ...

  10. bzoj2002 [Hnoi2010]Bounce 弹飞绵羊——分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 第一次用分块,感觉超方便啊: 如果记录每个点的弹力系数,那么是O(1)修改O(n)查询 ...

随机推荐

  1. Java二维码的解码和编码

    原文:http://www.open-open.com/code/view/1430906793866 import java.io.File; import java.util.Hashtable; ...

  2. Meteor在手机上运行

    在本章中,我们将学习如何在Android设备上运行你的应用程序.最近Meteor刚刚添加此功能适用于Windows操作系统,所以我们需要更新 Meteor 应用到 1.3测试版. 注 在写的时候本教程 ...

  3. 最新---java多线程下载文件

    import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; impo ...

  4. MYSQL将时间格式化

    SELECT *,DATE_FORMAT(FROM_UNIXTIME(createtime), "%Y/%m/%d %H:%i:%s") AS dateFormat FROM `I ...

  5. eclipse maven 插件的安装和配置

    maven3 安装: 安装 Maven 之前要求先确定你的 JDK 已经安装配置完毕.Maven是 Apache 下的一个项目.眼下最新版本号是 3.0.4.我用的也是这个. 首先去官网下载 Mave ...

  6. 追踪分布式Memcached默认的一致性hash算法

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  7. MVC+ZTree大数据异步树加载

    实例部分: 首先是为ZTree提供的数据规范,定义一个标准的接口,这样对于前台调用是清楚的,简单的,因为它返回的JSON数据将与ZTree默认的数据元素保持一致 /// <summary> ...

  8. makefile redefinition or previous definition

    operation.h:4: error: redefinition of 'class operation' operation.h:5: error: previous definition of ...

  9. 2016/04/26 权限 数据库mydb2 五个表 分别是 1,用户 2,角色 3,权限 4,用户对应的角色 5,角色对应的权限

    权限:   1,后台分配角色     角色对应权限    2,各用户通过登录页面登录    查看到各自的权限 五个页面   加引入一个jquery-1.11.2.min.js 1,guanli.php ...

  10. 'cmd' 不是内部或外部命令,也不是可运行的程序 或批处理文件。

    'cmd' 不是内部或外部命令,也不是可运行的程序或批处理文件. Path 添加 %SystemRoot%/system32;%SystemRoot%;%SystemRoot%/System32/Wb ...