to hangry

卡布叻_周深發表於2024-10-05
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int N=2e5+10,P=10;
int a[N],dep[N]; vector<int>e[N];
void dfs(int x,int t) {dep[x]=dep[t]+1; for(int y:e[x]) if(y!=t) dfs(y,x);}
signed main()
{
    freopen("/dev/urandom","r",stdin);
    freopen("rand.out","w",stdout);
    srand(getchar()*getchar()*getchar()*time(0));
    int n=rand()%P+1,m=rand()%P+1;
    printf("%d\n",n);
    for(int i=2,x;i<=n;i++)
    {
        x=rand()%(i-1)+1;
        printf("%d %d\n",x,i);
        e[x].pb(i),e[i].pb(x);
    }
    dfs(1,0);
    printf("%d\n",m);
    for(int i=1,op,x,y;i<=m;i++)
    {
        op=rand()%3+1;
        if(op==1) printf("%d %d\n",op,rand()%n+1);
        if(op==2)
        {
            int x=rand()%n+1,y=rand()%(dep[x]*2);
            y+=(y&1);
            printf("%d %d %d\n",op,x,y);
        }
        if(op==3) printf("%d %d\n",op,rand()%max(1,i-1));
    }
}
#include<bits/stdc++.h>
#define ll long long 
#define endl '\n'
#define sort stable_sort
#define pb push_back
using namespace std;
const int N=1e5+10,M=2e6+10;
template<typename Tp> inline void read(Tp&x)
{
	x=0;register bool z=true;
	register char c=getchar_unlocked();
	for(;!isdigit(c);c=getchar_unlocked()) if(c=='-') z=0;
	for(;isdigit(c);c=getchar_unlocked()) x=(x<<1)+(x<<3)+(c^48);
	x=(z?x:~x+1);
}
template<typename T,typename ...Tp> inline void read(T &x,Tp &...y){read(x);read(y...);}
template<typename Tp> inline void wt(Tp x){if(x>9)wt(x/10);putchar_unlocked((x%10)+'0');}
template<typename Tp> inline void write(Tp x){if(x<0)putchar_unlocked('-'),x=~x+1;wt(x);}
template<typename T,typename ...Tp> inline void write(T x,Tp ...y){write(x);putchar_unlocked(' ');write(y...);}
int n,m,tot,rt[N],ls[M],rs[M],in[N],out[N],dep[N],val[M],ans[N],fa[N][20];
struct aa {int op,x,y;}q[N]; vector<int>e[N],g[N]; bool a[N];
void change(int &p,int l,int r,int x,int d)
{
	if(!p) p=++tot;
	if(l==r) return val[p]+=d,void();
	int mid=l+r>>1;
	x<=mid?change(ls[p],l,mid,x,d):change(rs[p],mid+1,r,x,d);
	val[p]=val[ls[p]]+val[rs[p]];
}
int ask(int p,int l,int r,int vl,int vr)
{
	if(!p) return 0;
	if(vl<=l&&vr>=r) return val[p];
	int res=0,mid=l+r>>1;
	if(vl<=mid) res+=ask(ls[p],l,mid,vl,vr);
	if(vr>mid) res+=ask(rs[p],mid+1,r,vl,vr);
	return res;
}
int ask(int x,int k) {return ask(rt[k],1,n,in[x],out[x]);}
void dfs(int x,int t)
{
	in[x]=++tot,dep[x]=dep[fa[x][0]=t]+1;
	for(int i=1;i<=__lg(dep[x]);i++) fa[x][i]=fa[fa[x][i-1]][i-1];
	for(int y:e[x]) if(y!=t) dfs(y,x); out[x]=tot;
}
int find(int x,int k)
{
	if(dep[x]-1<k) return 0;
	for(int i=__lg(dep[x]);~i;i--) if((k>>i)&1) x=fa[x][i];
	return x;
}
void solve(int i)
{
	int op=q[i].op,x=q[i].x,y=q[i].y,tmp;
	if(op==1) change(rt[dep[x]],1,n,in[x],a[x]?-1:1),a[x]^=1;
	if(op==2) if(!(y&1))
	{
		if(!y) ans[i]=a[x];
		else if(fa[tmp=find(x,(y>>1)-1)][0])
			ans[i]=ask(fa[tmp][0],dep[x])-ask(tmp,dep[x]);
	}
	for(int j:g[i]) solve(j);
	if(op==1) change(rt[dep[x]],1,n,in[x],a[x]?-1:1),a[x]^=1;
}
signed main()
{
	read(n);
	for(int i=1,x,y;i<n;i++) read(x,y),e[x].pb(y),e[y].pb(x);
	dfs(1,0),tot=0; read(m);
	for(int i=1,op,x,y;i<=m;i++)
	{
		read(op,x); if(op==2) read(y);
		op!=3?g[i-1].pb(i):g[x].pb(i),q[i]={op,x,y};
	}
	solve(0);
	for(int i=1;i<=m;i++) if(q[i].op==2) write(ans[i]),puts("");
}