题解
一写过一交A的水题
只要求一个dfs序,新加一个标记在子树所在的区间上覆盖上该点,维护深度最大的答案
代码
#include <bits/stdc++.h>
#define enter putchar('
')
#define space putchar(' ')
#define pii pair<int,int>
#define fi first
#define se second
#define MAXN 100005
#define pb push_back
#define mp make_pair
#define eps 1e-8
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) out(x / 10);
putchar('0' + x % 10);
}
int N,Q;
struct node {
int to,next;
}E[MAXN * 2];
int head[MAXN],sumE,dfn[MAXN],idx,siz[MAXN],dep[MAXN];
namespace seg_tr {
struct node {
int l,r,cov;
}tr[MAXN * 4];
void build(int u,int l,int r) {
tr[u].l = l;tr[u].r = r;
if(l == r) {
tr[u].cov = 1;
return;
}
int mid = (l + r) >> 1;
build(u << 1,l,mid);
build(u << 1 | 1,mid + 1,r);
}
int max_dep(int a,int b) {
return dep[a] < dep[b] ? b : a;
}
void cover(int u,int v) {
if(!tr[u].cov) tr[u].cov = v;
else tr[u].cov = max_dep(tr[u].cov,v);
}
void push_down(int u) {
if(tr[u].cov) {
cover(u << 1,tr[u].cov);
cover(u << 1 | 1,tr[u].cov);
tr[u].cov = 0;
}
}
void Cover(int u,int l,int r,int v) {
if(tr[u].l == l && tr[u].r == r) {cover(u,v);return;}
int mid = (tr[u].l + tr[u].r) >> 1;
push_down(u);
if(r <= mid) Cover(u << 1,l,r,v);
else if(l > mid) Cover(u << 1 | 1,l,r,v);
else {Cover(u << 1,l,mid,v);Cover(u << 1 | 1,mid + 1,r,v);}
}
int Query(int u,int pos) {
if(tr[u].l == tr[u].r) return tr[u].cov;
push_down(u);
int mid = (tr[u].l + tr[u].r) >> 1;
if(pos <= mid) return Query(u << 1,pos);
else return Query(u << 1 | 1,pos);
}
}
void add(int u,int v) {
E[++sumE].to = v;
E[sumE].next = head[u];
head[u] = sumE;
}
void dfs(int u,int fa) {
dfn[u] = ++idx;
siz[u] = 1;
dep[u] = dep[fa] + 1;
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(v != fa) {
dfs(v,u);
siz[u] += siz[v];
}
}
}
void Solve() {
read(N);read(Q);
int u,v;
for(int i = 1 ; i < N ; ++i) {
read(u);read(v);
add(u,v);add(v,u);
}
dfs(1,0);
seg_tr::build(1,1,N);
char op[5];int a;
while(Q--) {
scanf("%s%d",op + 1,&a);
if(op[1] == 'Q') {out(seg_tr::Query(1,dfn[a]));enter;}
else {seg_tr::Cover(1,dfn[a],dfn[a] + siz[a] - 1,a);}
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}