BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 动态树
题目链接: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 弹飞绵羊 动态树的更多相关文章
- 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个装置,每个装置 ...
随机推荐
- CenOS shell脚本
1.先查看脚本解释器 [es@bigdata-senior01 ~]$ echo $SHELL /bin/bash 2.编写最简单的脚本 vi test.sh#第一行的脚本声明(#!)用来告诉系统使用 ...
- [洛谷P5075][JSOI2012]分零食
题目大意:有$m(m\leqslant10^8)$个人站成一排,有$n(n\leqslant10^4)$个糖果,若第$i$个人没有糖果,那么第$i+1$个人也没有糖果.一个人有$x$个糖果会获得快乐值 ...
- 洛谷 P3952 时间复杂度 解题报告
P3952 时间复杂度 题目描述 小明正在学习一种新的编程语言A++,刚学会循环语句的他激动地写了好多程序并 给出了他自己算出的时间复杂度,可他的编程老师实在不想一个一个检查小明的程序, 于是你的机会 ...
- Rabbitmq----基础使用
---------------rabbitmq的基础使用--------------- 查看消息队列信息 在安装好rabbitmq-server之后,启动服务 使用命令rabbitmqctl list ...
- git使用笔记(十一)rebase
By francis_hao Oct 22,2017 git-rebase,改变commit的基础参照 概要 git rebase [-i | --interactive] [options ...
- HDU 2852 主席树
KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 【题解】Matrix BZOJ 4128 矩阵求逆 离散对数 大步小步算法
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4128 大水题一道 使用大步小步算法,把数字的运算换成矩阵的运算就好了 矩阵求逆?这么基础的线 ...
- (转) jsp学习笔记
fromhttp://www.cnblogs.com/tao975/p/4445070.html 什么是JSP JSP的优势 JSP的劣势 JSP与PHP的比较 JSP工作原理 JSP的九大内置对象 ...
- JavaScript之typedof,instanceof,Array.isArray()
typedof value检测对象类型. value instanceof Array检测a对象是否由b对象类型 Array.isArray(value)在两个框架中判断是否为数组类型.
- Mybatis xml 写sql如何判断集合的size
在mybtis的映射文件中判断集合大小 list.size 例子如下: <if test="groupIds != null and groupIds.size>0" ...