分块。
记录每个点到下一块区间需要走的步数,和到达下个区间的哪个点。
时间复杂度:
O(qn−√)
#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
const int maxn = 200010;
int n, m, Size;
int k[maxn], bl[maxn], sp[maxn], pt[maxn];
int main(){
scanf("%d", &n), Size = sqrt(n);
for(int i = 1; i <= n; i ++) scanf("%d", &k[i]);
for(int i = 1; i <= n; i ++) bl[i] = (i-1)/Size + 1;
for(int i = n; i >= 1; i --){
int nxt = i + k[i];
if(nxt > n) sp[i] = 1, pt[i] = 0;
else if(bl[nxt] > bl[i]) sp[i] = 1, pt[i] = nxt;
else sp[i] = sp[nxt] + 1, pt[i] = pt[nxt];
}
scanf("%d", &m);
for(int i = 1; i <= m; i ++){
int a, b, c;
scanf("%d%d", &a, &b);
b ++;
if(a == 1){
int res = 0;
for(int j = b; j; j = pt[j]) res += sp[j];
printf("%d\n", res);
}else{
scanf("%d", &c);
k[b] = c;
for(int j = b; bl[j] == bl[b]; j --){
int nxt = j + k[j];
if(nxt > n) sp[j] = 1, pt[j] = 0;
else if(bl[nxt] > bl[j]) sp[j] = 1, pt[j] = nxt;
else sp[j] = sp[nxt] + 1, pt[j] = pt[nxt];
}
}
}
return 0;
}