传送门: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. [WinForm]DataGridView列头右键菜单

    [WinForm]DataGridView列头右键菜单 前言 继续"不误正业" - - #,记录一下.有时候有这样的需求:DataGridView的列头菜单可以选择具体显示哪些列, ...

  2. BC一周年B

    #include <cstdio> #include <iostream> #include <algorithm> #include <queue> ...

  3. Echarts 的样例

    jsp页面: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8 ...

  4. MVC在View中页面跳转

    在做人事系统的时候须要用到页面跳转,那么页面跳转究竟用什么方法好呢?依照曾经的思路,我就会这么写. <span style="font-size:18px;">wind ...

  5. 通过SQL SERVER加入系统管理员帐号

    通过SQL SERVER加入系统管理员帐号.当然是须要有足够的权限,远程链接数据库后执行脚本,脚本例如以下: /* 此代码是在master数据库下执行 添加系统管理员:mmcgzs password: ...

  6. 如何离线分析Kafka海量业务消息?1分钟快速为您支招

    场景介绍 说起Kafka,许多使用者对它是又爱又恨.Kafka是一种分布式的.基于发布/订阅的消息系统,其极致体验让人欲罢不能,但操心的运维.复杂的安全策略.可靠性易用性的缺失等,仍需要使用者付出诸多 ...

  7. 利用shuf对数据记录进行随机采样

    最近在用SVM为分类器做实验,但是发现数据量太大(2000k条记录)但是训练时间过长...让我足足等了1天的啊!有人指导说可以先进行一下随机采样,再训练,这样对训练结果不会有太大影响(这个待考证).所 ...

  8. EJB学习笔记六(EJB中的拦截器)

     1.前言 听到拦截器,预计都不陌生,尤其是在Servlet规范中,充分应用了拦截器的概念.EJB3也提供了拦截器的支持,本质上是轻量级的AOP实现.拦截器能够将多个业务方法中的通用逻辑从业务方法中抽 ...

  9. oracle多表关联多字段update

    多表关联多字段update 有代码有J8: update spatial_references set( auth_name, auth_srid, falsex, falsey, xyunits, ...

  10. SSH常见错误

    错误一: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml] ...