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个装置,每个装置 ...
随机推荐
- 【题解】Atcoder AGC#16 E-Poor Turkeys
%拜!颜神怒A此题,像我这样的渣渣只能看看题解度日╭(╯^╰)╮在这里把两种做法都记录一下吧~ 题解做法:可以考虑单独的一只鸡 u 能否存活.首先我们将 u 加入到集合S.然后我们按照时间倒序往回推, ...
- BZOJ1293:[SCOI2009]生日礼物——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1293 https://www.luogu.org/problemnew/show/P2564#sub ...
- bzoj4870: [Shoi2017]组合数问题(DP+矩阵乘法优化)
为了1A我居然写了个暴力对拍... 那个式子本质上是求nk个数里选j个数,且j%k==r的方案数. 所以把组合数的递推式写出来f[i][j]=f[i-1][j]+f[i-1][(j-1+k)%k].. ...
- 2017-7-18-每日博客-关于Linux下的history的常用命令.doc
History history命令可以用来显示曾执行过的命令.执行过的命令默认存储在HOME目录中的.bash_history文件中,可以通过查看该文件来获取执行命令的历史记录.需要注意的是.bash ...
- cuda环境下安装opencv出现nvcc warning : The 'compute_11'
警告打印: nvcc warning : The 'compute_11', 'compute_12', 'compute_13', 'sm_11', 'sm_12', and 'sm_13' arc ...
- 在CentOS6.5 下安装并使用Java开发opencv的配置(一)
1) 安装gcc以及cmake等等乱七八糟的软件 yum install gcc yum install python yum install cmake yum groupinstall " ...
- poj2060——Taxi Cab Scheme(最小路径覆盖)
Description Running a taxi station is not all that simple. Apart from the obvious demand for a centr ...
- c++模板类编译错误
最近想写一个c++模板类,实现一个有向图.依据惯例,类在头文件中声明,类的实现写在源文件中.可是编译的时候出现了如下错误: undefined reference to 通过谷歌发现,这是一个很常见的 ...
- ASP.Net初级学习一(基本语句入门)
<body > <form method="post" action="program.ashx"> <input type=&q ...
- HDU1914 稳定婚姻匹配
The Stable Marriage Problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (J ...