[SDOI2017]树点涂色【LCT的Access函数的妙用】

题目链接


  有一颗有根树,1为根节点,有这样的操作:我们给从1~x的节点染色成相同且未出现过的颜色,现在我们想知道u到v的简单路径上不同颜色的个数、或者u的子树下到根节点颜色数最多的颜色数是多少?

  其实,我一开始没有往LCT想,但是它非要挂了个标签,于是开始想了,如果说我们将1号节点默认成根节点,并且是整棵LCT树上的根节点,那么,如果一开始将所有的节点看成是独立的Splay树上的节点,每棵Splay树上有且只有一个点。那么,颜色的个数,实际上就是它到根节点需要Access的次数,并且可以换一种表述的方式,如果Access了一次,那么它的子树中的节点(包括它本身)都要减少一个到1号节点的颜色种类数。

  所以,我们可以利用LCT的Access操作来进行维护到根节点的颜色个数了,然后操作2、3便可以求得了。

操作1

在跳Access的时候要注意,我们要给原来的右儿子放逐,但是要去除它的贡献,又因为它是Splay过的,所以我们需要找到它的深度最浅的节点,也就是在Splay树上最左儿子的节点;同理,我们加上新的连接节点时候,也是需要对最浅的节点来对子树来更新的。

操作2

ans = col[u] + col[v] - 2 * col[lca(u, v)] + 1(+1是因为lca节点被多减去了一次);

操作3

ans = query(dfn[u], dfn[u] + size[u] - 1)查询子树信息即可。

Code

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, M, Q;
namespace Segement
{
    int tree[maxN << 2], lazy[maxN << 2];
    void pushdown(int rt)
    {
        if(lazy[rt])
        {
            tree[lsn] += lazy[rt];
            tree[rsn] += lazy[rt];
            lazy[lsn] += lazy[rt];
            lazy[rsn] += lazy[rt];
            lazy[rt] = 0;
        }
    }
    void pushup(int rt)
    {
        tree[rt] = max(tree[lsn], tree[rsn]);
    }
    void update(int rt, int l, int r, int ql, int qr, int val)
    {
        if(ql <= l && qr >= r)
        {
            tree[rt] += val;
            lazy[rt] += val;
            return;
        }
        pushdown(rt);
        int mid = HalF;
        if(qr <= mid) update(QL, val);
        else if(ql > mid) update(QR, val);
        else { update(QL, val); update(QR, val); }
        pushup(rt);
    }
    int query(int rt, int l, int r, int qx)
    {
        if(l == r) return tree[rt];
        pushdown(rt);
        int mid = HalF;
        if(qx <= mid) return query(Lson, qx);
        else return query(Rson, qx);
    }
    int query_Range(int rt, int l, int r, int ql, int qr)
    {
        if(ql <= l && qr >= r) return tree[rt];
        pushdown(rt);
        int mid = HalF;
        if(qr <= mid) return query_Range(QL);
        else if(ql > mid) return query_Range(QR);
        else return max(query_Range(QL), query_Range(QR));
    }
}
using namespace Segement;
int dfn[maxN], tot, end_tim[maxN];
namespace LCT
{
    int fa[maxN], c[maxN][2];
    int r[maxN];
    bool isroot(int x) { return c[fa[x]][0] != x && c[fa[x]][1] != x; }
    void pushup(int x)
    {
        
    }
    void pushr(int x) { swap(c[x][0], c[x][1]); r[x] ^= 1; }
    void pushdown(int x)
    {
        if(r[x])
        {
            if(c[x][0]) pushr(c[x][0]);
            if(c[x][1]) pushr(c[x][1]);
            r[x] = 0;
        }
    }
    void Rotate(int x)
    {
        int y = fa[x], z = fa[y], k = c[y][1] == x;
        if(!isroot(y)) c[z][c[z][1] == y] = x;
        fa[x] = z;
        c[y][k] = c[x][k ^ 1];
        fa[c[x][k ^ 1]] = y;
        c[x][k ^ 1] = y;
        fa[y] = x;
        pushup(y);
        pushup(x);
    }
    int Stap[maxN];
    void Splay(int x)
    {
        int y = x, z = 0;
        Stap[++z] = y;
        while(!isroot(y)) Stap[++z] = y = fa[y];
        while(z) pushdown(Stap[z--]);
        while(!isroot(x))
        {
            y = fa[x]; z = fa[y];
            if(!isroot(y)) (c[z][0] == y) ^ (c[y][0] == x) ? Rotate(x) : Rotate(y);
            Rotate(x);
        }
    }
    int findroot(int x);
    void Access(int x)
    {
        int y = 0, z;
        while(x)
        {
            Splay(x);
            if(c[x][1])
            {
                z = c[x][1];
                while(c[z][0])
                {
                    z = c[z][0];
                }
                update(1, 1, N, dfn[z], end_tim[z], 1);
            }
            c[x][1] = y;
            if(y)
            {
                z = y;
                while(c[z][0])
                {
                    z = c[z][0];
                }
                update(1, 1, N, dfn[z], end_tim[z], -1);
            }
            pushup(x);
            y = x;
            x = fa[x];
        }
    }
    void makeroot(int x)
    {
        Access(x);
        Splay(x);
        pushr(x);
    }
    int findroot(int x)
    {
        Access(x);
        Splay(x);
        while(c[x][0])
        {
            pushdown(x);
            x = c[x][0];
        }
        Splay(x);
        return x;
    }
    void Split(int x, int y)
    {
        makeroot(x);
        Access(y);
        Splay(y);
    }
    void link(int x, int y)
    {
        makeroot(x);
        if(findroot(y) != x)
        {
            fa[x] = y;
            pushup(y);
        }
    }
    void cut(int x, int y)
    {
        makeroot(x);
        if(findroot(y) != x || fa[y] != x || c[y][0]) return;
        fa[y] = c[x][1] = 0;
        pushup(x);
    }
};
using namespace LCT;
namespace Graph
{
    int head[maxN], cnt;
    struct Eddge
    {
        int nex, to;
        Eddge(int a=-1, int b=0):nex(a), to(b) {}
    } edge[maxN << 1];
    inline void addEddge(int u, int v)
    {
        edge[cnt] = Eddge(head[u], v);
        head[u] = cnt++;
    }
    inline void _add(int u, int v) { addEddge(u, v); addEddge(v, u); }
    inline void init()
    {
        cnt = 0;
        for(int i=1; i<=N; i++) head[i] = -1;
    }
};
using namespace Graph;
int root[maxN][20] = {0}, deep[maxN];
void dfs(int u, int father)
{
    fa[u] = father; root[u][0] = father; deep[u] = deep[father] + 1;
    dfn[u] = ++tot;
    for(int i=head[u], v; ~i; i=edge[i].nex)
    {
        v = edge[i].to;
        if(v == father) continue;
        dfs(v, u);
    }
    end_tim[u] = tot;
    update(1, 1, N, dfn[u], end_tim[u], 1);
}
void Init_LCA()
{
    for(int j=1; (1 << j) < N; j++)
    {
        for(int i=1; i<=N; i++)
        {
            root[i][j] = root[root[i][j - 1]][j - 1];
        }
    }
}
int _LCA(int u, int v)
{
    if(deep[u] < deep[v]) swap(u, v);
    int det = deep[u] - deep[v];
    for(int i=log2(det); i>=0; i--)
    {
        if((det >> i) & 1)
        {
            u = root[u][i];
        }
    }
    if(u == v) return u;
    for(int i=log2(N); i>=0; i--)
    {
        if(root[u][i] ^ root[v][i])
        {
            u = root[u][i];
            v = root[v][i];
        }
    }
    return root[u][0];
}
int main()
{
    scanf("%d%d", &N, &Q);
    init();
    for(int i=1, u, v; i<N; i++)
    {
        scanf("%d%d", &u, &v);
        _add(u, v);
    }
    tot = 0;
    dfs(1, 0);
    Init_LCA();
    for(int i=1, op, x, y; i<=Q; i++)
    {
        scanf("%d%d", &op, &x);
        switch (op)
        {
            case 1:
            {
                Access(x);
                break;
            }
            case 2:
            {
                scanf("%d", &y);
                int lca = _LCA(x, y);
                printf("%d\n", query(1, 1, N, dfn[x]) + query(1, 1, N, dfn[y]) + 1 - 2 * query(1, 1, N, dfn[lca]));
                break;
            }
            default:
            {
                printf("%d\n", query_Range(1, 1, N, dfn[x], end_tim[x]));
                break;
            }
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值