zcmimi's blog
查看原题

点击跳转

#include<cstdio>
#include<algorithm>
#define RG register
#define I inline
#define R int
#define lc c[x][0]
#define rc c[x][1]
#define G if(++ip==ie)if(fread(ip=ibuf,1,L,stdin))
using namespace std;
const int N=5e5+9,M=1.5e6+9,L=1<<19;
char ibuf[L],*ie=ibuf+L,*ip=ie-1;
int n,f[M],c[N][2],t[N],n1[N],n2[N],v[M],q[M],d[N];
I int max(R x,R y){return x>y?x:y;}
I int in(){
    G;while(*ip<'-')G;
    R x=*ip&15;G;
    while(*ip>'-'){(x*=10)+=*ip&15;G;}
    return x;
}
I bool nrt(R x){
    return c[f[x]][0]==x||c[f[x]][1]==x;
}
I void up(R x){
    if(!(n1[x]=n1[rc])&&!(n1[x]=x*(v[x]!=1)))n1[x]=n1[lc];
    if(!(n2[x]=n2[rc])&&!(n2[x]=x*(v[x]!=2)))n2[x]=n2[lc];
}
I void dn(R x,R tg){
    v[x]^=3;swap(n1[x],n2[x]);t[x]+=tg;
}
I void all(R x){
    if(nrt(x))all(f[x]);
    if(t[x])dn(lc,t[x]),dn(rc,t[x]),t[x]=0;
}
I void rot(R x){
    R y=f[x],z=f[y],k=c[y][1]==x,w=c[x][!k];
    if(nrt(y))c[z][c[z][1]==y]=x;
    f[f[f[c[c[x][!k]=y][k]=w]=y]=x]=z;up(y);
}
I void sp(R x){
    all(x);
    for(R y;nrt(x);rot(x))
        if(nrt(y=f[x]))rot((c[f[y]][0]==y)^(c[y][0]==x)?x:y);
    up(x);
}
I void ac(R x){
    for(R y=0;x;sp(x),rc=y,up(y=x),x=f[x]);
}
int main(){
    n=in();R he,tl=0,i,x,tp,nowrt;
    for(i=1;i<=n;++i)d[f[in()]=f[in()]=f[in()]=i]=3;
    for(;i<=3*n+1;++i)v[q[++tl]=i]=in()<<1;
    for(he=1;he<=tl;++he){
        x=q[he];if(x<=n)up(x);
        v[f[x]]+=v[x]>>1;
        if(!--d[f[x]])q[++tl]=f[x];
    }
    nowrt=v[1]>>1;
    for(R q=in();q;--q){
        tp=(v[x=in()]^=2)-1;
        ac(x=f[x]);sp(x);
        if((~tp?n1:n2)[x]){
            sp(x=(~tp?n1:n2)[x]);
            dn(rc,tp),up(rc);
            v[x]+=tp;up(x);
        }
        else dn(x,tp),up(x),nowrt^=1;
        putchar(nowrt|'0');putchar('\n');
    }
}
LG 4332 [SHOI2014]三叉神经树
comment评论
Search
search