显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int n,q,sq;
int a[N],f[N][400];
//根号分治算法
int main(){
freopen("arrayque.in","r",stdin);
freopen("arrayque.out","w",stdout);
scanf("%d",&n);
sq = sqrt(n);
for(int i = 1;i <= n;i++)scanf("%d",&a[i]);
for(int i = n;i >= 1;i--){
for(int j = 1;j <= sq;j++){
if(i + a[i] + j > n)f[i][j] = 1;
else f[i][j] = f[i+a[i]+j][j] + 1;
}
}//预处理k <= sqrt(n)的部分
scanf("%d",&q);
for(int i = 1;i <= q;i++){
int x,y;
scanf("%d%d",&x,&y);
if(y <= sq)printf("%d\n",f[x][y]);
else{
int ans = 0;
while(x <= n){
x = x + a[x] + y;
ans++;
}//k > sqrt(n) 暴力枚举
printf("%d\n",ans);
}
}
return 0;
}