倍增
倍增我是真滴不会
倍增法(英语:binary lifting),顾名思义就是翻倍。
能够使线性的处理转化为对数级的处理,大大地优化时间复杂度。
(ps:上次学倍增LCA,没学会,老老实实为了严格次小生成树滚回来重新学)
RMQ_QWQ
(n~log(n)~)的预处理与(O~(1))的查询
- 设(f_{i,j})表示区间([i,i+2^j - 1])的最大值
- 一开始(f_{i,0}=a_i) ((2^0 -1 = 0) (f_{i,0})的区间为([i,i]))
- 转移方程 ;[f_{i,~j} = max(f_{i,~j-1}, f_{i+2^{j-1},~~j-1}) ]
感性理解一下
对于每次询问([l,~r])
- (r = l + 2^x- 1)
- (x = log_2~ (r - l + 1))
- (ans = max(f_{l,~l + 2^x-1},f_{r - 2^x + 1,~r}))(此处的表达不是很准确,其实表达应该为)[f(l,r) = max(f_{l,~l + 2^x-1},f_{r - 2^x + 1,~r}) ]将这个(f(l,r))理解为一个函数,可能就不会有太大的歧义了
Q:这里为什么不能直接用(f_{i,~x}) 呢?
A:因为我们这里的(log)是向下取整的,可能会出现有一块取不到的部分
Q:那有重复的部分怎么办呐??
A:重复部分对答案的贡献有影响吗?
Q:貌似莫得影响
A:
(ans = max(f_{l,~x}, f_{r - 2^x + 1,~x}))
完事
注意点
-
输入输出数据一般很多,建议开启输入输出优化。
-
每次用 std::log 重新计算 log 函数值并不值得,建议进行如下的预处理:
第二个式子是这样推导出来的
code
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#define ll long long
using namespace std;
const int logn = 22;
const int N = 2000001;
int read() {
int s = 0, f = 0;
char ch = getchar();
while (!isdigit(ch))
f |= (ch == '-'), ch = getchar();
while (isdigit(ch))
s = s * 10 + (ch ^ 48), ch = getchar();
return f ? -s : s;
}
int Log[N];
int n ,m;
void pre() {
Log[1] = 0; Log[2] = 1;
for (int i = 3; i <= n; ++i)
Log[i] = Log[i / 2] + 1;
}
int f[N][25];
int main() {
n = read(), m = read();
for (int i = 1; i <= n; ++i) f[i][0] = read();
pre();
for (int j = 1; j <= logn; ++j)
for (int i = 1; i + (1 << j) - 1 <= n; i++)
f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
for (int i = 1; i <= m; i++) {
int l = read(), r = read();
int log_x = Log[r - l + 1];
printf("%d
", max(f[l][log_x], f[r - (1 << log_x) + 1][log_x]));
}
system("pause");
return 0;
}
倍增求LCA
- 核心的思想就是每次找祖先的时候多跳几个以保证时间复杂度的优秀
先说朴素的算法
可以每次找深度比较大的那个点,让它向上跳。显然在树上,这两个点最后一定会相遇,相遇的位置就是想要求的 LCA。 或者先向上调整深度较大的点,令他们深度相同,然后再共同向上跳转,最后也一定会相遇.(摘自OI Wiki)
精简版本:
就是从深度深的向上跳,到达同一深度后一起向上跳(学过树剖的都知道吧,艹估计没人跟我一样先学的树剖,然后回来学倍增)
倍增就很优秀了
- 本质是朴素算法的改进算法。通过预处理(f)数组,可以使指针快速的移动(f_{x,i}) 表示点(x)的第(2^i)个祖先。这个过程需要通过(dfs) 预处理出来。
dfs
void dfs(int x, int fa) {
dep[x] = dep[fa] + 1;
f[x][0] = fa;
for (int i = 1; (1 << i) <= dep[x]; i++)
f[x][i] = f[f[x][i - 1]][i - 1];
for (int i = head[x]; i; i = e[i].next) {
int to = e[i].to;
if (to != fa)
dfs(to, x);
}
}
这一块可以解释一下,假如说求的是最下面两个节点,很明显两个点的LCA是紫线所指向的点,问题是怎么找,( ext{for}) 循环从大的向小遍历,可以发现先从(红线 o 粉线 o 蓝线 o)到了蓝线所指的点,会向上跳一次到了紫线就发现了LCA
( ext {return f[x][0]}) 就是答案
这种是较为朴素倍增的求法,没有用(log)优化
加入log优化的(但好像没太大的常数优化)
Log数组还是原来的求法
void dfs(int x, int fa) {
f[x][0] = fa;
dep[x] = dep[fa] + 1;
for (int i = 1; i <=Log[dep[x]]; ++i)
f[x][i] =f[f[x][i - 1]][i - 1];
for (int i = head[x]; i; i = e[i].net)
if (e[i].to != fa)
dfs(e[i].to, x);
}
有一说一,我不大喜欢这种优化的方法我最喜欢的还是树剖
- 裸的倍增LCA,这种写法确实好理解
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 5e5 + 10;
struct tree {
int from, to, next;
} e[N << 1];
int nume, head[N];
void add_edge(int from, int to) {
e[++nume].from = from;
e[nume].to = to;
e[nume].next = head[from];
head[from] = nume;
}
int dep[N], f[N][21];
void dfs(int u, int fa) {
dep[u] = dep[fa] + 1;
f[u][0] = fa;
for (int i = 1; (1 << i) <= dep[u]; i++)
f[u][i] = f[f[u][i - 1]][i - 1];
for (int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if (to != fa)
dfs(to, u);
}
}
int lca(int x, int y) {
if (dep[x] > dep[y])
swap(x, y);
for (int i = 20; i >= 0; i--) {
if (dep[x] <= dep[y] - (1 << i))
y = f[y][i];
}
if (x == y)
return x;
for (int i = 20; i >= 0; i--) {
if (f[x][i] == f[y][i])
continue;
x = f[x][i], y = f[y][i];
}
return f[x][0];
}
int main() {
int n, m, s;
scanf("%d%d%d", &n, &m, &s);
for (int i = 1, x, y; i < n; i++) {
scanf("%d%d", &x, &y);
add_edge(x, y);
add_edge(y, x);
}
dfs(s, 0);
for (int i = 1, x, y; i <= m; i++) {
scanf("%d%d", &x, &y);
printf("%d
", lca(x, y));
}
}
- 用(Log)的求法
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#define ll long long
using namespace std;
const int N = 5e5 + 10;
int read() {
int s = 0, f = 0;
char ch = getchar();
while (!isdigit(ch))
f |= (ch == '-'), ch = getchar();
while (isdigit(ch))
s = s * 10 + (ch ^ 48), ch = getchar();
return f ? -s : s;
}
struct Edge {
int from, to, net;
} e[N << 1];
int head[N], nume;
void add_edge(int from, int to) {
e[++nume].from = from, e[nume].to = to, e[nume].net = head[from],
head[from] = nume;
}
int f[N][25], Log[N], dep[N];
void dfs(int x, int fa) {
f[x][0] = fa, dep[x] = dep[fa] + 1;
for (int i = 1; i <= Log[dep[x]]; i++)
f[x][i] = f[f[x][i - 1]][i - 1];
for (int i = head[x]; i; i = e[i].net) {
int to = e[i].to;
if (to == fa)
continue;
dfs(to, x);
}
}
int lca(int x, int y) {
if (dep[x] < dep[y])
swap(x, y);
while (dep[x] > dep[y])
x = f[x][Log[dep[x] - dep[y]]];
if (x == y)
return x;
for (int i = Log[dep[x]] ; i >= 0; i--) {
if (f[x][i] != f[y][i])
x = f[x][i], y = f[y][i];
}
return f[x][0];
}
int main() {
int n = read(), m = read(), s = read();
for (int i = 1, u, v; i < n; i++) {
u = read(), v = read();
add_edge(u, v), add_edge(v, u);
}
Log[1] = 0;
for (int i = 2; i <= n; i++)
Log[i] = Log[i / 2] + 1;
dfs(s, 0);
for (int i = 1; i <= m; i++) {
int x = read(), y = read();
printf("%d
", lca(x, y));
}
system("pause");
return 0;
}
嗷嗷嗷,有没有人喜欢树剖呢
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 5e5 + 10;
int head[N], nume;
struct node {
int from, to, next;
} e[N << 1];
void add_edge(int from, int to) {
e[++nume].from = from;
e[nume].to = to;
e[nume].next = head[from];
head[from] = nume;
}
int fath[N], siz[N], dep[N], son[N];
void dfs(int x, int fa) {
siz[x] = 1;
fath[x] = fa, dep[x] = dep[fa] + 1;
for (int i = head[x]; i; i = e[i].next) {
int to = e[i].to;
if (to == fa)
continue;
dfs(to, x), siz[x] += siz[to];
if (siz[son[x]] < siz[to])
son[x] = to;
}
}
int dfn[N], pre[N], top[N], cnt;
void dfs2(int x, int tp) {
dfn[x] = ++cnt, pre[cnt] = x, top[x] = tp;
if (son[x])
dfs2(son[x], tp);
for (int i = head[x]; i; i = e[i].next) {
int to = e[i].to;
if (to == fath[x] || to == son[x])
continue;
dfs2(to, to);
}
}
int lca(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]])
y = fath[top[y]];
else
x = fath[top[x]];
}
if (dep[x] < dep[y])
swap(x, y);
return y;
}
int n, m, s;
int main() {
scanf("%d%d%d", &n, &m, &s);
int u, v;
for (int i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
add_edge(u, v), add_edge(v, u);
}
dfs(s, 0), dfs2(s, s);
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
printf("%d
", lca(x, y));
}
return 0;
}