BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊(动态树)
【题目链接】 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 弹飞绵羊(动态树)的更多相关文章
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 动态树
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题意:加边,删边,查询到根的距离. #include <bits/stdc++ ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 (动态树LCT)
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2843 Solved: 1519[Submi ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 LCT
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 9071 Solved: 4652[Submi ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 分块
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...
- bzoj 2002: [Hnoi2010]Bounce 弹飞绵羊 動態樹
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 4055 Solved: 2172[Submi ...
- bzoj 2002 : [Hnoi2010]Bounce 弹飞绵羊 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 题面: 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 【分块】
任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 ...
- BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊:分块
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题意: 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆 ...
- 【刷题】BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
随机推荐
- mysql性能监控工具Innotop
mysql监控管理工具--innotop 1.innotop安装参考官网:http://innotop.googlecode.com/svn/html/installing.html # wget h ...
- Android R.layout. 找不到已存在的布局文件
今天写新页面的时候,突然发现R.layout. 无法找到我已经写好的页面,于是顿时就不淡定了. 把R文件翻了一遍 发现也没有.... 然后我就看到了这个. android.R 原来是我错把Andr ...
- jQuery简单前端表单验证
<!DOCTYPE html> <html> <head> <title>表单验证</title> <script src=" ...
- CentOS rc.local 不执行的问题
centos7 开机/etc/rc.local 不执行的问题 时间:2015-02-09 00:54来源:blog.51cto.com 作者:“闲潭小筑” 博客, 举报 点击:次 最近发现centos ...
- PHPExcel 多工作表 导出
//浏览器输出excel header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ...
- #include <iostream>
1 static_assert 2 std::nothrow 3 std::ref() 4 std::string 1 static_assert 执行编译时断言检查 语法 static_assert ...
- 接着上一个版本在上一个分离access-token和ticket的版本
上代码: 本次修改将获取token和ticket分离出来,分别封装在函数中: 每个函数最后一个参数是一个回调参数: 回调函数的参数,是这一步中需要处理的结果; 结果怎么处理,根据传递进去的函数: va ...
- jQuery $.each用法比较详细了
以下内容非原创,来自百度文库http://wenku.baidu.com/view/4796b6145f0e7cd18425368e.html 通过它,你可以遍历对象.数组的属性值并进行处理. 使用说 ...
- iOS获取手机当前的网络状态
获取iOS网络状态,目前有两个办法. 1.通过监听手机状态栏的信息. 2.通过使用官方提供的类Reachability. 一.通过手机监听手机状态栏的信息 好处: 1.可以通过苹果的审核上架AppSt ...
- Ubuntu小私房(3)--Uubutnu启动美化大变身
Grub是什么? GNU GRUB 和GRUB是GRand Unified Bootloader的缩写,它是一个多重操作系统启动管理器.用来引导不同系统,如windows,linux.GRUB是多启动 ...