4817 [Sdoi2017] Coloring tree points

Title Description

Bob has an n points rooted tree, where point 1 is the root node. Bob coating color at each point, and different colors at each point.

Weights define a path is: How many different colors point on this path (including start and end) total.

Bob might be these types of operations:

  • 1 x X represents the point to all points on the unused one kind of infected root node path of the new color.
  • 2 x y Seeking the path from x to y of the weight.
  • 3 x Select a point x to the root of the subtree, so that the point to the root of the maximum weight path, find a maximum priority value.

Bob will be a total of m times the operation

Input Format

The first line of the two numbers n, m.

Next, n-1 lines of two numbers a, b indicates there is an edge between a and b.

Subsequently m row, showing the operation, formatted as described in the title

Output Format

2,3 operation occurs whenever the output line.

If 2 operation, outputs a number representing the path weights

If the operation is the maximum value 3, the output represents a number of weights

Sample input and output

Input # 1

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

Output # 1

3
4
2
2

Description / Tips

\(n , m <= 1e5\)

analysis

The same color is seen as a point Splay, then the operator 1 is the standard access

For twenty-three maintenance operation, a number of the color point to the root, but also the number of Splay.

This can maintain a segment tree

When assess the operation, and will go up to the sub-tree - Disconnect the sub-tree +

note

1. Remember initialization, each node initially mutually different colors, i.e. ans = d (distance to the root)
to distinguish when and who 2.rot, brain damage I wrote a 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
*/
@. Posted 2020-01-06 21:52   R-qrq   read ( ... ) Comments ( ... edit   collections

Guess you like

Origin www.cnblogs.com/R-Q-R-Q/p/12158712.html