bzoj 2002 Bounce 弹飞绵羊
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 弹飞绵羊的更多相关文章
- BZOJ 2002 Bounce 弹飞绵羊 (分块或动态树)
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 13768 Solved: 6989[Subm ...
- BZOJ 2002 Bounce 弹飞绵羊 —— 分块算法
题目链接:https://vjudge.net/problem/HYSBZ-2002 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Li ...
- bzoj 2002 Bounce 弹飞绵羊(分块)
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 11202 Solved: 5698[Subm ...
- BZOJ.2002.Bounce 弹飞绵羊(LCT)
题目链接 从一个点只能往后跳,即后继状态只有一个,那么拿nxt[x]做fa[x]啊!这样就成了一棵树,从每个点开始的答案是它到所在树的根节点的距离. nxt[]的更改即动态修改树边,用LCT即可. 这 ...
- [BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree)
[BZOJ 2002] [HNOI2010]弹飞绵羊(Link Cut Tree) 题面 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一 ...
- 【bzoj 2002】弹飞绵羊
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
- 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 弹飞绵羊 LCT
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOn ...
随机推荐
- 【转】jQuery的attr与prop
原文:<jQuery的attr与prop> jQuery1.6中新添加了一个prop方法,看起来和用起来都和attr方法一样,这两个方法有什么区别呢?这要从HTMl 的attribute与 ...
- 用Chrome查看微信访问需要OAuth2.0网页授权的页面
在PC浏览器打开某网站页面提示页面错误,是因为进行了OAuth2.0网页授权 有以下限制, 1.必须在微信打开 2.微信页面授权 其中第一步比较容易实现,修改下ua(user-agent),让其携带“ ...
- pg按日,周,月进行数据统计
pg数据库按周,月统计数据 SELECT date_trunc('WEEK', insert_time) as insertDate, SUM(data_increment) as dataTotal ...
- 上下行分流下行负载方式和能ping通但不能打开
1 下行线路负载方式选择 目的端口+协议 否则有可能出现微信443端口图片打不开的情况. 2.彭ping通但是打不开的情况下将上行线路mtu值改小 由1500改为1450
- python:input()和raw_input()
1.input() 接受各种合法类型的输入,比如输入字符串,则需要使用双引号,否则报错. input()会自动判断类型,比如输入的是 1.1,则返回的类型是float. 示例: 2.raw_input ...
- 使用Eclipse EE(汉化版) 创建一个JavaWeb工程
第一步:打开eclipse ee,单击“文件”-->单击“新建”-->单击“动态Web项目”. 若没找到“动态Web项目”,单击“其他” -->在弹出的窗口中打开“Web”下拉菜单 ...
- poj2771
题解: 二分图最大独立及 每两个不能选的渐变 输出n+m-最大匹配 代码: #include<cstdio> #include<cmath> #include<algor ...
- LeetCode OJ:Search a 2D Matrix II(搜寻二维矩阵)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- New Concept English three(19)
27w/m 76words Kidnappers are rarely interested in Animals, but they recently took considerable inter ...
- jquery表单验证插件 jquery.form.js-转
来自:http://www.cnblogs.com/luluping/archive/2009/04/15/1436177.html Form插件,支持Ajax,支持Ajax文件上传,功能强大,基本满 ...