We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<bits/stdc++.h>usingnamespacestd;#define rep(i, x, y) for(int i = x; i <= y; ++i)#define dep(i, x, y) for(int i = x; i >= y; --i)#define N 100005structbst{bst*l,*r,*fa;intsize;inlinevoidupdata(){size=1+(l?l->size:0)+(r?r->size:0);}inlinebooltop(){return!fa||fa&&fa->l!=this&&fa->r!=this;}inlinevoidzig(){bst*y=fa,*z=y->fa;y->l=r;if(r)r->fa=y;r=y;y->fa=this;fa=z;if(z&&z->l==y)z->l=this;if(z&&z->r==y)z->r=this;y->updata();}inlinevoidzag(){bst*y=fa,*z=y->fa;y->r=l;if(l)l->fa=y;l=y;y->fa=this;fa=z;if(z&&z->l==y)z->l=this;if(z&&z->r==y)z->r=this;y->updata();}}t[N];voidsplay(bst*x){while(!x->top()){bst*y=x->fa,*z=y->fa;if(!y->top()){if(z->l==y){if(y->l==x)y->zig(),x->zig();elsex->zag(),x->zig();}else{if(y->r==x)y->zag(),x->zag();elsex->zig(),x->zag();}}else{if(y->l==x)x->zig();elsex->zag();}}x->updata();}bst*access(bst*x){bst*y=NULL;for(;x;y=x,x=x->fa){splay(x);x->r=y;x->updata();}returny;}intn,m,x,y,son[N],Next[N<<1],ed[N<<1],w[N],f[N][30];voiddfs(intx,intfa){f[x][0]=fa;rep(i,1,20)f[x][i]=f[f[x][i-1]][i-1];for(intj=son[x];j;j=Next[j])if(ed[j]^fa){t[ed[j]].fa=t+x;dfs(ed[j],x);}}intjump(intx,intw){dep(i,20,0)if(1<<i<w){w-=1<<i;x=f[x][i];}x=f[x][0];if(!x)x=1;returnx;}bst*Right(bst*x){while(x->r)x=x->r;returnx;}voidreset_w(intx,intw){intp=jump(x,w);bst*y=t+x;access(y);splay(y);access(Right(y->l));splay(y);y->fa=t+p;}intmain(){scanf("%d%d",&n,&m);rep(i,1,n)scanf("%d",w+i);rep(i,2,n){scanf("%d%d",&x,&y);Next[i]=son[x],son[x]=i,ed[i]=y;Next[i+n]=son[y],son[y]=i+n,ed[i+n]=x;}rep(i,1,n)t[i].size=1;dfs(1,0);rep(i,2,n)reset_w(i,w[i]);while(m--){intopt;scanf("%d%d",&opt,&x);if(opt==1){scanf("%d",&y);if(x^1)reset_w(x,y);}else{bst*v=access(t+x);printf("%d\n",v->size-1);}}}
No more comments
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Can you explain this please ? taweez for love attraction
C++ solution