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

【题目大意】

  给出一片森林,操作允许更改一个节点的父亲,查询一个节点的深度。

【题解】

  更改父亲操作直接cutf然后修改一下即可,查询深度则直接提取链然后splay一下

【代码】

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=200010;
int f[N],son[N][2],val[N],sum[N],tmp[N];bool rev[N];
bool isroot(int x){return !f[x]||son[f[x]][0]!=x&&son[f[x]][1]!=x;}
void rev1(int x){if(!x)return;swap(son[x][0],son[x][1]);rev[x]^=1;}
void pb(int x){if(rev[x])rev1(son[x][0]),rev1(son[x][1]),rev[x]=0;}
void up(int x){
sum[x]=val[x];
if(son[x][0])sum[x]+=sum[son[x][0]];
if(son[x][1])sum[x]+=sum[son[x][1]];
}
void rotate(int x){
int y=f[x],w=son[y][1]==x;
son[y][w]=son[x][w^1];
if(son[x][w^1])f[son[x][w^1]]=y;
if(f[y]){
int z=f[y];
if(son[z][0]==y)son[z][0]=x;else if(son[z][1]==y)son[z][1]=x;
}f[x]=f[y];f[y]=x;son[x][w^1]=y;up(y);
}
void splay(int x){
int s=1,i=x,y;tmp[1]=i;
while(!isroot(i))tmp[++s]=i=f[i];
while(s)pb(tmp[s--]);
while(!isroot(x)){
y=f[x];
if(!isroot(y)){if((son[f[y]][0]==y)^(son[y][0]==x))rotate(x);else rotate(y);}
rotate(x);
}up(x);
}
void access(int x){for(int y=0;x;y=x,x=f[x])splay(x),son[x][1]=y,up(x);}
void cutf(int x){access(x);splay(x);f[son[x][0]]=0;son[x][0]=0;up(x);}
int query(int x){access(x);splay(x);return sum[x];}
int n,x,y,q,op,K[N];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&K[i]);
if(i+K[i]>n)f[i]=0;
else f[i]=i+K[i];
val[i]=sum[i]=1;
}scanf("%d",&q);
while(q--){
scanf("%d%d",&op,&x);x++;
if(op==1)printf("%d\n",query(x));
else{
scanf("%d",&y);
if(x+y>n)y=0; else y=x+y;
K[x]=y; cutf(x); f[x]=y;
}
}return 0;
}

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

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

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题意:加边,删边,查询到根的距离. #include <bits/stdc++ ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. loading 动画效果(收藏起来以后留着慢慢用)

    动画效果一: html代码: <div class="spinner">   <div class="rect1"></div&g ...

  2. 编写一个程序实现strcpy函数的功能

    #include <stdio.h> #include <string.h> #define N 5 char *mycpy(char *s1, char *s2) { //数 ...

  3. Android消息机制不完全解析(下)

    接着上一篇文章Android消息机制不完全解析(上),接着看C++部分的实现. 首先,看看在/frameworks/base/core/jni/android_os_MessageQueue.cpp文 ...

  4. 07.31 zepto

    tag事件 viewport标签 flexbox弹性布局 响应式布局 rem 交互优化 touchstart touchend 高清图片 1px  单文本溢出  多文本溢出

  5. poj2486 Apple Tree (树形dp)

    题意:有一颗苹果树,树上的u节点上有num[u]个苹果,树根为1号节点,囧king从根开始走,没走到一个节点就把接点上的苹果吃光,问囧king在不超过k步的情况下最多吃多少个苹果. 解题思路:处理出两 ...

  6. 关于 FPGA 和 外部芯片接口时序设计

    在看这篇文章之前, 建议先好好读下这篇文章.http://download.csdn.net/detail/angelbosj/8013827. 因为我不太会用 VISio.要是哪位网友能告诉我.怎么 ...

  7. 2.&与&&以及位运算符。

    这是单独的一块,因为一条讲不清楚(虽然内容也不够一篇),而且我之前也没好好弄清楚,所以有必要写出来. 说位运算符也是从&与&&(|与||类似)之间的区别讲起的.事实上,对于两个 ...

  8. pl/sql developer 编码格式设置(转)

    一.pl/sql developer 中文字段显示乱码 原因:因为数据库的编号格式和pl /sql developer的编码格式不统一造成的. 二.查看和修改oracle数据库字符集: select ...

  9. SQL Server 2012学习笔记 2 Server Core中命令行安装SQL

    Setup.exe /qs /ACTION=Install /FEATURES=SQLEngine,Replication /INSTANCENAME=MSSQLSERVER /SQLSVCACCOU ...

  10. js判断年龄是否在0-100之间

    //判断年龄 $('#info_age').change(function(){ var _val = $(this).val(); -]+$/.test( _val ))||_val< || ...