求助!玄关
查看原帖
求助!玄关
758200
_LDX_WWS_楼主2024/8/3 19:56

60pts,分块喜提TLE

#include<bits/stdc++.h>
#define gc getchar
using namespace std;
const int N=3*1e5+10;
template<class T>void in(T &x) {
    x = 0; bool f = 0; char c = gc();
    while (c < '0' || c > '9') {if (c == '-') f = 1; c = gc();}
    while ('0' <= c && c <= '9') {x = (x << 3) + (x << 1) + (c ^ 48); c = gc();}
    if (f) x = -x;
}
int n,T,k[N],bg[N],ed[N],to[N],step[N],pos[N],cnt;
int main(){
	in(n);
	for(int i=1;i<=n;i++) in(k[i]);
	for(int i=1;i<=sqrt(n);i++) bg[++cnt]=((i-1)*sqrt(n))+1,ed[cnt]=i*sqrt(n);
	if(sqrt(n)*sqrt(n)!=n) bg[++cnt]=bg[cnt-1]+1,ed[cnt]=n;
	for(int i=1;i<=cnt;i++){
		for(int j=ed[i];j>=bg[i];j--){
			pos[j]=i;
			if(k[j]+j>ed[i]){
				to[j]=k[j]+j;
				step[j]=1;
			}
			else{
				to[j]=to[j+k[j]];
				step[j]=step[j+k[j]]+1;
			}
//			cout<<j<<" "<<to[j]<<" "<<step[j]<<endl;
		}
	}
	in(T);
	while(T--){
		int op,x,y;
		in(op);in(x);
		x++;
		if(op==1){
			int ans=0;
			for(int i=x;i<=n;i=to[i]) ans+=step[i];
			printf("%d\n",ans);
		}
		else{
			in(y);
			int i=pos[x];k[x]=y;
			for(int j=ed[i];j>=bg[i];j--){
				if(k[j]+j>ed[i]){
					to[j]=k[j]+j;
					step[j]=1;
				}
				else{
					to[j]=to[j+k[j]];
					step[j]=step[j+k[j]]+1;
				}
			}
		}
	}
	return 0;
}
2024/8/3 19:56
加载中...