51nod2004 终结之时 (支配树+树剖+树链的并)
我永远喜欢洛天依
给定一张图世末积雨云,你需要维护其支配树:
单点修改,子树修改,树链修改
子树求和,树链求和,多条树链的并集求和
撤销之前的操作
可以先用 Lengauer-Tarjan 算法 求出图的支配树,然后把它树剖,开一个线段树维护
至于多条树链的并集求和(有点像虚树的那种操作),现将所有点按照 dfn 排序,然后按照 dfn 的顺序依次加点
枚举节点a[i],每次假装a[1..i]的答案中除了a[i]到根的路径没有求值之外其它的都求值了,那么转移的时候需要加上当前点到lca的距离。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAAB4CAYAAAB1ovlvAAAPWklEQVR42u1df2xeVRke2dYNlpaOMe02WLsNq2A0Ff9oiXFWJrYDazqpsCJzDahbnIWJYWQ6UoiaJppGFMhwko1MWYSJc0A2AiQIRg1h0hD/UNEMiMjiHO02N7qZzGPfb55y7vudc+45956f97t/vFm7fr293/M993l/nPc9ZxohZFpppfmyEoTSSgKWVhKwtNJKApZWErC00koCllYSMErbv38/aWlpmXxX0yr/bt26lZQfctgYFgowng0PD5ckDBjD6IFrbGwUAgc2e/ZssmPHjpKEgWIYNXhNTU1S4FiDJ7wkYngYxh3AMuCsX7+eDA0NVQxchuhJdhEXvvjSUXLrpr+Q/U8fISWGNUJAChxrIhBtx4Wr+l8hn+g+QK767MukxLCGCSgCEZ5im/cF5KNWYlgSsGITExMJAG3GgyoEBDd9/do/Tr0Ovua5bPy6tNfHhmFhCAhgpAEIT60LFVQhIHXTrGGXDeS7pm+06nWi18eIYdQExGCkgYfdiC8C7n3ysJBUauR7yZiL941h1ATEYMD3aQDaJiAmV5r63T18sKJk+PVYIUfufWPqZytXnSXmJ6/5A4kdw+jrWBs3buTWqnwREJMrTSFPnz5DHn7kUIJkoH4i8tlIcnxiGD0BITBm3QgLItSrXBMQkyuvQvJeY5qAPjEsRDVfVKtKM1fxH1uY1lVIrH62yjy+MCzUshIL4orZ55HH5y8i/XMavBEQCMcmE2ysJ1JIGc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AJlVWF0PfLff4YbKFodK+erKeL1UChjiDm1bLMrk3oS6GPlvuMT4yLPJimNtNpREQ70xV9E3DTWHooxGBttw7TXx0N/zWJSDvGjEcn2Cy/JMFQxeGy2M+OpmMPdGs3KYVMWtJCbNi6Pp+fGXf1utJIiUIpW2/NM9Nw7YLviXIpcnsfzIIb8YQf2laAAAAAElFTkSuQmCC" alt="">
如图,我们从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\n", query(1, 1, n, dfn[u], dfn[u] + weight[u] - 1));
if (opd == 2) printf("%lld\n", 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\n", 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;
}
51nod2004 终结之时 (支配树+树剖+树链的并)的更多相关文章
- [CTSC2008]网络管理(整体二分+树剖+树状数组)
一道经典的带修改树链第 \(k\) 大的问题. 我只想出三个 \(\log\) 的解法... 整体二分+树剖+树状数组. 那不是暴力随便踩的吗??? 不过跑得挺快的. \(Code\ Below:\) ...
- 【51nod 2004】终结之时
题目大意 "将世界终结前最后的画面,深深刻印进死水般的心海." 祈愿没有得到回应,雷声冲破云霄,正在祈愿的洛天依受到了极大的打击. 洛天依叹了口气,说:"看来这个世界正如 ...
- 浅谈算法和数据结构: 十 平衡查找树之B树
前面讲解了平衡查找树中的2-3树以及其实现红黑树.2-3树种,一个节点最多有2个key,而红黑树则使用染色的方式来标识这两个key. 维基百科对B树的定义为“在计算机科学中,B树(B-tree)是一种 ...
- B树、B+树的实现
B树的定义 假设B树的度为t(t>=2),则B树满足如下要求:(参考算法导论) (1) 每个非根节点至少包含t-1个关键字,t个指向子节点的指针:至多包含2t-1个关键字,2t个指向子女的指针 ...
- B树、B-树、B+树、B*树
B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right) 2.所有结点存储一个关键字 3.非叶子节点的左指针指向小于其关键字的字数,右指针指向大于其关键字的字数: 如: B树的 ...
- 人人都是 DBA(VII)B 树和 B+ 树
B 树(B-Tree)是为磁盘等辅助存取设备设计的一种平衡查找树,它实现了以 O(log n) 时间复杂度执行查找.顺序读取.插入和删除操作.由于 B 树和 B 树的变种在降低磁盘 I/O 操作次数方 ...
- (转)B-树、B+树、B*树
B-树 是一种多路搜索树(并不是二叉的): 1.定义任意非叶子结点最多只有M个儿子:且M>2: 2.根结点的儿子数为[2, M]: 3.除根结点以外的非叶子结点的儿子数为[M/2, M]: 4. ...
- 从B 树、B+ 树、B* 树谈到R 树
从B 树.B+ 树.B* 树谈到R 树 作者:July.weedge.Frankie.编程艺术室出品. 说明:本文从B树开始谈起,然后论述B+树.B*树,最后谈到R 树.其中B树.B+树及B*树部分由 ...
- B树和B+树
当数据量大时,我们如果用二叉树来存储的会导致树的高度太高,从而造成磁盘IO过于频繁,进而导致查询效率下降.因此采用B树来解决大数据存储的问题,很多数据库中都是采用B树或者B+树来进行存储的.其目的就是 ...
随机推荐
- ”$-”与shell默认选项
一.前言 之所以整理这篇博客,主要是写Linux环境设置文件 的时候,在查看/etc/profile时看到这么一段代码: for i in /etc/profile.d/*.sh ; do if [ ...
- python-nmap模块常用方法说明
一.模块常用方法说明 本节介绍python-nmap模块的两个常用类,一个为PortScanner()类,实现一个nmap工具的端口扫描功能封装:另一个为PortScannerHostDict()类, ...
- VMware安装完后,没有虚拟网卡
1 问题描述: 1.1 windows10首次安装VMware,或者非首次安装VMware时,安装后,没有出现如下图所示的虚拟网卡: 1.2 Xshell或者SecureCRT 或者editplus等 ...
- Nginx概述、安装及配置详解
nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外 ...
- [C++ Mind Map] class and memory
class and memory
- code3728 联合权值
一开始暴搜,超时3个点... 后来看了题解: 首先,两个点的距离为2当且仅当它们都与一个点直接相连 反过来说,一个点所有的出边的终点都是互相距离2的(最大值可以依靠这个方法,前向星处理的时候将每个点的 ...
- DeepLearning 学习资料
1 sotfmax 函数: stanford UFLDL: http://deeplearning.stanford.edu/wiki/index.php/Softmax%E5%9B%9E%E5%BD ...
- centos7.0查看有没有运行mysql
centos7自带是mariadb,一个mysql的变种. 查看有没有安装过:yum list installed mysql*rpm -qa | grep mysql* 安装mysql软件(mari ...
- java IO类简单介绍
一.流的概念 流是字节序列的抽象概念.流和文件的差别:文件是数据的静态存储形式,而流是指数据传输时的形态.文件只是流的操作对象之一.流按其操作的对象不同可以分为文件流.网络流.内存流.磁带流等.Jav ...
- 编写高质量代码改善C#程序的157个建议——建议144:一个方法只做一件事
建议144:一个方法只做一件事 “单一职责原则”(SRP)要求每一个类型只负责一件事情.我们将此概念扩展到方法上,就变成了:一个方法只做一件事. 回顾上一建议的代码,LocalInit和RemoteI ...