LCT不会写,只能写写简单一点的分块了。
每sqrt(n)分一块,然后对每一块内的点的信息维护的是跳出这块的下一个位置,和次数。每次操作复杂度为sqrt(n),不是很快。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define clr(x,y) memset(x,y,sizeof x)
#define PI acos(-1.0)
#define ITER set<int>::iterator
const int Mod = 1e9 + 7;
const int maxn = 2e5 + 100;
int a[maxn];
int nexts[maxn],times[maxn];
int main()
{
int n,m;
while( ~ scanf("%d",&n))
{
for(int i = 0; i < n; i ++)scanf("%d",&a[i]);
int block = static_cast<int>(sqrt(n) + 1e-7);
for(int i = n - 1; ~ i; i --)
{
int temp = i + a[i];
if(temp >= n)
{
nexts[i] = -1;times[i] = 1;
}
else if(temp >= (i / block + 1) * block)
{
nexts[i] = temp;times[i] = 1;
}
else nexts[i] = nexts[temp],times[i] = times[temp] + 1;
}
scanf("%d",&m);
while(m --)
{
int x,y,z;scanf("%d%d",&x,&y);
if(x == 1)
{
int ans = 0;
for(int i = y; ~ i; i = nexts[i])
{
ans += times[i];
}
printf("%d\n",ans);
}
else if(x == 2)
{
scanf("%d",&z);a[y] = z;
for(int i = y; i >= y/block * block; i --)
{
int temp = i + a[i];
if(temp >= n)
{
nexts[i] = -1;times[i] = 1;
}
else if(temp >= (i / block + 1) * block)
{
nexts[i] = temp;times[i] = 1;
}
else nexts[i] = nexts[temp],times[i] = times[temp] + 1;
}
}
}
}
return 0;
}