题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002

题意:加边,删边,查询到根的距离。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int ch[maxn][2], pre[maxn], rev[maxn], Next[maxn];
int size[maxn];
bool rt[maxn];
void Update_Rev(int r){
if(!r) return;
swap(ch[r][0], ch[r][1]);
rev[r] ^= 1;
}
void push_down(int r){
if(rev[r]){
Update_Rev(ch[r][0]);
Update_Rev(ch[r][1]);
rev[r] = 0;
}
}
void push_up(int r){
size[r] = size[ch[r][0]]+size[ch[r][1]]+1;
}
void Rotate(int x){
int y = pre[x], kind = ch[y][1] == x;
ch[y][kind] = ch[x][!kind];
pre[ch[y][kind]] = y;
pre[x] = pre[y];
pre[y] = x;
ch[x][!kind] = y;
if(rt[y])
rt[y] = false, rt[x] = true;
else
ch[pre[x]][ch[pre[x]][1]==y]=x;
push_up(y);
}
void P(int r){
if(!rt[r]) P(pre[r]);
push_down(r);
}
void Splay(int r){
P(r);
while(!rt[r]){
int f = pre[r], ff = pre[f];
if(rt[f])
Rotate(r);
else if((ch[ff][1]==f)==(ch[f][1]==r))
Rotate(f), Rotate(r);
else Rotate(r), Rotate(r);
}
push_up(r);
}
int Access(int x){
int y = 0;
for(;x;x=pre[y=x]){
Splay(x);
rt[ch[x][1]] = true, rt[ch[x][1]=y] = false;
push_up(x);
}
return y;
}
void mroot(int r){
Access(r);
Splay(r);
Update_Rev(r);
}
void link(int u, int v){
mroot(u);
pre[u] = v;
}
void cut(int u, int v){
mroot(u);
Splay(v);
pre[ch[v][0]] = pre[v];
pre[v] = 0;
rt[ch[v][0]] = true;
ch[v][0] = 0;
push_up(v);
}
int n, m;
int main()
{
scanf("%d", &n);
for(int i=0; i<=n+1; i++){
pre[i]=0;
ch[i][0]=ch[i][1]=0;
rev[i]=0;
rt[i] = true;
size[i] = 0;
}
for(int i=1,t; i<=n; i++){
scanf("%d", &t);
t = min(i+t, n+1);
pre[i] = Next[i] = t;
size[i] = 1;
}
size[n+1] = 1;
scanf("%d", &m);
int op, x, y;
for(int i=1; i<=m; i++){
scanf("%d%d", &op,&x);
x++;
if(op==1){
mroot(n+1);
Access(x);
Splay(x);
printf("%d\n", size[ch[x][0]]);
}
else{
scanf("%d", &y);
y = min(x+y,n+1);
cut(x,Next[x]);
link(x,y);
Next[x]=y;
}
}
return 0;
}

BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 动态树的更多相关文章

  1. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 (动态树LCT)

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 2843  Solved: 1519[Submi ...

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

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

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

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 9071  Solved: 4652[Submi ...

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

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

  5. bzoj 2002: [Hnoi2010]Bounce 弹飞绵羊 動態樹

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 4055  Solved: 2172[Submi ...

  6. bzoj 2002 : [Hnoi2010]Bounce 弹飞绵羊 (LCT)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 题面: 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: ...

  7. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 【分块】

    任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 ...

  8. BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊:分块

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题意: 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆 ...

  9. 【刷题】BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊

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

随机推荐

  1. css边框以及其他常用样式

    1. 边框是1像素,实体的,红色的. <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  2. 个人vim配置

    YouCompletMe支持golang cd .vim/bundle/YouCompleteMe ./install.sh --clang-completer --go-completer clan ...

  3. BZOJ3523 [Poi2014]Bricks 【贪心】

    题目链接 BZOJ3523 题解 简单的贪心题 优先与上一个不一样且数量最多的,如果有多个相同,则优先选择非结尾颜色 比较显然,但不知怎么证 #include<algorithm> #in ...

  4. CF993E Nikita and Order Statistics

    小于x的赋值为1,否则为0 区间等于k的个数 求0~n连续的n+1个k? N<=1e5? FFT! 考虑卷积建模:用下标相加实现转移到位,数值相乘类比乘法原理! 法一: 分治,然后FFT没了 法 ...

  5. 专题训练之2-sat

    推荐几篇博客:https://blog.csdn.net/JarjingX/article/details/8521690 研究总结2-sat问题 https://blog.csdn.net/wher ...

  6. spring全局异常处理 自定义返回数据结构

    在写api接口中,正常返回和异常错误返回我们都希望很清楚的将这些信息清楚的返回给用户,出现异常情况下需要清楚的知道是参数异常还是未知异常,而不是返回一个不正确的数据结构. 所以此处只针对写api接口时 ...

  7. Codeforces Round #532 (Div. 2):F. Ivan and Burgers(贪心+异或基)

    F. Ivan and Burgers 题目链接:https://codeforces.com/contest/1100/problem/F 题意: 给出n个数,然后有多个询问,每次回答询问所给出的区 ...

  8. JAVA对象的深度克隆

    有时候,我们需要把对象A的所有值复制给对象B(B = A),但是这样用等号给赋值你会发现,当B中的某个对象值改变时,同时也会修改到A中相应对象的值! 也许你会说,用clone()不就行了?!你的想法只 ...

  9. FreeRTOS - configASSERT(断言)的使用

    原文地址:http://www.cnblogs.com/god-of-death/p/6891400.html  FreeRTOS中的断言函数configASSERT()和标准C中的断言函数asser ...

  10. 不可不知的robots.txt文件

    robots.txt基本介绍 robots.txt是一个纯文本文件,在这个文件中网站管理者可以声明该网站中不想被robots访问的部分,或者指定搜索引擎只收录指定的内容. 当一个搜索机器人(有的叫搜索 ...