• bzoj4530 [Bjoi2014]大融合 子树信息 LCT


    题目传送门

    https://lydsy.com/JudgeOnline/problem.php?id=4530/

    题解

    想要求出一条边的负载那么就是要求出一个点为根的时候的另一个点的子树大小。

    又因为要动态加边,所以只能用 LCT。

    (突然想起来因为没有强制在线是不是可以把树先建起来然后跑以时间为权值的主席树?

    但是 LCT 只能维护链上的信息,怎么维护子树大小呢?

    所以就来搞一个维护子树信息的 LCT。

    LCT 上每个点开一个变量 (sum) 表示这个点的所有轻儿子的子树大小之和,(siz) 表示这个点在 Splay 上的子树的权值和。

    (很像 ddp 呢

    然后 access 的时候,更换重儿子的时候,需要先断掉旧的重儿子,连上新的,所以需要先减去旧重儿子的权值,加上新重儿子的权值。


    时间复杂度就是纯 LCT 吧,(O(qlog n))

    #include<bits/stdc++.h>
    
    #define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
    #define dbg(...) fprintf(stderr, __VA_ARGS__)
    #define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
    #define fi first
    #define se second
    #define pb push_back
    
    template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
    template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
    
    typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
    
    template<typename I> inline void read(I &x) {
    	int f = 0, c;
    	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    	x = c & 15;
    	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    	f ? x = -x : 0;
    }
    
    const int N = 100000 + 7;
    
    #define lc c[0]
    #define rc c[1]
    
    int n, m;
    
    struct Node { int c[2], s, sum, siz, fa, rev; } t[N];
    int st[N];
    inline bool idtfy(int o) { return t[t[o].fa].rc == o; }
    inline bool isroot(int o) { return t[t[o].fa].lc != o && t[t[o].fa].rc != o; }
    inline void connect(int fa, int o, int d) { t[fa].c[d] = o, t[o].fa = fa; }
    inline void pushup(int o) {
    	t[o].s = t[t[o].lc].s + t[t[o].rc].s + 1;
    	t[o].siz = t[t[o].lc].siz + t[t[o].rc].siz + t[o].sum + 1;
    }
    inline void pushdown(int o) {
    	if (!t[o].rev) return;
    	if (t[o].lc) t[t[o].lc].rev ^= 1, std::swap(t[t[o].lc].lc, t[t[o].lc].rc);
    	if (t[o].rc) t[t[o].rc].rev ^= 1, std::swap(t[t[o].rc].lc, t[t[o].rc].rc);
    	t[o].rev = 0;
    }
    inline void rotate(int o) {
    	int fa = t[o].fa, pa = t[fa].fa, d1 = idtfy(o), d2 = idtfy(fa), b = t[o].c[d1 ^ 1];
    	if (!isroot(fa)) t[pa].c[d2] = o; t[o].fa = pa;
    	connect(o, fa, d1 ^ 1), connect(fa, b, d1);
    	pushup(fa), pushup(o);
    }
    inline void splay(int o) {
    	int x = o, tp = 0;
    	st[++tp] = x;
    	while (!isroot(x)) st[++tp] = x = t[x].fa;
    	while (tp) pushdown(st[tp--]);
    	while (!isroot(o)) {
    		int fa = t[o].fa;
    		if (isroot(fa)) rotate(o);
    		else if (idtfy(o) == idtfy(fa)) rotate(fa), rotate(o);
    		else rotate(o), rotate(o);
    	}
    }
    inline void access(int o)  {
    	for (int x = 0; o; o = t[x = o].fa) {
    		splay(o);
    		t[o].sum += t[t[o].rc].siz;
    		t[o].sum -= t[x].siz;
    		t[o].rc = x;
    		pushup(o);
    	}
    }
    inline void mkrt(int x) {
    	access(x), splay(x);
    	t[x].rev ^= 1, std::swap(t[x].lc, t[x].rc);
    }
    inline int getrt(int x) {
    	access(x), splay(x);
    	while (pushdown(x), t[x].lc) x = t[x].lc;
    	splay(x);
    	return x;
    }
    inline void link(int  x, int y) {
    	mkrt(x);
    	if (getrt(y) != x) {
    		access(y), splay(y);
    		t[x].fa = y, t[y].sum += t[x].siz, pushup(y);
    	}
    }
    
    inline void work() {
    	while (m--) {
    		char opt[5];
    		int x, y;
    		scanf("%s", opt);
    		read(x), read(y);
    		if (*opt == 'Q') mkrt(x), access(y), splay(x), assert(t[x].rc == y), assert(t[x].siz > t[y].siz), printf("%lld
    ", (ll)t[y].siz * (t[x].siz - t[y].siz));
    		else link(x, y);
    	}
    }
    
    inline void init() {
    	read(n), read(m);
    }
    
    int main() {
    #ifdef hzhkk
    	freopen("hkk.in", "r", stdin);
    #endif
    	init();
    	work();
    	fclose(stdin), fclose(stdout);
    	return 0;
    }
    
  • 相关阅读:
    《Real Time 3D Terrain Engines Using C++ And DirectX9》(实时地形引擎)随书源码
    (转)ogreUML类图
    (转)导弹跟踪算法
    转:正则表达式语法
    读取数据
    Python lambda用法及其与def的区别
    转Vb6.0安装失败解决办法,完全卸载VB(提高班里win7没装上VB的可以看看,我实验成功了)
    vb imagelist 作用
    二叉树的实现
    转:Python语言编程学习资料(电子书+视频教程)下载汇总:
  • 原文地址:https://www.cnblogs.com/hankeke/p/bzoj4530.html
Copyright © 2020-2023  润新知