1.别忘了初始化,初始时各个节点颜色互不相同,也就是ans = d(到根的距离)
2.rot时分清谁和谁, 我脑残写了 fa[x] = y , fa[y] = x
#include<iostream>
#include<cstdio>
using namespace std;
const int N = 210000;
inline int read()
{
register int x = 0; register char c = getchar();
while(c < '0' || c > '9') c = getchar(); while(c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0' , c = getchar();
return x;
}
int n , m , dfn , cnt;
int head[N] , st[N] , ed[N] , d[N] , fdfn[N] , f[N][20] , siz[N] , fa[N] , tr[N][2] , rev[N] , maxn[N<<2] , tag[N<<2];
struct edge{int v , nex; } e[N<<1];
inline void add(int u , int v) { e[++cnt].v = v; e[cnt].nex = head[u]; head[u] = cnt; return ; }
#define D_bag puts("this is OK!!");
// Segment_tree->begin()
void Down(int k)
{
if(tag[k])
{
maxn[k<<1] += tag[k]; maxn[k<<1|1] += tag[k];
tag[k<<1] += tag[k]; tag[k<<1|1] += tag[k]; tag[k] = 0; // tag -->clear
}
return ;
}
void build(int k , int l , int r) // 建树初始化
{
if(l == r)
{
maxn[k] = d[fdfn[l]];
return ;
}
int mid = (l + r) >> 1;
build(k << 1 , l , mid);
build(k << 1 | 1 , mid + 1 , r);
maxn[k] = max(maxn[k<<1] , maxn[k<<1|1]);
return ;
}
void Change(int k , int l , int r , int x , int y , int val)
{
if(x <= l && r <= y) return (void)(maxn[k] += val , tag[k] += val);
int mid = (l + r) >> 1; Down(k);
if(x <= mid) Change(k << 1 , l , mid , x , y , val);
if(y > mid) Change(k << 1 | 1 , mid+1 , r , x , y , val);
maxn[k] = max(maxn[k<<1] , maxn[k<<1|1]); return ;
}
int Ask(int k , int l , int r , int x , int y)
{
if(x <= l && r <= y) return maxn[k];
Down(k); int mid = (l + r) >> 1 , ans = 0;
if(x <= mid) ans = max(ans , Ask(k<<1 , l , mid , x , y));
if(y > mid) ans = max(ans , Ask(k<<1|1 , mid+1 , r , x , y));
return ans;
}
// Segment_tree->end()
inline int witch(int x) { return x == tr[fa[x]][1]; }
inline int nrt(int x) { return (fa[x] && (tr[fa[x]][0] == x || tr[fa[x]][1] == x)); }
void rot(int x)
{
int y = fa[x] , z = fa[y] , k = witch(x) , w = tr[x][!k];
if(nrt(y)) tr[z][witch(y)] = x; fa[x] = z; // !!!!
tr[y][k] = w; if(w) fa[w] = y;
tr[x][!k] = y; fa[y] = x;
}
void Splay(int x)
{
while(nrt(x))
{
if(nrt(fa[x])) rot(witch(x) == witch(fa[x]) ? fa[x] : x);
rot(x);
}
return ;
}
int find_son(int x)
{
while(tr[x][0]) x = tr[x][0]; return x;
}
void assess(int x)
{
int t = 0;
for(t = 0; x ; t = x , x = fa[x])
{
Splay(x);
if(t)
{
int son = find_son(t);
Change(1 , 1 , n , st[son] , ed[son] , -1);
}
if(tr[x][1])
{
int son = find_son(tr[x][1]);
Change(1 , 1 , n , st[son] , ed[son] , 1);
}
tr[x][1] = t;
}
return ;
}
int lca(int x , int y)
{
if(x == y) return x;
if(d[x] < d[y]) swap(x , y);
for(int i = 18 ; i >= 0 ; --i) if(d[f[x][i]] >= d[y]) x = f[x][i];
if(x == y) return x;
for(int i = 18 ; i >= 0 ; --i) if(f[x][i] != f[y][i]) x = f[x][i] , y = f[y][i];
return f[x][0];
}
void dfs(int x)
{
st[x] = ++dfn; fdfn[dfn] = x;
for(int i = 1 ; i <= 18 ; ++i) f[x][i] = f[f[x][i-1]][i-1];
for(int i = head[x] , v; i ; i = e[i].nex)
{
v = e[i].v;
if(v == f[x][0]) continue;
f[v][0] = x; d[v] = d[x] + 1; fa[v] = x; dfs(v);
}
ed[x] = dfn;
return ;
}
int main()
{
n = read(); m = read();
for(int i = 1 , a , b; i <= n - 1 ; ++i)
{
a = read(); b = read();
add(a , b); add(b , a);
}
d[1] = 1; dfs(1);
build(1 , 1 , n);
for(int i = 1 , op , x ; i <= m ; ++i)
{
op = read(); x = read();
if(op == 1) assess(x);
else
if(op == 2)
{
int y = read() , p = lca(x , y);
int ans = Ask(1 , 1 , n , st[x] , st[x]) + Ask(1 , 1 , n , st[y] , st[y]) - 2 * Ask(1 , 1 , n , st[p] , st[p]) + 1;
printf("%d\n" , ans);
}
else
if(op == 3)
printf("%d\n" , Ask(1 , 1 , n , st[x] , ed[x]));
}
return 0;
}
/*
5 6
1 2
2 3
3 4
3 5
2 4 5
3 3
1 4
2 4 5
1 5
2 4 5
*/
4817 [Sdoi2017]树点涂色 标签:颜色 最大权值 初始 define 操作 ios val 路径 getchar 原文地址:https://www.cnblogs.com/R-Q-R-Q/p/12158712.html
|