BZOJ 2002 HNOI2010 弹飞羊 块

标题效果,LCT解释版本:见 http://blog.csdn.net/popoqqq/article/details/38849471

如今,用一只手滑动块,并再次改写这个问题0.0 块短啊

将进入春天√n片

对于每一个弹簧的 我们记录一下从这个弹簧出发直到弹到块外为止的弹跳次数及落点

查询沿着落点弹到出去为止 改动从块開始到这个点为止改动一遍

这样改动和查询都是O(√n)的

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define M 200200
using namespace std;
int n,m,ans,block,a[M];
int pos[M],times[M];
int main()
{
	int i,j,p,x,y;
	cin>>n;
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	block=static_cast<int>(sqrt(n)+1e-7);
	for(i=n-1;~i;i--)
	{
		int temp=i+a[i];
		if(temp>=n)
			pos[i]=-1,times[i]=1;
		else if(temp>=(i/block+1)*block)
			pos[i]=temp,times[i]=1;
		else
			pos[i]=pos[temp],times[i]=times[temp]+1;
	}
	cin>>m;
	for(i=1;i<=m;i++)
	{
		scanf("%d",&p);
		if(p==1)
		{
			scanf("%d",&x);
			ans=0;
			for(j=x;~j;j=pos[j])
				ans+=times[j];
			printf("%d
",ans);
		}
		else
		{
			scanf("%d%d",&x,&y);
			a[x]=y;
			for(j=x;j>=x/block*block;j--)
			{
				int temp=j+a[j];
				if(temp>=n)
					pos[j]=-1,times[j]=1;
				else if(temp>=(j/block+1)*block)
					pos[j]=temp,times[j]=1;
				else
					pos[j]=pos[temp],times[j]=times[temp]+1;
			}
		}
	}
}


版权声明:本文博客原创文章,博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/lcchuguo/p/4663937.html