Link Cut Tree 应用五

2022年02月9日,第十九天

1. 题目链接:P4219 [BJOI2014]大融合

思路 L C T LCT LCT 解决,时间复杂度 O ( n l o g   n ) O(nlog\ n) O(nlog n) L C T LCT LCT 维护子树信息的模板题。众所周知, L C T LCT LCT 的实边形成的实链是用来维护树链信息的,除了实边之外,还存在着一种虚边,我们定义以下两个数组:

  • i s u m [ i ] isum[i] isum[i] ,表示第 i i i 个结点所有虚边连向的结点 j j j 的子树的结点隔宿;
  • s i z [ i ] siz[i] siz[i] ,表示第 i i i 个结点形成子树的结点个数。

由此我们可以按动态树的更新过程动态更新两个数组,从而对于边 ( u , v ) (u,v) (u,v) 的答案为 ( i s u m [ u ] + 1 ) × ( i s u m [ v ] + 1 ) (isum[u]+1)\times (isum[v]+1) (isum[u]+1)×(isum[v]+1)

#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define re register
#define endl '\n'

using namespace std;

const int MOD = 998244353;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double Pi = acos(-1.0);
const int N = 3e5 + 5;

#define ls(x) (ch[x][0])
#define rs(x) (ch[x][1])

int fa[N], ch[N][2], siz[N], isum[N];
bool rev[N];
inline bool nroot (int x) { return ls(fa[x])==x || rs(fa[x])==x; }
inline void pushup (int x) {
    siz[x] = siz[ls(x)] + siz[rs(x)] + isum[x] + 1;
}
inline void pushrev (int x) {
    swap (ls(x), rs(x));
    rev[x] ^= 1;
}
inline void pushdown (int x) {
    if (rev[x]) {
        if (ls(x)) pushrev (ls(x));
        if (rs(x)) pushrev (rs(x));
        rev[x] = 0;
    }
}
inline void rotate (int x) {
    int y = fa[x], z = fa[y], k = rs(y) == x;
    if (nroot (y)) ch[z][rs(z) == y] = x;
    ch[y][k] = ch[x][k ^ 1], ch[x][k ^ 1] = y;
    if (ch[y][k]) fa[ch[y][k]] = y;
    fa[x] = z, fa[y] = x;
    pushup (y), pushup (x);
}
int st[N], top;
inline void splay (int x) {
    int r = x;
    st[top = 1] = r;
    while (nroot (r)) st[++ top] = r = fa[r];
    while (top) pushdown (st[top --]);
    while (nroot (x)) {
        int y = fa[x], z = fa[y];
        if (nroot (y)) 
            rotate ((rs(y) == x) ^ (rs(z) == y) ? x : y);
        rotate (x);
    }
    pushup (x);
}
inline void access (int x) { 
    for (int y = 0; x; x = fa[y = x]) {
        splay (x);
        isum[x] += siz[rs(x)];
        isum[x] -= siz[rs(x) = y];
        pushup (x);
    }
}
inline void makeroot (int x) { access (x), splay (x), pushrev (x); }
inline void split (int x, int y) { makeroot (x), access (y), splay (y); }
inline void link (int x, int y) { split (x, y), isum[fa[x] = y] += siz[x], pushup (x); }

int main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int n, m, x, y; char op[2];
    cin >> n >> m;
    for (int i = 1; i <= n; i ++) siz[i] = 1;
    for (int i = 1; i <= m; i ++) {
        cin >> op >> x >> y;
        if (* op == 'A') link (x, y);
        else {
            split (x, y);
            cout << (ll)(isum[x] + 1) * (isum[y] + 1) << endl;
        }
    }
    return 0;
}

2. 题目链接:P3703 [SDOI2017]树点涂色

思路 L C T LCT LCT + 线段树 + 树链剖分解决,时间复杂度 O ( n l o g 2 n ) O(nlog^2n) O(nlog2n) 。 不得不说是目前见过的最复杂的数据结构 Q w Q QwQ QwQ 之一。首先题目所给的染色方式,最终会导致一条链上颜色一定是连续的一段段的出现,考虑到用 L C T LCT LCT 维护颜色相同的链,每一棵 s p l a y splay splay 维护的是原树上颜色相同的一段链,由于每次染色直接染到根,这相当于 a c c e s s access access 操作,那么每当有操作 1 1 1 时,就 a c c e s s ( x ) access(x) access(x) ,这样 L C T LCT LCT a c c e s s access access 操作就不能用在其它地方了,否则就会破坏维护的集合。 并且对于这棵 L C T LCT LCT ,每一个点的答案是它走到根节点的虚边数目 + 1 +1 +1(因为虚边代表颜色出现了变化)。

对于操作一来说,在 a c c e s s access access 的过程中,就是不断地改变虚边和实边,当一条虚边变成实边时,这条虚边连接的子树所有结点的答案 − 1 -1 1 ,因为它们经过的虚边少了一条,同理实边变成虚边,子树的答案 + 1 +1 +1 。这个可以用线段树 + + + 原树的 d f s dfs dfs 序维护, s p l a y splay splay 上的虚边和实边并不是原树的边,因此要维护一下每棵 s p l a y splay splay 的最左端点,方便找到一颗子树的根。

对于操作二来说,设 f [ x ] f[x] f[x] 表示 x x x 到根这个路径上的答案,那么答案为 f [ x ] + f [ y ] − 2 × f [ l c a ] + 1 f[x]+f[y]-2\times f[lca]+1 f[x]+f[y]2×f[lca]+1

对于操作三来说:用线段树维护区间最值。

需要注意的是,在一开始时,每个结点的颜色是不同的,因此每个结点的初始权值就是每个结点的深度。

#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define re register
#define endl '\n'

using namespace std;

const int MOD = 998244353;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double Pi = acos(-1.0);
const int N = 4e5 + 5;

#define ls(x) (ch[x][0])
#define rs(x) (ch[x][1])
#define lson (x << 1) 
#define rson (x << 1 | 1)

int f[N], dfn[N], dep[N], siz[N], son[N], top[N];
int mx[N], lazy[N], w[N], ch[N][2], fa[N], tot, n, m;
vector<int> out[N];

void build (int l, int r, int x = 1) {
    if (l == r) return void(mx[x] = w[l]);
    int mid = (l + r) >> 1;
    build (l, mid, lson), build (mid + 1, r, rson);
    mx[x] = max (mx[lson], mx[rson]);
}

inline void pushdown (int x) {
    if (lazy[x]) {
        mx[lson] += lazy[x], mx[rson] += lazy[x];
        lazy[lson] += lazy[x], lazy[rson] += lazy[x];
        lazy[x] = 0;
    }
}

void update (int l, int r, int c, int L, int R, int x = 1) {
    if (l <= L && R <= r) {
        mx[x] += c, lazy[x] += c;
        return ;
    }
    pushdown (x);
    int mid = (L + R) >> 1;
    if (l <= mid) update (l, r, c, L, mid, lson);
    if (r >  mid) update (l, r, c, mid + 1, R, rson);
    mx[x] = max (mx[lson], mx[rson]);
}

int query (int l, int r, int L, int R, int x = 1) {
    if (l <= L && R <= r) return mx[x];
    pushdown (x);
    int mid = (L + R) >> 1, res = 0;
    if (l <= mid) res = max (res, query (l, r, L, mid, lson));
    if (r >  mid) res = max (res, query (l, r, mid + 1, R, rson));
    return res;
}

void dfs1 (int u, int p) {
    f[u] = fa[u] = p;
    dep[u] = dep[p] + 1, siz[u] = 1;
    for (int v : out[u]) {
        if (v == p) continue;
        dfs1 (v, u);
        siz[u] += siz[v];
        if (siz[son[u]] < siz[v]) son[u] = v;
    }
}

void dfs2 (int u, int t) {
    dfn[u] = ++ tot, top[u] = t, w[dfn[u]] = dep[u];
    if (! son[u]) return ;
    dfs2 (son[u], t);
    for (int v : out[u]) {
        if (v == son[u] || v == f[u]) continue;
        dfs2 (v, v);
    }
}

int lca (int x, int y) {
    while (top[x] != top[y]) {
        if (dep[top[x]] < dep[top[y]]) swap (x, y);
        x = f[top[x]];
    }
    if (dep[x] > dep[y]) swap (x, y);
    return x;
}

bool nroot (int x) { return ls(fa[x])==x || rs(fa[x])==x; }

void rotate (int x) {
    int y = fa[x], z = fa[y], k = rs(y) == x;
    if (nroot (y)) ch[z][rs(z)==y] = x;
    ch[y][k] = ch[x][k ^ 1], ch[x][k ^ 1] = y;
    if (ch[y][k]) fa[ch[y][k]] = y;
    fa[x] = z, fa[y] = x;
}

void splay (int x) {
    while (nroot (x)) {
        int y = fa[x], z = fa[y];
        if (nroot (y)) 
            rotate ((rs(y) == x) ^ (rs(z) == y) ? x : y);
        rotate (x);
    }
}
int findroot (int x) {
    while (ls(x)) x = ls(x);
    return x;
}

void access (int x) {
    for (int y = 0; x; x = fa[y = x]) {
        splay (x);
        if (rs(x)) {
            int p = findroot (rs(x));
            update (dfn[p], dfn[p] + siz[p] - 1, 1, 1, n);
        }
        rs(x) = y;
        if (rs(x)) {
            int p = findroot (rs(x));
            update (dfn[p], dfn[p] + siz[p] - 1, -1, 1, n);
        }
    }
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for (int i = 1, u, v; i < n; i ++) {
        cin >> u >> v;
        out[u].push_back (v);
        out[v].push_back (u);
    }
    dfs1 (1, 0), dfs2 (1, 1), build (1, n);
    for (int i = 1, op, x, y; i <= m; i ++) {
        cin >> op >> x;
        if (op == 1) access (x);
        if (op == 2) {
            cin >> y;
            int lc = lca (x, y);
            int a = query (dfn[x], dfn[x], 1, n);
            int b = query (dfn[y], dfn[y], 1, n);
            int c = query (dfn[lc], dfn[lc], 1, n);
            cout << a + b - 2 * c + 1 << endl;
        }
        if (op == 3) cout << query (dfn[x], dfn[x] + siz[x] - 1, 1, n) << endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值