<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>https://www.33dai.wiki/index.php?action=history&amp;feed=atom&amp;title=13-%E9%AB%98%E7%BA%A7%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%2F01-%E6%A0%91%E7%8A%B6%E6%95%B0%E7%BB%84</id>
	<title>13-高级数据结构/01-树状数组 - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://www.33dai.wiki/index.php?action=history&amp;feed=atom&amp;title=13-%E9%AB%98%E7%BA%A7%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%2F01-%E6%A0%91%E7%8A%B6%E6%95%B0%E7%BB%84"/>
	<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=13-%E9%AB%98%E7%BA%A7%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/01-%E6%A0%91%E7%8A%B6%E6%95%B0%E7%BB%84&amp;action=history"/>
	<updated>2026-05-20T23:10:08Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=13-%E9%AB%98%E7%BA%A7%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/01-%E6%A0%91%E7%8A%B6%E6%95%B0%E7%BB%84&amp;diff=447&amp;oldid=prev</id>
		<title>33DAI：​批量导入三三文档</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=13-%E9%AB%98%E7%BA%A7%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/01-%E6%A0%91%E7%8A%B6%E6%95%B0%E7%BB%84&amp;diff=447&amp;oldid=prev"/>
		<updated>2026-05-20T18:24:10Z</updated>

		<summary type="html">&lt;p&gt;批量导入三三文档&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2026年5月20日 (三) 18:24的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l117&quot;&gt;第117行：&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;第117行：&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:高级数据结构]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:高级数据结构]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:三三文档]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[Category:三三文档]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key my_wiki:diff:1.41:old-425:rev-447:php=table --&gt;
&lt;/table&gt;</summary>
		<author><name>33DAI</name></author>
	</entry>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=13-%E9%AB%98%E7%BA%A7%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/01-%E6%A0%91%E7%8A%B6%E6%95%B0%E7%BB%84&amp;diff=425&amp;oldid=prev</id>
		<title>33DAI：​导入1个版本</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=13-%E9%AB%98%E7%BA%A7%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/01-%E6%A0%91%E7%8A%B6%E6%95%B0%E7%BB%84&amp;diff=425&amp;oldid=prev"/>
		<updated>2026-05-20T18:12:30Z</updated>

		<summary type="html">&lt;p&gt;导入1个版本&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2026年5月20日 (三) 18:12的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;zh-Hans-CN&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;（没有差异）&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>33DAI</name></author>
	</entry>
	<entry>
		<id>https://www.33dai.wiki/index.php?title=13-%E9%AB%98%E7%BA%A7%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/01-%E6%A0%91%E7%8A%B6%E6%95%B0%E7%BB%84&amp;diff=424&amp;oldid=prev</id>
		<title>-&gt;Importer：​批量导入三三文档</title>
		<link rel="alternate" type="text/html" href="https://www.33dai.wiki/index.php?title=13-%E9%AB%98%E7%BA%A7%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/01-%E6%A0%91%E7%8A%B6%E6%95%B0%E7%BB%84&amp;diff=424&amp;oldid=prev"/>
		<updated>2026-05-20T16:50:33Z</updated>

		<summary type="html">&lt;p&gt;批量导入三三文档&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
== 树状数组（Fenwick Tree / BIT） ==&lt;br /&gt;
&lt;br /&gt;
树状数组用于高效维护&amp;#039;&amp;#039;&amp;#039;前缀和&amp;#039;&amp;#039;&amp;#039;，支持单点修改和区间查询，时间复杂度 &amp;lt;math&amp;gt;O(\log n)&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
=== 核心操作 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;lowbit(x) = x &amp;amp; -x&amp;lt;/code&amp;gt;，取出 &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; 在二进制下最低位的 &amp;lt;math&amp;gt;1&amp;lt;/math&amp;gt; 及其后面的 &amp;lt;math&amp;gt;0&amp;lt;/math&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int lowbit(int x)&lt;br /&gt;
{&lt;br /&gt;
    return x &amp;amp; -x;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 基础模板 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int bit[MAXN], n;&lt;br /&gt;
&lt;br /&gt;
// 单点 a[i] += delta&lt;br /&gt;
void add(int i, int delta)&lt;br /&gt;
{&lt;br /&gt;
    for (; i &amp;lt;= n; i += lowbit(i))&lt;br /&gt;
        bit[i] += delta;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// 查询前缀和 sum[1..i]&lt;br /&gt;
int sum(int i)&lt;br /&gt;
{&lt;br /&gt;
    int res = 0;&lt;br /&gt;
    for (; i &amp;gt; 0; i -= lowbit(i))&lt;br /&gt;
        res += bit[i];&lt;br /&gt;
    return res;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// 区间和 [l, r]&lt;br /&gt;
int range_sum(int l, int r)&lt;br /&gt;
{&lt;br /&gt;
    return sum(r) - sum(l - 1);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 应用场景 ==&lt;br /&gt;
&lt;br /&gt;
=== 1. 维护权值数组 ===&lt;br /&gt;
&lt;br /&gt;
将值作为下标，快速统计区间内元素个数（结合离散化）：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// 统计 &amp;lt;= x 的元素个数&lt;br /&gt;
int cnt = sum(x);&lt;br /&gt;
&lt;br /&gt;
// 插入一个元素&lt;br /&gt;
add(x, 1);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 2. 求逆序对 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
long long inversion_count(vector&amp;lt;int&amp;gt;&amp;amp; a)&lt;br /&gt;
{&lt;br /&gt;
    // 先离散化&lt;br /&gt;
    Discrete disc(a);&lt;br /&gt;
    long long ans = 0;&lt;br /&gt;
    memset(bit, 0, sizeof bit);&lt;br /&gt;
    for (int i = 1; i &amp;lt;= n; i++)&lt;br /&gt;
    {&lt;br /&gt;
        int idx = disc.id(a[i]);&lt;br /&gt;
        ans += i - 1 - sum(idx); // 前面比 a[i] 大的数&lt;br /&gt;
        add(idx, 1);&lt;br /&gt;
    }&lt;br /&gt;
    return ans;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== 3. 维护差分数组（区间修改 + 单点查询） ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// 区间 [l, r] + k&lt;br /&gt;
add(l, k);&lt;br /&gt;
add(r + 1, -k);&lt;br /&gt;
&lt;br /&gt;
// 查询 a[x]（即差分数组的前缀和）&lt;br /&gt;
int val = sum(x);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 二维树状数组 ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int bit[MAXN][MAXM];&lt;br /&gt;
&lt;br /&gt;
void add(int x, int y, int delta)&lt;br /&gt;
{&lt;br /&gt;
    for (int i = x; i &amp;lt;= n; i += lowbit(i))&lt;br /&gt;
        for (int j = y; j &amp;lt;= m; j += lowbit(j))&lt;br /&gt;
            bit[i][j] += delta;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int sum(int x, int y)&lt;br /&gt;
{&lt;br /&gt;
    int res = 0;&lt;br /&gt;
    for (int i = x; i &amp;gt; 0; i -= lowbit(i))&lt;br /&gt;
        for (int j = y; j &amp;gt; 0; j -= lowbit(j))&lt;br /&gt;
            res += bit[i][j];&lt;br /&gt;
    return res;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// 子矩阵 (x1,y1) 到 (x2,y2) 的和&lt;br /&gt;
int query(int x1, int y1, int x2, int y2)&lt;br /&gt;
{&lt;br /&gt;
    return sum(x2, y2) - sum(x1 - 1, y2) - sum(x2, y1 - 1) + sum(x1 - 1, y1 - 1);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:高级数据结构]]&lt;br /&gt;
[[Category:三三文档]]&lt;br /&gt;
&lt;/div&gt;</summary>
		<author><name>-&gt;Importer</name></author>
	</entry>
</feed>