bzoj 2002 Bounce 弹飞绵羊

  • 设一个虚拟节点表示被弹飞,则每个点的后继点是唯一确定的,每个点向它的后继点连边,就形成了一颗树.
  • 询问就是问某个节点到虚拟节点的路径长度,修改就删除原来向后继点的边,向新后继点连边.
  • 用一颗 \(LCT\) 来维护即可.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
inline int read()
{
int x=0;
bool pos=1;
char ch=getchar();
for(;!isdigit(ch);ch=getchar())
if(ch=='-')
pos=0;
for(;isdigit(ch);ch=getchar())
x=x*10+ch-'0';
return pos?x:-x;
}
int n,m,Rt;
const int MAXN=2e5+10;
namespace LCT{
int stk[MAXN],tp;
struct node{
int ch[2];
int fa;
int rev;
int siz;
node()
{
rev=0;
siz=1;
ch[0]=ch[1]=fa=0;
}
}tree[MAXN];
#define root tree[x]
#define lson tree[root.ch[0]]
#define rson tree[root.ch[1]]
void pushup(int x)
{
if(!x)
return;
root.siz=lson.siz+1+rson.siz;
}
void inverse(int x)
{
swap(root.ch[0],root.ch[1]);
root.rev^=1;
}
void pushdown(int x)
{
if(root.rev)
{
if(root.ch[0])
inverse(root.ch[0]);
if(root.ch[1])
inverse(root.ch[1]);
root.rev=0;
}
}
bool isroot(int x)
{
return tree[root.fa].ch[0]!=x && tree[root.fa].ch[1]!=x;
}
void rotate(int x)
{
int y=tree[x].fa,z=tree[y].fa;
int k=tree[y].ch[1]==x;
if(!isroot(y))
tree[z].ch[tree[z].ch[1]==y]=x;
tree[x].fa=z;
tree[y].ch[k]=tree[x].ch[k^1];
tree[tree[x].ch[k^1]].fa=y;
tree[x].ch[k^1]=y;
tree[y].fa=x;
pushup(y);
pushup(x);
pushup(z);
}
void splay(int x)
{
tp=0;
stk[++tp]=x;
for(int pos=x;!isroot(pos);pos=tree[pos].fa)
stk[++tp]=tree[pos].fa;
while(tp)
pushdown(stk[tp--]);
while(!isroot(x))
{
int y=tree[x].fa,z=tree[y].fa;
if(!isroot(y))
(tree[y].ch[0]==x)^(tree[z].ch[0]==y)?rotate(x):rotate(y);
rotate(x);
}
pushup(x);
}
void Access(int x)
{
for(int y=0;x;y=x,x=tree[x].fa)
{
splay(x);
tree[x].ch[1]=y;
pushup(x);
}
}
void makeroot(int x)
{
Access(x);
splay(x);
inverse(x);
}
int findroot(int x)
{
Access(x);
splay(x);
while(tree[x].ch[0])
x=tree[x].ch[0];
return x;
}
void split(int x,int y)
{
makeroot(x);
Access(y);
splay(y);
}
void Link(int x,int y)
{
makeroot(x);
tree[x].fa=y;
}
void Cut(int x,int y)
{
split(x,y);
tree[y].ch[0]=0;
tree[x].fa=0;
pushup(y);
}
};
using namespace LCT;
int K[MAXN];
int main()
{
tree[0].siz=0;
n=read();
Rt=n+1;
for(int i=1;i<=n;++i)
{
int k=read();
K[i]=k;
if(k+i<=n)
tree[i].fa=k+i;
else
tree[i].fa=Rt;
}
m=read();
while(m--)
{
int op=read();
if(op==1)
{
int x=read();
++x;
split(x,Rt);
printf("%d\n",tree[tree[Rt].ch[0]].siz);
}
else
{
int x=read();
++x;
int y=x+K[x]>n?Rt:x+K[x];
Cut(x,y);
K[x]=read();
y=x+K[x]>n?Rt:x+K[x];
Link(x,y);
}
}
return 0;
}

bzoj 2002 Bounce 弹飞绵羊的更多相关文章

  1. BZOJ 2002 Bounce 弹飞绵羊 (分块或动态树)

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 13768  Solved: 6989[Subm ...

  2. BZOJ 2002 Bounce 弹飞绵羊 —— 分块算法

    题目链接:https://vjudge.net/problem/HYSBZ-2002 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Li ...

  3. bzoj 2002 Bounce 弹飞绵羊(分块)

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 11202  Solved: 5698[Subm ...

  4. BZOJ.2002.Bounce 弹飞绵羊(LCT)

    题目链接 从一个点只能往后跳,即后继状态只有一个,那么拿nxt[x]做fa[x]啊!这样就成了一棵树,从每个点开始的答案是它到所在树的根节点的距离. nxt[]的更改即动态修改树边,用LCT即可. 这 ...

  5. [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)

    [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...

  6. 【bzoj 2002】弹飞绵羊

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

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

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

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

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

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

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

随机推荐

  1. CentOS7配置自定义JDK

    由于选择的是centos7-dvd镜像自带了open jdk,需要卸载.首先查询自带的jdk: rpm -qa | grep java 会出现7个带有java名称的东西,删除其中的带有openjdk字 ...

  2. 《Django By Example》

    <Django By Example>第六章 中文 翻译 (个人学习,渣翻) 书籍出处:https://www.packtpub.com/web-development/django-ex ...

  3. Bigdecimal: Non-terminating decimal expansion; no exact representable decimal result.

    做除法没有指定保留小数点后几位,就会抛出此异常. 因为会除不尽 Non-terminating decimal expansion; no exact representable decimal re ...

  4. pg按日,周,月进行数据统计

    pg数据库按周,月统计数据 SELECT date_trunc('WEEK', insert_time) as insertDate, SUM(data_increment) as dataTotal ...

  5. Mac 终端命令行报错 -bash: vi: command not found

    我遇到的问题与这个类似,但是我的问题也是用该博文作者方法进行中断才解决的,在此表示感谢. 前段时间在 Mac 下使用终端遇到了这个问题: appledeMacBook-Air:~ air$ vi .b ...

  6. Linux shell常用命令

    1. sz 和 rz  sz命令发送文件到本地: # sz filename rz命令本地上传文件到服务器: # rz 执行该命令后,在弹出框中选择要上传的文件即可.

  7. Dubbo本地开发技巧

    背景 作为后端服务负载.前后分离的主要手段,dubbo在业界中使用率还比较高.随着Dubbo系统的增多,本地开发.调试就出现了麻烦之处 直接在开发本地起同样一份服务 由于Dubbo采用负载均衡的策略, ...

  8. JAVA之Map使用

    参考: http://blog.csdn.net/laixiaobailing/article/details/41644327

  9. Zeratul的完美区间(线段树||RMQ模板题)

    原题大意:原题链接 给定元素无重复数组,查询给定区间内元素是否连续 解体思路:由于无重复元素,所以如果区间内元素连续,则该区间内的最大值和最小值之差应该等于区间长度(r-l) 解法一:线段树(模板题) ...

  10. MySQLdb和PIL安装

    最近将个人博客部署到树莓派上海真是颇费周折,尤其是在MySQLdb和PIL的安装上 MySQLdb 先说Windows吧(比较简单) 直接pip安装即可或者下载编译版更方便:http://source ...