• Codeforces 343D


    div1里看到树剖就像看见亲爹(

    #include<bits/stdc++.h>
    //#pragma comment(linker, "/STACK:1024000000,1024000000") 
    #include<stdio.h>
    #include<algorithm>
    #include<queue>
    #include<string.h>
    #include<iostream>
    #include<math.h>
    #include<set>
    #include<map>
    #include<vector>
    #include<iomanip>
    using namespace std;
    #define ll long long
    #define pb push_back
    #define FOR(a) for(int i=1;i<=a;i++)
    const int inf=0x3f3f3f3f;
    const int maxn=5e5+9;  
    //int arr[maxn];
     
    int n;
     
    struct EDGE{
        int v;int next;
    }G[maxn<<2];
    int head[maxn],tot;
    
    void addedge(int u,int v){
        ++tot;G[tot].v=v;G[tot].next=head[u];head[u]=tot;
    }
     
    int top[maxn];
    int pre[maxn];
    int dep[maxn];
    int num[maxn];
    int p[maxn];    //v的对应位置
    int out[maxn];  //退出时间戳
    int fp[maxn];   //访问序列
    int son[maxn];  //重儿子
    int pos;
    
    void init(){
        memset(head,-1,sizeof head);tot=0;
    	memset(son,-1,sizeof son);
    }
    
    void dfs1(int u,int fa,int d){
        dep[u]=d;
        pre[u]=fa;
        num[u]=1;
        for(int i=head[u];~i;i=G[i].next){
            int v=G[i].v;
            if(v==fa)continue;
            dfs1(v,u,d+1);
            num[u]+=num[v];
            if(son[u]==-1||num[v]>num[son[u]])son[u]=v;
        }
    }
    void getpos(int u,int sp){
        top[u]=sp;
        p[u]=out[u]=++pos;
        fp[p[u]]=u;
        if(son[u]==-1)return;
        getpos(son[u],sp);
        for(int i=head[u];~i;i=G[i].next){
            int v=G[i].v;
            if(v!=son[u]&&v!=pre[u])getpos(v,v);
        }
        out[u]=pos;
    }
     
    struct NODE{
    	int col;	//-1非纯色
    }ST[maxn<<2];
    void pushup(int rt){
    	if(ST[rt<<1].col==ST[rt<<1|1].col)ST[rt].col=ST[rt<<1].col;
    	else ST[rt].col=-1;
    }
    void pushdown(int rt){
    	if(ST[rt].col!=-1){
    		ST[rt<<1].col=ST[rt<<1|1].col=ST[rt].col;
    	}
    }
    void update(int a,int b,int c,int l,int r,int rt){
    	if(a<=l && b>=r){
    		ST[rt].col=c;return;
    	}
    	pushdown(rt);
    	int m=l+r>>1;
    	if(a<=m)update(a,b,c,l,m,rt<<1);
    	if(b>m)update(a,b,c,m+1,r,rt<<1|1);
    	pushup(rt);
    }
    int query(int a,int l,int r,int rt){
    	if(ST[rt].col!=-1)return ST[rt].col;
    	pushdown(rt);
    	int m=l+r>>1;
    	if(a<=m)return query(a,l,m,rt<<1);
    	else return query(a,m+1,r,rt<<1|1);
    }
    
    void solve(int u,int v){
    
        while(top[u]!=top[v]){
    		if(dep[top[u]]<dep[top[v]])swap(u,v);
    		//cout<<u<<"~~~"<<top[u]<<endl;
            //cout<<p[top[u]]<<" "<<p[u]<<endl;
            update(p[top[u]],p[u],0,1,n,1);
    		u=pre[top[u]];
        }
    
    	if(dep[u]<dep[v])swap(u,v);
        update(p[v],p[u],0,1,n,1);
    }
     
    int main(){
    	scanf("%d",&n);
    	init();
    
    	for(int i=1,x,y;i<n;i++){
    		scanf("%d%d",&x,&y);
    		addedge(x,y);addedge(y,x);
    	}
    
    	dfs1(1,1,0);
    	getpos(1,1);
    
    	int q;scanf("%d",&q);
    	while(q--){
    		int knd,u;
    		scanf("%d%d",&knd,&u);
    		if(knd==1){
    			update(p[u],out[u],1,1,n,1);
    		}else if(knd==2){
    			solve(u,1);
    		}else{
    			printf("%d
    ",query(p[u],1,n,1));
    		}
    	}
    }

    还发现一个恐怖的事情就是我的板子接口变化莫测,有空得整理一波


  • 相关阅读:
    C#:字符串(转载9)
    C# 数组(转载8)
    C#:可空类型(转载7)
    XD 05
    eclipse 快捷键 干货
    XD 04
    XD 03
    model, mapper, xml
    02 MyBatis & Druid
    注解 用到
  • 原文地址:https://www.cnblogs.com/Drenight/p/8611256.html
Copyright © 2020-2023  润新知