样例没过,错误未知,OI萌新求调
查看原帖
样例没过,错误未知,OI萌新求调
585011
__AlRTy__楼主2022/12/22 23:11

样例没过,错误未知,/kel 求调

#include<map>
#include<cmath>
#include<cstdio>
#include<string>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')
        x=x*10+ch-'0',ch=getchar();
    return x*f;
}
const int N=5e5+10;
int T,n,cnt;
int a[N*4];
int fa[N],zson[N],dep[N],pre[N],top[N],dfn[N],head[N],size[N];
struct Node{
    int x,to,nxt;
}edge[N];
struct Tree{
    int sum,tag;
}tree[N*4];
void add(int x,int y){
    edge[++cnt]=((Node){x,y,head[x]}),head[x]=cnt;
    edge[++cnt]=((Node){y,x,head[y]}),head[y]=cnt;
    return ;
}
void dfs1(int x,int f){
    size[x]=1;
    for(int i=head[x];i;i=edge[i].nxt){
        if(edge[i].to==f){
            continue ;
        }
        dep[edge[i].to]=dep[x]+1;
        fa[edge[i].to]=x;
        dfs1(edge[i].to,x);
        size[x]+=size[edge[i].to];
        if(size[edge[i].to]>size[zson[x]]){
            zson[x]=edge[i].to;
        }
    }
    return ;
}
void dfs2(int x,int Top){
    top[x]=Top;
    dfn[x]=++cnt;
    pre[cnt]=x;
    if(zson[x]){
        dfs2(zson[x],Top);
    }
    for(int i=head[x];i;i=edge[i].nxt){
        if(edge[i].to==fa[x] || edge[i].to==zson[x]){
            continue ;
        }
        dfs2(edge[i].to,edge[i].to);
    }
    return ;
}
void push_down(int o){
    tree[o<<1].sum=tree[o<<1|1].sum=tree[o].sum;
    tree[o<<1].tag=tree[o<<1|1].tag=tree[o].tag;
    tree[o].tag=-1;
    return ;
}
void Swap(int &x,int &y){
    int t=x;
    x=y;
    y=t;
    return ;
}
void update(int o,int l,int r,int ql,int qr,int v){
    int mid=(l+r)>>1;
    if(l==r){
        tree[o].sum=v;
        tree[o].tag=v;
        return ;
    }
    if(tree[o].tag>=0){
        push_down(o);
    }
    if(ql<=mid){
        update(o<<1,l,mid,ql,qr,v);
    }
    if(qr>mid){
        update(o<<1|1,mid+1,r,ql,qr,v);
    }
    return ;
}
void cover_line(int x,int y){
    while(top[x]!=top[y]){
        if(dep[top[x]]<dep[top[y]]){
            Swap(x,y);
        }
        update(1,1,n,dfn[top[x]],dfn[x],0);
        x=fa[top[x]];
    }
    if(dep[x]<dep[y]){
        Swap(x,y);
    }
    update(1,1,n,dfn[x],dfn[y],0);
    return ;
}
int query(int o,int l,int r,int p){
    if(l==r){
        return tree[o].sum;
    }
    if(tree[o].tag>=0){
        push_down(o);
    }
    int mid=(l+r)>>1;
    if(p<=mid){
        return query(o<<1,l,mid,p);
    }else{
        return query(o<<1|1,mid+1,r,p);
    }
}
void build(int o,int l,int r){
    int mid=(l+r)>>1;
    if(l==r){
        tree[o].tag=-1;
        return ;
    }
    build(o<<1,l,mid);
    build(o<<1|1,mid+1,r);
    return ;
}
int main(){
    n=read();
    for(int i=1;i<n;i++){
        int x=read(),y=read();
        add(x,y);
    }
    dep[1]=1,fa[1]=1;
    cnt=0;
    dfs1(1,-1);
    dfs2(1,1);
    build(1,1,n);
    T=read();
    while(T--){
        int k=read(),x=read();
        if(k==1){
            update(1,1,n,dfn[x],dfn[x]+size[x]-1,1);
        }else if(k==2){
            cover_line(1,x);
        }else{
            printf("%d\n",query(1,1,n,x));
        }
    }
    return 0;
}
2022/12/22 23:11
加载中...