1. 程式人生 > >bzoj2002 彈飛綿羊 分塊

bzoj2002 彈飛綿羊 分塊

getchar() -- 處理 自己的 方便 open pla none alt

這道題是分塊的初嘗試 講給定的區間n進行分塊處理 這個每次修改的復雜的只有logn 很方便

代碼是學黃學長的 http://hzwer.com/3505.html

當然裏面還是有一定我自己的想法在裏面的 嫌我代碼醜的可以去看黃學長的咯

技術分享
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std;
const int M=200007;
int read(){
    int ans=0,f=1
,c=getchar(); while(c<0||c>9){if(c==-) f=-1; c=getchar();} while(c>=0&&c<=9){ans=ans*10+(c-0); c=getchar();} return ans*f; } int n,m,block,sum; int stp[M],w[M],fa[M],belong[M]; int l[1005],r[1005]; int push_ans(int x){ int ans=0; while(1){ ans+=stp[x];
if(!fa[x]) break; x=fa[x]; } return ans; } int main() { n=read(); block=sqrt(n); for(int i=1;i<=n;i++) w[i]=read(); if(n%block) sum=n/block+1; else sum=n/block; for(int i=1;i<=sum;i++) l[i]=r[i-1]+1,r[i]=i*block; r[sum]=n; for(int i=1;i<=n;i++) belong[i]=(i-1
)/block+1; for(int i=n;i;i--){ if(i+w[i]>n) stp[i]=1; else if(belong[i]==belong[i+w[i]]) stp[i]=stp[i+w[i]]+1,fa[i]=fa[i+w[i]]; else stp[i]=1,fa[i]=i+w[i]; } int k,x,y; m=read(); for(int i=1;i<=m;i++){ k=read(); if(k==1) x=read()+1,printf("%d\n",push_ans(x)); else{ x=read()+1; y=read(); w[x]=y; for(int j=x;j>=l[belong[x]];j--){ if(belong[j]==belong[j+w[j]]) stp[j]=stp[j+w[j]]+1,fa[j]=fa[j+w[j]]; else stp[j]=1,fa[j]=j+w[j]; } } //for(int j=1;j<=n;j++) printf("[%d %d] ",fa[j],stp[j]); printf("\n"); } return 0; }
View Code

bzoj2002 彈飛綿羊 分塊