刚学OI的萌新写了个假的分块
查看原帖
刚学OI的萌新写了个假的分块
247540
tongyf楼主2020/5/2 21:57

RT,T飞了,20pts,怀疑是时间复杂度假了(比如pre_solve函数)

#include<bits/stdc++.h>
using namespace std;
int n,m,siz,pos[200005],len[200005],num[200005];
int get_pos(int x){
	return x/siz+1;
}
void pre_solve(int x){
	int r=x,ans=0,block=get_pos(x);
	while(get_pos(r)==block){
		ans++;
		r+=num[r];
	}
	pos[x]=r;
	len[x]=ans;
}
void change(int x,int y){
	num[x]=y;
	int r=get_pos(x);
	for(int i=(r-1)*siz;i<=x;i++) pre_solve(i);
}
int query(int x){
	int ans=0;
	while(x<n){
		ans+=len[x];
		x=pos[x];
	}
	return ans;
}
int main(){
	cin>>n;
	siz=sqrt(n);
	for(int i=0;i<n;i++) cin>>num[i];
	for(int i=0;i<n;i++){
		pre_solve(i);
	}
	cin>>m;
	for(int i=1;i<=m;i++){
		int opt,a,b;
		cin>>opt;
		if(opt==1){
			cin>>a;
			cout<<query(a)<<endl;
		}
		else{
			cin>>a>>b;
			change(a,b);
		}
	}
	return 0;
}
2020/5/2 21:57
加载中...