传送门: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. centos mysql错误信息处理

    mysql_secure_installation 提示错误:Enter current password for root (enter for none):ERROR 1045 (28000): ...

  2. 说说Android应用的persistent属性(转)

    1 启动persistent应用 在Android系统中,有一种永久性应用.它们对应的AndroidManifest.xml文件里,会将persistent属性设为true,比如: <appli ...

  3. ZOJ 2859 二维线段树

    思路:自己写的第二发二维线段树1A.哈哈,看来对二维的push操作比較了解了:可是还没遇到在两个线段树中同一时候进行push操作的,事实上这题我是想在x维和y维同一时候进行push操作的.可是想了好久 ...

  4. Apach POI 如何拿到有公式的单元格,计算结果

    public static void getFormulaCellValue(){ FileInputStream fis = new FileInputStream("c:/temp/te ...

  5. Xsolla和Hi-Rez工作室联手推行SMITE

    视频游戏店面管理和计费解决方式的领导者,Xsolla.将重拳出击将与Hi-Rez游戏工作室合作.该工作室是一家美国的独立游戏开发商,主要开发MOBA游戏-SMITE. 支持全球600多种支付方式 Xs ...

  6. Node-webkit 介绍

    什么是Node-webkit ? Node-webkit 是Intelproject师rogerwang写的一个基于node.js和chromium的应用程序执行环境,通过node-webkit,我们 ...

  7. Oracle imp exp命令具体解释

    怎样在oracle中导入dmp数据库文件? oracle数据导入导出imp/exp就相当于oracle数据还原与备份.exp命令能够把数据从远程数据库server导出到本地的dmp文件,imp命令能够 ...

  8. SSH三大框架整合配置详细步骤(3)

    5 配置Spring2.5 5.1 基础配置 1)        导入spring包.下载spring-framework-2.5.6并解压后,在spring-framework-2.5.6" ...

  9. Hadoop 解除 “Name node is in safe mode”

    运行Hadoop程序时,有时候会报以下错误:  org.apache.hadoop.dfs.SafeModeException: Cannot delete /user/hadoop/input. N ...

  10. CXF+Spring+Tomcat 案例

    多系统(异构系统)进行交互时,一种良好的方式便是调用Web Service,本示例基于Apache组织的CXF 环境:EclipseJDK6Tomcat6CXF2.6.1Spring3 示例项目结构图 ...