-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFenwickTreeInt.cs
More file actions
104 lines (93 loc) · 2.75 KB
/
Copy pathFenwickTreeInt.cs
File metadata and controls
104 lines (93 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// filepath: f:\Terraria\Github\NetSimplified\NetSimplifiedExample\CustomTypes\SegmentTreeIntSum.cs
using System;
namespace NetSimplifiedExample.CustomTypes;
/// <summary>
/// 基于差分的区间加法 + 区间求和的数据结构(Fenwick 树实现)。
/// 原名 SegmentTreeIntSum,已重命名为 FenwickTreeInt,不再继承自 SegmentTree<int>。
/// 支持 RangeAdd 和 RangeSum 操作。
/// </summary>
public class FenwickTreeInt
{
private readonly int _n;
private readonly long[] _bit1;
private readonly long[] _bit2;
public FenwickTreeInt(int n)
{
_n = n;
_bit1 = new long[_n + 2];
_bit2 = new long[_n + 2];
}
public FenwickTreeInt(int[] initial)
{
_n = initial?.Length ?? 0;
_bit1 = new long[_n + 2];
_bit2 = new long[_n + 2];
if (_n > 0 && initial != null)
{
// 构建为每个位置添加初始值
for (int i = 0; i < _n; i++)
{
RangeAdd(i, i, initial[i]);
}
}
}
private void AddInternal(long[] bit, int idx, long val)
{
idx++; // 使用 1-based 内部索引
while (idx <= _n + 1)
{
bit[idx] += val;
idx += idx & -idx;
}
}
private long SumInternal(long[] bit, int idx)
{
idx++; // 1-based
long res = 0;
while (idx > 0)
{
res += bit[idx];
idx -= idx & -idx;
}
return res;
}
// 在区间 [l, r] 上加上 val
public void RangeAdd(int l, int r, int val)
{
if (l < 0 || r < l || r >= _n) throw new ArgumentOutOfRangeException();
long v = val;
// 对 bit1 和 bit2 做差分
AddInternal(_bit1, l, v);
AddInternal(_bit1, r + 1, -v);
AddInternal(_bit2, l, v * (l - 1));
AddInternal(_bit2, r + 1, -v * r);
}
// 前缀和 [0..idx]
private long PrefixSum(int idx)
{
if (idx < 0) return 0;
if (idx >= _n) idx = _n - 1;
long s1 = SumInternal(_bit1, idx);
long s2 = SumInternal(_bit2, idx);
return s1 * idx - s2;
}
// 区间求和 [l, r]
public long RangeSum(int l, int r)
{
if (l < 0 || r < l || r >= _n) throw new ArgumentOutOfRangeException();
return PrefixSum(r) - PrefixSum(l - 1);
}
// 获取指定位置(点)的值
public int GetPoint(int idx)
{
if (idx < 0 || idx >= _n) throw new ArgumentOutOfRangeException(nameof(idx));
return (int)RangeSum(idx, idx);
}
// 导出当前数组快照(用于序列化)
public int[] ToArray()
{
var arr = new int[_n];
for (int i = 0; i < _n; i++) arr[i] = GetPoint(i);
return arr;
}
}