BZOJ4764弹飞大爷——LCT
题目描述
输入
输出
样例输入
1 1 1
1 1
1 2
1 3
2 1 2
1 1
1 2
1 3
2 3 -1
1 1
1 2
1 3
2 2 233
1 1
1 2
1 3
2 2 -233
1 1
1 2
1 3
样例输出
2
1
2
2
1
-1
-1
-1
3
1
2
3
1
2
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<bitset>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pr pair<int,int>
#define ll long long
using namespace std;
int n,m;
int opt;
int x,y;
int v[200010];
int s[200010][2];
int st[200010];
int f[200010];
int size[200010];
int a[200010];
int is_root(int rt)
{
return rt!=s[f[rt]][0]&&rt!=s[f[rt]][1];
}
int get(int rt)
{
return rt==s[f[rt]][1];
}
void pushup(int rt)
{
size[rt]=size[s[rt][0]]+size[s[rt][1]]+1;
}
void rotate(int rt)
{
int fa=f[rt];
int anc=f[fa];
int k=get(rt);
if(!is_root(fa))
{
s[anc][get(fa)]=rt;
}
s[fa][k]=s[rt][k^1];
f[s[fa][k]]=fa;
s[rt][k^1]=fa;
f[fa]=rt;
f[rt]=anc;
pushup(fa);
pushup(rt);
}
void splay(int rt)
{
for(int fa;!is_root(rt);rotate(rt))
{
if(!is_root(fa=f[rt]))
{
rotate(get(rt)==get(fa)?fa:rt);
}
}
}
void access(int rt)
{
for(int x=0;rt;x=rt,rt=f[rt])
{
splay(rt);
s[rt][1]=x;
pushup(rt);
}
}
int find(int rt)
{
access(rt);
splay(rt);
while(s[rt][0])
{
rt=s[rt][0];
}
splay(rt);
return rt;
}
void link(int x,int y)
{
if(find(y)==x)
{
v[x]=y;
}
else
{
access(x);
splay(x);
f[x]=y;
}
}
void cut(int x,int y)
{
if(v[x]==y)
{
v[x]=0;
}
else
{
int root=find(x);
access(x);
splay(x);
f[s[x][0]]=0;
s[x][0]=0;
pushup(x);
if(v[root]&&find(v[root])!=root)
{
link(root,v[root]);
v[root]=0;
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
size[i]=1;
if(i+a[i]<=n&&i+a[i]>=1)
{
link(i,i+a[i]);
}
}
while(m--)
{
scanf("%d",&opt);
if(opt==1)
{
scanf("%d",&x);
int root=find(x);
if(v[root])
{
printf("-1\n");
}
else
{
access(x);
splay(x);
printf("%d\n",size[x]);
}
}
else
{
scanf("%d%d",&x,&y);
if(x+a[x]<=n&&x+a[x]>=1)
{
cut(x,x+a[x]);
}
a[x]=y;
if(x+a[x]<=n&&x+a[x]>=1)
{
link(x,x+a[x]);
}
}
}
}
BZOJ4764弹飞大爷——LCT的更多相关文章
- bzoj4764 弹飞大爷 LCT
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4764 题解 如果 \(a_i > 0\) 的话,那么就是 bzoj2002 的原题.直接 ...
- 【BZOJ4764】弹飞大爷 LCT
[BZOJ4764]弹飞大爷 Description 自从WC退役以来,大爷是越来越懒惰了.为了帮助他活动筋骨,也是受到了弹飞绵羊一题的启发,机房的小伙伴们决定齐心合力构造一个下面这样的序列.这个序列 ...
- 【LCT维护基环内向树森林】BZOJ4764 弹飞大爷
4764: 弹飞大爷 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 101 Solved: 52[Submit][Status][Discuss] ...
- 【BZOJ】4764: 弹飞大爷 LCT
[题意]给定n个数字ai,表示大爷落到i处会被弹飞到i+ai处,弹飞到>n或<1处则落地.m次操作,修改一个ai,或询问大爷落到x处经过几次落地(或-1).n,m<=10^5,|ai ...
- bzoj4764: 弹飞大爷
Description 自从WC退役以来,大爷是越来越懒惰了.为了帮助他活动筋骨,也是受到了弹飞绵羊一题的启发,机房的小伙伴们 决定齐心合力构造一个下面这样的序列.这个序列共有N项,每项都代表了一个小 ...
- BZOJ 4764: 弹飞大爷 LCT
思路并不难,主要是细节需要注意一下. 在 lct 中,删边要写成:f[lson]=0,lson=0 (因为删 x->y 时 y 不一定是左儿子,y 只是 x 的前驱) 然后 f[lson]=ls ...
- bzoj4764: 弹飞大爷 link-cut-tree
题目传送门 这道题啊 调了一个晚上 因为写的是一个有根树和n个基环的写法 所以写得很奇怪..... 最后发现单独处理树的时候不能随意改变S(就是原来的根)不然size会出错.... #include& ...
- BZOJ 4764: 弹飞大爷
4764: 弹飞大爷 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 4 Solved: 4[Submit][Status][Discuss] Des ...
- [BZOJ2002] [Hnoi2010] Bounce 弹飞绵羊 (LCT)
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
随机推荐
- C++ 文件和流
到目前为止,我们已经使用了 iostream 标准库,它提供了 cin 和 cout 方法分别用于从标准输入读取流和向标准输出写入流. 本教程介绍如何从文件读取流和向文件写入流.这就需要用到 C++ ...
- Spring @Scheduled定时任务的fixedRate,fixedDelay,cron执行差异
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import org.sp ...
- 【博客大赛】使用LM2677制作的3V至24V数控可调恒压源
[博客大赛]使用LM2677制作的3V至24V数控可调恒压源 http://bbs.ednchina.com/BLOG_ARTICLE_3013105.HTM LM2677,是TI公司生产的高效率 ...
- 如何下载google play商店里面的app?
如何不FQ的下载这国际版的app呢? 方法如下: https://androidappsapk.co/category/apps/ 你可以直接登入这个网站,下载你所需要的国际版的软件. 就像是踏入一个 ...
- 3.4《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——grepping(检索目标行命令)
grep是检查文件内容最强大的工具之一,这也许不能代表什么,但这不是重点.的确,grep常用作动词,比如'你完全应该检索(grep)那个文件'. grep最常用于在文件中搜索子字符串.例如,我们在第三 ...
- ThinkPad T43续命记
// Description: 原作于2016年8月25日. Mr. Robot 最近有部叫<黑客军团>(Mr. Robot)的戏比较火.目前第二季已经出到一大半了,深得技术宅和技术宅仰慕 ...
- npm install xxx --save-dev 与npm install xxx --save 的区别
正常情况下: 当你为你的模块安装一个依赖模块时 1.你得先安装他们(在模块根目录下npm install module-name) 2.连同版本号手动将他们添加到模块配置文件package.json中 ...
- 重启 IIS7 应用或者应用程序池的批处理bat
重启应用 本地: ctrl+r->iisreset -stop ctrl+r->iisreset -start ctrl+r->iisreset 远程(假如远程机器地址为10.5.6 ...
- 绍一集训Round#2
Preface 感觉这次的题目是真的太水了,可能是为了让我们涨一波信心的吧. 不过最后一题没有想到那种玄学做法还是太菜了,还是要一波姿势的啊. 交换 一道入门难度题,根据排序不等式(又或是简单推导可以 ...
- Codeforces 999D Equalize the Remainders (set使用)
题目连接:Equalize the Remainders 题意:n个数字,对m取余有m种情况,使得每种情况的个数都为n/m个(保证n%m=0),最少需要操作多少次? 每次操作可以把某个数字+1.输出最 ...