Codeforces - Array Queries

题目链接:Codeforces - Array Queries


如果k比较大,暴力即可。

如果k小,我们又可以预处理。所以直接分块即可。

跑得还挺快。


AC代码:

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=1e5+10;
int n,q,a[N],bl,dp[N][400];
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;
inline int read(){
    int x=0,f=1; char ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=gc();}
    return x*f;
}
signed main(){
	n=read();	bl=sqrt(n)+1;
	for(int i=1;i<=n;i++)	a[i]=read();
	for(int i=n;i>=1;i--){
		for(int j=1;j<=bl;j++){
			if(i+a[i]+j>n)	dp[i][j]=1;
			else	dp[i][j]=dp[i+a[i]+j][j]+1;
		}
	}
	q=read();
	for(int i=1,p,k,cnt;i<=q;i++){
		p=read(),k=read();
		if(k<=bl)	printf("%d\n",dp[p][k]);
		else{
			cnt=0;	while(p<=n)	p+=a[p]+k,cnt++;
			printf("%d\n",cnt);
		}
	}
	return 0;
}
发布了604 篇原创文章 · 获赞 242 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_43826249/article/details/104362523