省流:单点加,前缀和,树状数组两倍速度。
核心原理
将下标 拆成 进制 ,相当于把区间 拆成 个长度为 的区间。考虑一棵 叉线段树,自下向上第 层的节点管辖长度为 的区间,节点上有一个长度为 的序列 ,其中 表示:这个长度为 的区间中,前 个长度为 的小区间的所有元素之和。根据定义有 。
将第 层内所有节点的 序列从左到右拼接,得到这一层的序列 。它的下标是层内下标,不是原序列下标:原下标 落在 。
设 ,则 是所有满足下列条件的 之和:把 与 写成 进制后,高于第 位的各位与 相同,第 位严格小于 的第 位,更低位任意。
下面是一个 的例子,最下一行为原序列 :
[00, 10, 36, 78 ] S(1)
[00, 01, 03, 06, 00, 05, 11, 18, 00, 09, 19, 30, 00, 13, 27, 42] S(0)
[01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16] a查询 就是求 。每个 都有唯一的最高位 ,使得 与 在高于 的各位上相同,而 的第 位严格更小,更低位任意。
这些 的 之和就是 。 取遍 时,这些集合互不相交,并且覆盖全部 ,所以 的和为:
例如 ,则 。于是求和的时间复杂度为 。
接下来考虑修改 。第 层的 含有 ,当且仅当 与 位于同一个节点,且 。因此每一层只有一个节点会被改到:令 ,节点起点为 ,对节点内下标 执行 即可。于是修改的时间复杂度为 。
注意到修改操作中每一层的修改形如”将连续 个元素进行增加”,这启发我们进行向量化。预处理掩码 ,将 广播之后与 按位与再加到节点上即可。
接下来考虑 的选取。假设我们维护的是 位整数,使用 AVX2 指令集的 位向量,为了方便进行向量化,首先应当有 。同时内存读取时总会以缓存行为最小单位进行取出,缓存行的大小为 字节,那我们刚好可以取 ,这样一个节点刚好为一个缓存行,同时修改操作可以拆成两次指令。
下面的代码给出一个示例,实现了 -indexed 单点加,求左闭右开前缀和。冷知识:下面这个代码不依赖头文件。
示例代码
template <int N>
class WideSGT {
using Vector = int __attribute__((vector_size(32)));
static constexpr int Cacheline = 64;
static constexpr int Block = Cacheline / sizeof(int);
static constexpr int Width = __builtin_ctz(Block);
struct Mask {
alignas(Cacheline) int mask[Block][Block];
constexpr Mask() : mask{} {
for (int u = 0; u < Block; ++u)
for (int v = 0; v < Block; ++v)
mask[u][v] = u < v ? -1 : 0;
}
};
static constexpr int height(int i) {
int result = 1, n = i;
while (n >= Block) {
n >>= Width;
++result;
}
return result;
}
static constexpr int offset(int i) {
int result = 0, n = N;
while (i--) {
n = (n + ~-Block) / Block;
result += n * Block;
}
return result;
}
static constexpr int Size = N;
static constexpr int Height = height(Size);
alignas(Cacheline) int data[static_cast<unsigned>(offset(Height))];
static constexpr Mask mask{};
public:
int sum(int position) {
int result = 0;
for (int i = 0; i < Height; ++i) {
result += data[offset(i) + position];
position >>= Width;
}
return result;
}
void modify(int position, int delta) {
auto vector = Vector{} + delta;
for (int i = 0; i < Height; ++i) {
auto block = (Vector*)&data[offset(i) + (position & -Block)];
auto vmask = (Vector*)&mask.mask[position & ~-Block];
block[0] += vector & vmask[0];
block[1] += vector & vmask[1];
position >>= Width;
}
}
};效率测试
- 评测机:
Intel(R) Core(TM) Ultra 9 285H - 编译器:
g++ (x86_64-win32-seh-rev0, Built by MinGW-Builds project) 15.2.0 - 编译参数:
-std=c++23 -march=native -mtune=native -Ofast
序列长度 , 次随机下标修改, 次随机下标求和。 次测试平均。
WideSGT:Fenwick:
两倍速度。伟大无需多言。
测试代码
template <int N>
class WideSGT {
using Vector = int __attribute__((vector_size(32)));
static constexpr int Cacheline = 64;
static constexpr int Block = Cacheline / sizeof(int);
static constexpr int Width = __builtin_ctz(Block);
struct Mask {
alignas(Cacheline) int mask[Block][Block];
constexpr Mask() : mask{} {
for (int u = 0; u < Block; ++u)
for (int v = 0; v < Block; ++v)
mask[u][v] = u < v ? -1 : 0;
}
};
static constexpr int height(int i) {
int result = 1, n = i;
while (n >= Block) {
n >>= Width;
++result;
}
return result;
}
static constexpr int offset(int i) {
int result = 0, n = N;
while (i--) {
n = (n + ~-Block) / Block;
result += n * Block;
}
return result;
}
static constexpr int Size = N;
static constexpr int Height = height(Size);
alignas(Cacheline) int data[static_cast<unsigned>(offset(Height))];
static constexpr Mask mask{};
public:
int sum(int position) {
int result = 0;
for (int i = 0; i < Height; ++i) {
result += data[offset(i) + position];
position >>= Width;
}
return result;
}
void modify(int position, int delta) {
auto vector = Vector{} + delta;
for (int i = 0; i < Height; ++i) {
auto block = (Vector*)&data[offset(i) + (position & -Block)];
auto vmask = (Vector*)&mask.mask[position & ~-Block];
block[0] += vector & vmask[0];
block[1] += vector & vmask[1];
position >>= Width;
}
}
};
template <int N>
class Fenwick {
int data[static_cast<unsigned>(N)];
public:
int sum(int position) {
int result = 0;
while (position--) {
result += data[position];
position &= -~position;
}
return result;
}
void modify(int position, int delta) {
while (position < N) {
data[position] += delta;
position |= -~position;
}
}
};
#include <ctime>
#include <random>
#include <iostream>
auto main() -> int {
static constexpr int N = 1E7;
static constexpr int M = 2E7;
static WideSGT<N> tree;
int xorsum = 0;
std::mt19937 rng;
for (int i = M; i--; ) {
tree.modify(rng() % N, 1);
xorsum ^= i ^ tree.sum(rng() % N);
}
std::cerr << "XorSum = " << xorsum << '\n';
std::cout << "Duration = " << std::clock() << " clocks\n";
}