「51nod 1648」洞

小 P 非常喜欢玩。他最喜欢玩的一个游戏就是《洞》,这个游戏遵循以下规则:

NN 个洞呈直线分布,并且从左到右依次编号为 11NN 。每个洞都有它自己的能量值(编号为 ii 的洞有能量值 aia_i )。如果你把一个球扔到洞 ii ,它会迅速调到洞 i+aii+a_i ,以此类推。如果没有编号为 i+aii+a_i 的洞,这个球将会跳到这洞外,结束循环。玩家将会执行 MM 次以下两种操作之一:

  • 设置洞 aa 的能量值为 bb
  • 将球扔到洞 aa ,计算球跳到洞外之前跳跃的次数,以及球刚好跳到洞外之前最后经过的洞的编号。

小 P 不擅长数学,所以将由你实现这些计算。

Constraints

1n,m1000001\leq n,m \leq 100000

Solution

裸的分块。

需要注意的一点是,要记录下跳出去之前的最后一个洞。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define LL long long
using namespace std;
const int N=1e5+5;
int n,m,block,l,r,op,x,ans;
int a[N],b[N],to[N],cnt[N];
int read()
{
int x=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
void modify(int i)
{
l=(i-1)*block+1;
r=min(n,i*block);
for(int j=r;j>=l;j--)
{
if(a[j]>r)
{
if(a[j]>n)to[j]=j,cnt[j]=0;
else to[j]=a[j],cnt[j]=1;
}
else to[j]=to[a[j]],cnt[j]=cnt[a[j]]+1;
}
}
int main()
{
n=read();m=read();block=sqrt(n);
for(int i=1;i<=n;i++)
a[i]=i+read(),b[i]=(i-1)/block+1;
for(int i=1;i<=(n-1)/block+1;i++)modify(i);
while(m--)
{
op=read();x=read();
if(!op)a[x]=x+read(),modify(b[x]);
else
{
ans=0;
while(x!=to[x])ans+=cnt[x],x=to[x];
printf("%d %d\n",x,ans+1);
}
}
return 0;
}