• 51nod2004 终结之时 (支配树+树剖+树链的并)


    link

    我永远喜欢洛天依

    给定一张图世末积雨云,你需要维护其支配树:

    单点修改,子树修改,树链修改

    子树求和,树链求和,多条树链的并集求和

    撤销之前的操作

    可以先用 Lengauer-Tarjan 算法 求出图的支配树,然后把它树剖,开一个线段树维护

    至于多条树链的并集求和(有点像虚树的那种操作),现将所有点按照 dfn 排序,然后按照 dfn 的顺序依次加点

    枚举节点a[i],每次假装a[1..i]的答案中除了a[i]到根的路径没有求值之外其它的都求值了,那么转移的时候需要加上当前点到lca的距离。

    如图,我们从1转移到2时候,要加上绿色的链这一段的值

    最后转移完后,我们再加上最后一个点到根的距离即可

    至于撤销操作,我们可以考虑建立主席树,当然也可以不建主席树暴力撤销,因为每一步操作最多只会被撤销一次。

    代码:

    #include <cstdio>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int n, m;
    
    namespace dominate_tree
    {
    	vector<int> g[50010], g1[50010], g2[50010];
    	int sdom[50010], bel[50010], val[50010], idom[50010];
    	int dfn[50010], dfntot, id[50010], fa[50010];
    
    	void dfs(int x)
    	{
    		dfn[x] = ++dfntot, id[dfntot] = x;
    		for (int i : g[x]) if (dfn[i] == 0) fa[i] = x, dfs(i);
    	}
    
    	int getf(int x)
    	{
    		if (x == bel[x]) return x;
    		int rt = getf(bel[x]);
    		if (dfn[sdom[val[bel[x]]]] < dfn[sdom[val[x]]])
    			val[x] = val[bel[x]];
    		return bel[x] = rt;
    	}
    	
    	void work()
    	{
    		for (int i = 1; i <= n; i++) sdom[i] = bel[i] = val[i] = i;
    		dfs(1);
    		for (int i = n; i >= 2; i--)
    		{
    			int x = id[i];
    			for (int j : g1[x])
    				if (dfn[j])
    				{
    					getf(j);
    					if (dfn[sdom[val[j]]] < dfn[sdom[x]])
    						sdom[x] = sdom[val[j]];
    				}
    			g2[sdom[x]].push_back(x);
    			x = bel[x] = fa[x];
    			for (int j : g2[x])
    			{
    				getf(j);
    				if (sdom[val[j]] == x) idom[j] = x;
    				else idom[j] = val[j];
    			}
    		}
    		for (int i = 2; i <= n; i++)
    		{
    			int x = id[i];
    			if (idom[x] != sdom[x]) idom[x] = idom[idom[x]];
    		}
    	}
    }
    
    vector<int> out[50010];
    int fa[50010], depth[50010], wson[50010], weight[50010];
    int dfn[50010], top[50010], tot, val[50010], stop, tmp[50010];
    long long tree[200010], lazy[200010];
    struct ch { int type, u, w; } s[100010];
    
    void pushdown(int x, int cl, int mid, int cr)
    {
    	lazy[x * 2] += lazy[x], lazy[x * 2 + 1] += lazy[x];
    	tree[x * 2] += lazy[x] * (mid - cl + 1);
    	tree[x * 2 + 1] += lazy[x] * (cr - mid);
    	lazy[x] = 0;
    }
    
    void chenge(int x, int cl, int cr, int L, int R, int k)
    {
    	if (cr < L || R < cl) return;
    	if (L <= cl && cr <= R) { lazy[x] += k, tree[x] += (cr - cl + 1) * (long long)k; return; }
    	int mid = (cl + cr) / 2;
    	pushdown(x, cl, mid, cr);
    	chenge(x * 2, cl, mid, L, R, k), chenge(x * 2 + 1, mid + 1, cr, L, R, k);
    	tree[x] = tree[x * 2] + tree[x * 2 + 1];
    }
    
    long long query(int x, int cl, int cr, int L, int R)
    {
    	if (cr < L || R < cl) return 0;
    	if (L <= cl && cr <= R) return tree[x];
    	int mid = (cl + cr) / 2;
    	pushdown(x, cl, mid, cr);
    	return query(x * 2, cl, mid, L, R) + query(x * 2 + 1, mid + 1, cr, L, R);
    }
    
    void dfs1(int x)
    {
    	wson[x] = -1, weight[x] = 1;
    	for (int i : out[x])
    	{
    		fa[i] = x, depth[i] = depth[x] + 1, dfs1(i), weight[x] += weight[i];
    		if (wson[x] == -1 || weight[i] > weight[wson[x]]) wson[x] = i;
    	}
    }
    
    void dfs2(int x, int topf)
    {
    	dfn[x] = ++tot, top[x] = topf;
    	if (wson[x] != -1)
    	{
    		dfs2(wson[x], topf);
    		for (int i : out[x]) if (i != wson[x]) dfs2(i, i);
    	}
    }
    
    int lca(int x, int y)
    {
    	while (top[x] != top[y])
    	{
    		if (depth[top[x]] < depth[top[y]]) swap(x, y);
    		x = fa[top[x]];
    	}
    	if (depth[x] > depth[y]) swap(x, y);
    	return x;
    }
    
    long long getans(int x)
    {
    	long long res = 0;
    	while (x)
    		res += query(1, 1, n, dfn[top[x]], dfn[x]), x = fa[top[x]];
    	return res;
    }
    
    int main()
    {
    	scanf("%d%d", &n, &m);
    	for (int i = 1; i <= n; i++) scanf("%d", &val[i]);
    	for (int x, y, i = 1; i <= m; i++)
    		scanf("%d%d", &x, &y), dominate_tree::g[x].push_back(y), dominate_tree::g1[y].push_back(x);
    	dominate_tree::work();
    	for (int i = 2; i <= n; i++) out[dominate_tree::idom[i]].push_back(i);
    	dfs1(1), dfs2(1, 1);
    	for (int i = 1; i <= n; i++) chenge(1, 1, n, dfn[i], dfn[i], val[i]);
    	int q; scanf("%d", &q);
    	for (int i = 1; i <= q; i++)
    	{
    		char tp;
    		scanf(" %c", &tp);
    		if (tp == 'C')
    		{
    			int opd, u, w;
    			scanf("%d%d%d", &opd, &u, &w);
    			s[++stop] = (ch){opd, u, w};
    			if (opd == 1) chenge(1, 1, n, dfn[u], dfn[u], w);
    			if (opd == 2) chenge(1, 1, n, dfn[u], dfn[u] + weight[u] - 1, w);
    			if (opd == 3)
    				while (u) chenge(1, 1, n, dfn[top[u]], dfn[u], w), u = fa[top[u]];
    		}
    		else if (tp == 'Q')
    		{
    			int opd, u;
    			scanf("%d%d", &opd, &u);
    			if (opd == 1) printf("%lld
    ", query(1, 1, n, dfn[u], dfn[u] + weight[u] - 1));
    			if (opd == 2) printf("%lld
    ", getans(u));
    			if (opd == 3)
    			{
    				long long res = 0;
    				for (int j = 1; j <= u; j++) scanf("%d", &tmp[j]);
    				sort(tmp + 1, tmp + 1 + u, [](int x, int y) { return dfn[x] < dfn[y]; });
    				for (int j = 2; j <= u; j++)
    				{
    					int lc = lca(tmp[j - 1], tmp[j]);
    					if (lc != tmp[j - 1])
    						res += getans(tmp[j - 1]) - getans(lc);
    				}
    				res += getans(tmp[u]);
    				printf("%lld
    ", res);
    			}
    		}
    		else
    		{
    			int k;
    			scanf("%d", &k);
    			while (stop > 0 && k > 0)
    			{
    				int opd = s[stop].type, u = s[stop].u, w = -s[stop].w;
    				if (opd == 1) chenge(1, 1, n, dfn[u], dfn[u], w);
    				if (opd == 2) chenge(1, 1, n, dfn[u], dfn[u] + weight[u] - 1, w);
    				if (opd == 3)
    					while (u) chenge(1, 1, n, dfn[top[u]], dfn[u], w), u = fa[top[u]];
    				stop--, k--;
    			}
    		}
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    学习鸟哥linux私房菜--安装中文输入法fcitx
    学习鸟哥linux私房菜--安装centos5.6(u盘安装,中文乱码)
    CSS
    vue-cli脚手架搭建项目及Axios封装
    前端面试题套路
    移动端touch事件
    import和require的区别
    接口封装
    js 数组操作
    vue 小记
  • 原文地址:https://www.cnblogs.com/oier/p/10556305.html
Copyright © 2020-2023  润新知