time limit per test : 2 seconds
memory limit per test : 256 megabytes
is an array of n positive integers, all of which are not greater than .
You have to process q queries to this array. Each query is represented by two numbers p and k. Several operations are performed in each query; each operation changes to . There operations are applied until becomes greater than . The answer to the query is the number of performed operations.
Input
The first line contains one integer
.
The second line contains
integers — elements of
for each
from
to
.
The third line containts one integer
.
Then
lines follow. Each line contains the values of
and
for corresponding query
.
Output
Print integers, ith integer must be equal to the answer to ith query.
Example
Input
3
1 1 1
3
1 1
2 1
3 1
Output
2
1
1
Note
Consider first example:
In first query after first operation p = 3, after second operation p = 5.
In next two queries p is greater than n after the first operation.
题意:
给一个数组a(a[i]<=n),长度为n(<=10w),q(<=10w)个询问,每次询问两个数字p,k,问最多能做几次操作
操作:p=p+a[p]+k(当p>n之后就不能做了)
题解:
对于
的情况,我们发现操作个数不会超过
,那就暴力即可
对于
的情况,我们用
的时间预处理出所有结果即可
#include<bits/stdc++.h>
#define LiangJiaJun main
using namespace std;
int f[404][100004];
int n,q,a[100004];
void ex(int k,int p){
if(p>n)return ;
if(f[k][p])return ;
ex(k,p+k+a[p]);
if(p+k+a[p]<=n)f[k][p]=f[k][p+k+a[p]]+1;
else f[k][p]=1;
}
int bruteforce(int k,int p){
int ans=0;
while(p<=n){
++ans;
p=p+k+a[p];
}
return ans;
}
int LiangJiaJun(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
memset(f,0,sizeof(f));
for(int i=1;i<=sqrt(n);i++){
for(int j=1;j<=n;j++){
if(!f[i][j])ex(i,j);
}
}
scanf("%d",&q);
while(q--){
int p,k;
scanf("%d%d",&p,&k);
if(k<=sqrt(n)){
printf("%d\n",f[k][p]);
}
else{
printf("%d\n",bruteforce(k,p));
}
}
return 0;
}