首页 技术 正文
技术 2022年11月7日
0 收藏 912 点赞 542 浏览 1433 个字

【题目链接】

http://www.lydsy.com/JudgeOnline/problem.php?id=2002

【题意】

给定n个数的序列,i可以跳到i+k[i],需要能够修改k并可以查询跳出n需要的步数。

【思路】

把i->i+k看作一条边,则问题抽象为一个森林,越靠后的点离原树的根越近。

考虑LCT维护大小siz。

修改k的操作可以看作是断边与连边的操作。注意如果直接跳出n应该与null相连以切断原来的边。

  一次查询可以先Access使u到根的路径独立,然后splay将u调整至根查询siz。

【代码】

 #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; const int N = 4e5+; namespace LCT { struct Node {
Node *ch[],*fa;
int siz;
Node();
void maintain() {
siz=ch[]->siz+ch[]->siz+;
}
} *null=new Node,T[N];
Node::Node() { fa=ch[]=ch[]=null; siz=; }
void rot(Node* o,int d) {
Node *p=o->fa;
p->ch[d]=o->ch[d^];
o->ch[d^]->fa=p;
o->ch[d^]=p;
o->fa=p->fa;
if(p->fa->ch[]==p)
p->fa->ch[]=o;
else if(p->fa->ch[]==p)
p->fa->ch[]=o;
p->fa=o;
p->maintain();
}
void splay(Node* o) {
Node *nf,*nff;
while(o->fa->ch[]==o||o->fa->ch[]==o) {
nf=o->fa,nff=nf->fa;
if(o==nf->ch[]) {
if(nf==nff->ch[]) rot(nf,);
rot(o,);
} else {
if(nf==nff->ch[]) rot(nf,);
rot(o,);
}
}
o->maintain();
}
void Access(Node* o) {
Node *son=null;
while(o!=null) {
splay(o);
o->ch[]=son;
o->maintain();
son=o; o=o->fa;
}
}
void Link(Node* u,Node* v) {
Access(u); splay(u);
u->ch[]->fa=null;
u->ch[]=null;
u->fa=v;
u->maintain();
} }
using namespace LCT; int n,m; int main()
{
null->fa=null->ch[]=null->ch[]=null;
null->siz=;
scanf("%d",&n);
int op,x,y;
for(int i=;i<=n;i++) {
scanf("%d",&x);
if(i+x<=n) T[i].fa=&T[i+x];
}
scanf("%d",&m);
for(int i=;i<=m;i++) {
scanf("%d%d",&op,&x);
x++;
if(op==) {
Access(&T[x]); splay(&T[x]);
printf("%d\n",T[x].siz);
} else {
scanf("%d",&y);
if(x+y<=n) Link(&T[x],&T[x+y]);
else Link(&T[x],null);
}
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,942
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,468
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,281
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,095
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,728
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,765