社区讨论

分块求条

P3203[HNOI2010] 弹飞绵羊参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@mjcnxw28
此快照首次捕获于
2025/12/19 17:24
2 个月前
此快照最后确认于
2025/12/21 10:05
2 个月前
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
int n,m,op,id,x,a[N],to[N],step[N],pos[N];
int B,t,ans;
struct Block{
	int l,r;
}b[500];
void init(){
	B=sqrt(n);
	t=n/B;
	if(n%B)++t;
	for(int i=1;i<=t;++i){
		b[i].l=(i-1)*B+1;
		b[i].r=i*B;
	}
	b[t].r=n;
	for(int i=1;i<=t;++i){//O(n)
		for(int j=b[i].r;j>=b[i].l;--j){
			pos[j]=i;
			if(j+a[j]>b[i].r){
				step[j]=1;
				to[j]=j+a[j];
			}else{
				step[j]=step[j+a[j]]+1;
				to[j]=to[j+a[j]];
			}
		}
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr),cout.tie(nullptr);
	cin>>n;
	for(int i=1;i<=n;++i) cin>>a[i];
	init();
	cin>>m;
	while(m--){
		cin>>op>>id;
		++id;
		if(op==1){
			ans=0;
			int now=id;
			while(now<=n){
				ans+=step[now],now=to[now];
			}
			cout<<ans<<"\n";
		}else{
			cin>>x;
			a[id]=x;
			for(int i=b[pos[id]].l;i<=b[pos[id]].r;++i){
				if(i+a[i]>b[pos[id]].r){
					step[i]=1;
					to[i]=i+a[i];
				}else{
					step[i]=step[i+a[i]]+1;
					to[i]=to[i+a[i]];
				}
			}
		}
	}
	return 0;
}

只A了#6,9,10 QwQ

回复

0 条回复,欢迎继续交流。

正在加载回复...