珂朵莉树求调
查看原帖
珂朵莉树求调
1015670
Lyco_Reco楼主2023/11/14 20:03

如题,样例没过

#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <climits>
#include <set>
using namespace std;
#define int long long
#define inf 2147483647
#define maxn 500010
#define IT set<node>::iterator
int n,q;
int head[maxn],nxt[maxn<<1],to[maxn<<1],tot;
int fa[maxn],sz[maxn],wc[maxn],dep[maxn],dfn[maxn],rdfn[maxn],top[maxn],vis;
int rd(){
	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;
}
void wrt(int x){
	int y=10,len=1;
	if(x<0){
		x=-x;
		putchar('-');
	}
	while(y<=x){
		len++;
		y*=10;
	}
	while(len--){
		y/=10;
		putchar(x/y+'0');
		x%=y;
	}
}
void add(int u,int v){
	to[++tot]=v;
	nxt[tot]=head[u];
	head[u]=tot;
}
struct node{
	int l,r;
	mutable int v;
	node(int l,int r=-1,int v=0): l(l),r(r),v(v) {}
	friend bool operator<(node a,node b){
		return a.l<b.l;
	}
};
set<node> s;
int _min(int a,int b){
	return a<b? a:b;
}
int _max(int a,int b){
	return a>b? a:b;
}
void dfs1(int u,int f){
	fa[u]=f;
	dep[u]=dep[f]+1;
	sz[u]=1;
	for(int i=head[u];i;i=nxt[i]){
		int v=to[i];
		if(f==v){
			continue;
		}
		dfs1(v,u);
		sz[u]+=sz[v];
		if(sz[v]>sz[wc[u]]){
			wc[u]=v;
		}
	}
}
void dfs2(int u,int topf){
	dfn[u]=++vis;
	rdfn[vis]=u;
	top[u]=topf;
	if(wc[u]){
		dfs2(wc[u],topf);
		for(int i=head[u];i;i=nxt[i]){
			int v=to[i];
			if(fa[u]==v||v==wc[u]){
				continue;
			}
			dfs2(v,v);
		}
	}
}
IT split(int pos){
	IT it=s.lower_bound(node(pos));
	if(it!=s.end()&&it->l==pos){
		return it;
	}
	it--;
	if(it->r<pos){
		return s.end();
	}
	int l=it->l,r=it->r,v=it->v;
	s.erase(it);
	s.insert(node(l,pos-1,v));
	return s.insert(node(pos,r,v)).first;
}
void assign(int l,int r,int v){
	IT itr=split(r+1),itl=split(l);
	s.erase(itl,itr);
	s.insert(node(l,r,v));
}
int query(int pos){
	IT it=split(pos);
	return it->v;
}
void upd(int x,int y,int z){
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]]){
			swap(x,y);
		}
		assign(dfn[top[x]],dfn[x],z);
		x=fa[top[x]];
	}
	assign(_min(dfn[x],dfn[y]),_max(dfn[x],dfn[y]),z);
}
void slv(){
	n=rd();
	for(int i=1;i<n;i++){
		int u=rd(),v=rd();
		add(u,v);
		add(v,u);
	}
	dfs1(1,0);
	dfs2(1,0);
	s.insert(node(1,n,0));
	q=rd();
	while(q--){
		int op=rd();
		if(op==1){
			int u=rd();
			assign(dfn[u],dfn[u]+sz[u]-1,1);
		}
		else if(op==2){
			int u=rd();
			upd(u,1,0);
		}
		else{
			int u=rd();
			wrt(query(u));
			putchar('\n');
		}
	}
}
signed main(){
	slv();
	return 0;
}
2023/11/14 20:03
加载中...