分块 #2 #7 #10 AC 其余WA 求调
查看原帖
分块 #2 #7 #10 AC 其余WA 求调
823122
guozhe6241楼主2024/7/30 07:50
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
inline int read(){
    int x=0,f=1;char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
    return x*f;
}
inline void write(int x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
const int maxn=2e5+5;
int n,m,len;
int a[maxn],loc[maxn],l[maxn],to[maxn],dp[maxn];
void update(int x,int k){
	dp[x]=k;
	for(int i=l[loc[x]+1]-1;i>=l[loc[x]];i--){
		if(i+a[i]>=l[loc[i]+1]) dp[i]=1,to[i]=i+a[i];
		else dp[i]=dp[i+a[i]]+1,to[i]=to[i+a[i]];
	}
}
int query(int x){
	int ans=0;
	while(x<=n) ans+=dp[x],x=to[x];
	return ans;
}
signed main(){
	n=read();
	len=sqrt(n); 
	for(int i=1;i<=n;i++){
		a[i]=read();
		loc[i]=(i-1)/len+1;
		if(loc[i]!=loc[i-1]) l[loc[i]]=i;
	}
	l[loc[n]+1]=n+1;
	for(int i=n;i>=1;i--){
		if(i+a[i]>=l[loc[i]+1]) dp[i]=1,to[i]=i+a[i];
		else dp[i]=dp[i+a[i]]+1,to[i]=to[i+a[i]];
	}
	m=read();
	for(int i=1;i<=m;i++){
		int op,x,y;
		op=read(),x=read();
		if(op==1) cout<<query(x+1)<<endl;
		else{
			y=read();
			update(x+1,y);
		}
	}
	return 0;
}
2024/7/30 07:50
加载中...