样例已过但0pts求调
  • 板块P4116 Qtree3
  • 楼主CNS_5t0_0r2
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/12/7 16:45
  • 上次更新2024/12/7 19:37:55
查看原帖
样例已过但0pts求调
999274
CNS_5t0_0r2楼主2024/12/7 16:45
#include<bits/stdc++.h>
#define mid ((l + r) >> 1)
#define lchild (root << 1)
#define rchild ((root << 1) + 1)
using namespace std;
const int N = 1e5 + 9;
int n,m,root;
int a[N];

struct edge{
	int to,nex;
} e[N << 1];
int head[N],ecnt;
void addedge(int u,int v){
	ecnt++;
	e[ecnt] = (edge){v,head[u]};
	head[u] = ecnt;
}

int fa[N],siz[N],dep[N];
int w_ch[N];
void dfs1(int u,int father){
    fa[u] = father;
    siz[u] = 1;
    dep[u] = dep[father] + 1;
    for(int i = head[u];i;i = e[i].nex){
        int v = e[i].to;
        if(v != father){
            dfs1(v,u);
            siz[u] += siz[v];
            if(siz[v] > siz[w_ch[u]])
                w_ch[u] = v;
        }
    }
}
int top[N];
int dfn[N],rdfn[N],id;
void dfs2(int u,int link_top){
	id++;
	dfn[u] = id;
	rdfn[id] = u;
    top[u] = link_top;
    if(w_ch[u]){
        dfs2(w_ch[u],link_top);
        for(int i = head[u];i;i = e[i].nex){
            int v = e[i].to;
            if(v != fa[u] && v != w_ch[u])
                dfs2(v,v);
        }
    }
}

struct node{
	int id;//区间内第一个黑点 
} t[N << 2];

bool in_range(int l,int r,int L,int R){//[l,r]是否在修改/查询区间[L,R]中 
	return L <= l && r <= R;
}
bool out_range(int l,int r,int L,int R){//[l,r]是否与修改/查询区间[L,R]无交集 
	return l > R || r < L;
}

void push_up(int root){
	t[root].id = t[lchild].id ? t[lchild].id : t[rchild].id;
}

void build(int root,int l,int r){
	if(l == r){//到达叶节点
		t[root].id = 0;
		return;
	}
	build(lchild,l,mid);
	build(rchild,mid + 1,r);
	push_up(root);
}

void update(int root,int l,int r,int pos){
	if(l == r){
		t[root].id = t[root].id ? 0 : pos;
		return;
	}
	if(pos <= mid)
		update(lchild,l,mid,pos);
	else
		update(rchild,mid + 1,r,pos);
	push_up(root);
}

int interval_query(int root,int l,int r,int L,int R){
	if(out_range(l,r,L,R))
		return 0;
	if(in_range(l,r,L,R))
		return t[root].id;
	int q_lc = interval_query(lchild,l,mid,L,R),q_rc = interval_query(rchild,mid + 1,r,L,R);
	return q_lc ? q_lc : q_rc;
}

int query(int u){
	int ret = -1;
	while(top[u]){
		int l = dfn[top[u]],r = dfn[u];
		int tmp = interval_query(1,1,n,l,r);
		ret = tmp;
		u = fa[top[u]];
	}
	return ret;
}


signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> m;
	for(int i = 1;i < n;i++){
		int u,v;
		cin >> u >> v;
		addedge(u,v);
		addedge(v,u);
	}
	root = 1;
	dfs1(root,0);
	dfs2(root,root);
	rdfn[0] = -1;
	build(1,1,n);
	while(m--){
		int op,u;
		cin >> op >> u;
		if(op == 0)
			update(1,1,n,dfn[u]);
		else if(op == 1)
			cout << rdfn[query(u)] << '\n';
	}
	return 0;
}
2024/12/7 16:45
加载中...