题目链接:

洛谷:题目链接

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define pb push_back
const int N=1e6+10;
void read(int &a)
{
    a=0;int d=1;char ch;
    while(ch=getchar(),ch>'9'||ch<'0')
        if(ch=='-')
            d=-1;
    a=ch^48;
    while(ch=getchar(),ch>='0'&&ch<='9')
        a=(a<<3)+(a<<1)+(ch^48);
    a*=d;
}
int f[N],son[N],dep[N],siz[N],id[N],cnt,top[N],n,rt=1;
struct note{int l,r,sum,lazy;}tree[N<<1];
vector <int> v[N];
void dfs1(int x)
{
    siz[x]=1,dep[x]=dep[f[x]]+1;
    for(auto i:v[x])
    {
        if(i!=f[x])
        {
            f[i]=x;dfs1(i),siz[x]+=siz[i];
            if(siz[son[x]]<siz[i]) son[x]=i;
        }
    }
}
void dfs2(int x,int tp)
{
    top[x]=tp,id[x]=++cnt;
    if(son[x]) dfs2(son[x],tp);
    for(auto i:v[x]) if(i!=f[x]&&i!=son[x]) dfs2(i,i);
}
void build(int l,int r,int now)
{
    tree[now].l=l,tree[now].r=r;
    tree[now].lazy=-1;
    if(l==r) return;
    int m=l+r>>1;
    build(l,m,now<<1),build(m+1,r,now<<1|1);
}
void work(int now,int k)
{
    tree[now].sum=(tree[now].r-tree[now].l+1)*k;
    tree[now].lazy=k;
}
void pushdown(int now)
{
    work(now<<1,tree[now].lazy);
    work(now<<1|1,tree[now].lazy);
    tree[now].lazy=-1;
}
void modify(int l,int r,int now,int w)
{
    if(tree[now].l>=l&&tree[now].r<=r) {work(now,w);return;}
    if(tree[now].lazy!=-1) pushdown(now);
    int m=tree[now].l+tree[now].r>>1;
    if(m>=l) modify(l,r,now<<1,w);
    if(m<r) modify(l,r,now<<1|1,w);
    tree[now].sum=tree[now<<1].sum+tree[now<<1|1].sum;
}
int query(int l,int r,int now)
{
    if(tree[now].l>=l&&tree[now].r<=r) {return tree[now].sum;}
    if(tree[now].lazy!=-1) pushdown(now);
    int m=tree[now].l+tree[now].r>>1;
    int res=0;
    if(m>=l) res+=query(l,r,now<<1);
    if(m<r) res+=query(l,r,now<<1|1);
    tree[now].sum=tree[now<<1].sum+tree[now<<1|1].sum;
    return res;
}
void update(int x,int y,int z)
{
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        modify(id[top[x]],id[x],1,z);
        x=f[top[x]];
    }
    if(dep[x]>dep[y]) swap(x,y);
    modify(id[x],id[y],1,z);
}
int main()
{
    read(n);
    for(re int i=1,x,y;i<n;i++) read(x),read(y),v[x].pb(y),v[y].pb(x);
    dfs1(rt),dfs2(rt,rt);
    build(1,n,1);
    int T; read(T);
    for(re int i=1,op,x;i<=T;i++)
    {
        read(op),read(x);
        if(op==1) modify(id[x],id[x]+siz[x]-1,1,1);
        else if(op==2) update(1,x,0);
        else printf("%d\n",query(id[x],id[x],1));
    }
    return 0;
}
01-07 02:52