bzoj2002: [Hnoi2010]Bounce 彈飛綿羊 lct
阿新 • • 發佈:2018-05-31
comment ++ 設定 問題 unsigned optimize problem its pre
某天,Lostmonkey發明了一種超級彈力裝置,為了在他的綿羊朋友面前顯擺,他邀請小綿羊一起玩個遊戲。遊戲一開始,Lostmonkey在地上沿著一條直線擺上n個裝置,每個裝置設定初始彈力系數ki,當綿羊達到第i個裝置時,它會往後彈ki步,達到第i+ki個裝置,若不存在第i+ki個裝置,則綿羊被彈飛。綿羊想知道當它從第i個裝置起步時,被彈幾次後會被彈飛。為了使得遊戲更有趣,Lostmonkey可以修改某個彈力裝置的彈力系數,任何時候彈力系數均為正整數。
題解:lct操作,對於可以從i一步到i+a[i]我們連一條邊(如果i+a[i]>n那麽就變成n+1),問題就變成了,i到n+1的最短路,每次更新先把原來的邊cut,然後link上新邊即可,(剛開始數組開小了一直t,還以為lct寫搓了= = )
/************************************************************** Problem: 2002 User: walfy Language: C++ Result: Accepted Time:1740 ms Memory:6772 kb ****************************************************************/ //#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack-protector") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #define fi first #define se second #define mp make_pair #define pb push_back #define pi acos(-1.0) #define ll long long #define vi vector<int> #define mod 1000000007 #define ld long double #define C 0.5772156649 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #define pil pair<int,ll> #define pli pair<ll,int> #define pii pair<int,int> #define cd complex<double> #define ull unsigned long long #define base 1000000000000000000 #define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double eps=1e-6; const int N=200000+10,maxn=5000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f; struct LCT{ int fa[N],ch[N][2],rev[N],sz[N],q[N]; inline bool isroot(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;} inline void pushup(int x){sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;} inline void pushdown(int x) { if(rev[x]) { rev[x]=0;swap(ch[x][0],ch[x][1]); rev[ch[x][0]]^=1,rev[ch[x][1]]^=1; } } inline void Rotate(int x) { int y=fa[x],z=fa[y],l,r; if(ch[y][0]==x)l=0,r=l^1; else l=1,r=l^1; if(!isroot(y)) { if(ch[z][0]==y)ch[z][0]=x; else ch[z][1]=x; } fa[x]=z;fa[y]=x;fa[ch[x][r]]=y; ch[y][l]=ch[x][r];ch[x][r]=y; pushup(y);pushup(x); } inline void splay(int x) { int top=1;q[top]=x; for(int i=x;!isroot(i);i=fa[i])q[++top]=fa[i]; for(int i=top;i;i--)pushdown(q[i]); while(!isroot(x)) { int y=fa[x],z=fa[y]; if(!isroot(y)) { if((ch[y][0]==x)^(ch[z][0]==y))Rotate(x); else Rotate(y); } Rotate(x); } } inline void access(int x){for(int y=0;x;y=x,x=fa[x])splay(x),ch[x][1]=y,pushup(x);} inline void makeroot(int x){access(x),splay(x),rev[x]^=1;} inline int findroot(int x){access(x),splay(x);while(ch[x][0])x=ch[x][0];return x;} inline void split(int x,int y){makeroot(x),access(y),splay(y);} inline void cut(int x,int y){split(x,y);if(ch[y][0]==x)ch[y][0]=0,fa[x]=0;} inline void link(int x,int y){makeroot(x),fa[x]=y,splay(x);} }lct; int a[N]; inline const int read(){ register int f=1,x=0; register char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} return f*x; } int main() { int n=read(); lct.sz[n+1]=1; for(register int i=1;i<=n;i++) { a[i]=read(); a[i]=(i+a[i]<=n+1?i+a[i]:n+1); lct.fa[i]=a[i]; lct.sz[i]=1; } int m=read(); while(m--) { int op=read(); if(op==1) { int x=read()+1; lct.makeroot(n+1); lct.access(x),lct.splay(x); printf("%d\n",lct.sz[x]-1); } else { int x=read()+1,y=read(); lct.cut(x,a[x]); a[x]=(x+y<=n+1?x+y:n+1); lct.link(x,a[x]); } } return 0; } /******************** ********************/
bzoj2002: [Hnoi2010]Bounce 彈飛綿羊 lct