分块求调,只拿了30qwq
查看原帖
分块求调,只拿了30qwq
788202
water_monster楼主2023/8/26 09:56
#include<bits/stdc++.h>
using namespace std;
const int M=2e5+10;
int st[M],ed[M],pos[M],a[M],step[M],ne[M];
int n,m,opt,t,x,y,cnt=0;
void reset(int x){
	step[x]=0;
	int k=x,p=pos[x];
	while(k<=ed[p]){
		//cout<<"before:k="<<k<<",a[k]="<<a[k]<<endl;
		k+=a[k];
		step[x]++;
		//cout<<"after:k="<<k<<",a[k]="<<a[k]<<endl;
	}
	if(k>(n-1)) ne[x]=-1;
	else ne[x]=k;
	//cout<<step[x]<<" "<<ne[x]<<endl;
}
void build(){
	int block=sqrt(n);
	int t=n/block;
	if(n%block) t++;
	for(int i=1;i<=t;i++){
		st[i]=(i-1)*block;
		ed[i]=i*block-1;
	}
	ed[t]=n-1;
	for(int i=0;i<n;i++){
		pos[i]=i/block+1;
		reset(i);
	}
}
void change(int x,int w){
	a[x]=w;
	reset(x);
}
int query(int x){
	int ans=0,k=x;
	while(~k){
		ans+=step[k];
		k=ne[k];
	}
	return ans;
}
int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	build();
	cin>>m;
	for(int i=1;i<=m;i++){
		cin>>opt>>x;
		if(opt==1){
			cout<<query(x)<<endl;
		}else{
			cin>>y;
			change(x,y);
		}
	}
	return 0;
}
2023/8/26 09:56
加载中...