标签:namespace sync set lex swap 就是 节点 type oid
树上弹飞绵羊,现场树分块没写出来= =
先预处理倍增,新建一个n+1节点,能弹到就建一条边,然后每操作2就cut,然后link,1的答案就是x到n+1的距离,
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#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")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 998244353
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#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 Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
template<typename T>
inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>
inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
using namespace std;
const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=200000+10,maxn=400000+10,inf=0x3f3f3f3f;
struct LCT{
int fa[N],ch[N][2],rev[N],sz[N],q[N];
void init()
{
memset(ch,0,sizeof ch);
memset(fa,0,sizeof fa);
memset(sz,0,sizeof sz);
memset(rev,0,sizeof rev);
}
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 fa[20][N],a[N],n;
int go(int u,int len)
{
for(int i=19;i>=0;i--)
if(len>=(1<<i))
u=fa[i][u],len-=(1<<i);
return u?u:n+1;
}
void solve()
{
lct.init();
scanf("%d",&n);
for(int i=2;i<=n;i++)scanf("%d",&fa[0][i]);
for(int i=1;i<20;i++)
for(int j=1;j<=n;j++)
fa[i][j]=fa[i-1][fa[i-1][j]];
for(int i=1;i<=n+1;i++)lct.sz[i]=1;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
lct.link(i,go(i,a[i]));
lct.sz[i]=1;
}
int q;scanf("%d",&q);
while(q--)
{
int op,x,y;scanf("%d%d",&op,&x);
if(op==1)
{
lct.split(n+1,x);
printf("%d\n",lct.sz[x]-1);
}
else
{
scanf("%d",&y);
lct.cut(x,go(x,a[x]));
a[x]=y;
lct.link(x,go(x,a[x]));
}
}
}
int main()
{
int T;scanf("%d",&T);
while(T--)solve();
return 0;
}
/********************
********************/
标签:namespace sync set lex swap 就是 节点 type oid
原文地址:https://www.cnblogs.com/acjiumeng/p/9472624.html