跳到正文

剪贴板

剪贴板 qsoe4c1x

原文
剪贴板信息

剪贴板信息

收录
更新

线段树

  • 树状数组

这是一个树状数组

s:┌──────────────────────┐    ...└──────────────────────┘    ...┌──────────┐                                ┌─...└──────────┘                                └─...┌────┐                ┌────┐                ┌─...└────┘                └────┘                └─...┌─┐        ┌─┐        ┌─┐        ┌─┐        ┌─...└─┘        └─┘        └─┘        └─┘        └─...a:┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─...└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─...s:\\ \text{┌──────────────────────┐~~~~...} \\ \text{└──────────────────────┘~~~~...}\\ \text{┌──────────┐~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~┌─...} \\ \text{└──────────┘~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~└─...}\\ \text{┌────┐~~~~~~~~~~~~~~~~┌────┐~~~~~~~~~~~~~~~~┌─...} \\ \text{└────┘~~~~~~~~~~~~~~~~└────┘~~~~~~~~~~~~~~~~└─...}\\ \text{┌─┐~~~~~~~~┌─┐~~~~~~~~┌─┐~~~~~~~~┌─┐~~~~~~~~┌─...} \\ \text{└─┘~~~~~~~~└─┘~~~~~~~~└─┘~~~~~~~~└─┘~~~~~~~~└─...}\\ a:\\ \text{┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─...} \\ \text{└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─...}\\

分别存的是:

s[1]=a[1]s[2]=a[1]+a[2]s[3]=a[3]s[4]=a[1]+a[2]+a[3]+a[4]s[5]=a[5]s[6]=a[5]+a[6]s[7]=a[7]s[8]=a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]...\begin{aligned} s[1]&=a[1]\\ s[2]&=a[1]+a[2]\\ s[3]&=a[3]\\ s[4]&=a[1]+a[2]+a[3]+a[4]\\ s[5]&=a[5]\\ s[6]&=a[5]+a[6]\\ s[7]&=a[7]\\ s[8]&=a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]\\ \end{aligned} ...

如图:

s:┌──────────────────────┐    ...                                                 8└──────────────────────┘    ...┌──────────┐                                ┌─...4                └──────────┘                                └─...┌────┐                ┌────┐                ┌─...2                                6                 └────┘                └────┘                └─...┌─┐        ┌─┐        ┌─┐        ┌─┐        ┌─...1              3               5               7                 └─┘        └─┘        └─┘        └─┘        └─...a:┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─...1       2      3       4      5       6      7       8         └─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─...s:\\ \text{┌──────────────────────┐~~~~...} \\ \text{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~8} \\ \text{└──────────────────────┘~~~~...}\\ \text{┌──────────┐~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~┌─...} \\ \text{4~~~~~~~~~~~~~~~~} \\ \text{└──────────┘~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~└─...}\\ \text{┌────┐~~~~~~~~~~~~~~~~┌────┐~~~~~~~~~~~~~~~~┌─...} \\ \text{2~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~6~~~~~~~~~~~~~~~~~} \\ \text{└────┘~~~~~~~~~~~~~~~~└────┘~~~~~~~~~~~~~~~~└─...}\\ \text{┌─┐~~~~~~~~┌─┐~~~~~~~~┌─┐~~~~~~~~┌─┐~~~~~~~~┌─...} \\ \text{1~~~~~~~~~~~~~~3~~~~~~~~~~~~~~~5~~~~~~~~~~~~~~~7~~~~~~~~~~~~~~~~~} \\ \text{└─┘~~~~~~~~└─┘~~~~~~~~└─┘~~~~~~~~└─┘~~~~~~~~└─...}\\ a:\\ \text{┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─┐┌─...} \\ \text{1~~~~~~~2~~~~~~3~~~~~~~4~~~~~~5~~~~~~~6~~~~~~7~~~~~~~8~~~~~~~~~} \\ \text{└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─┘└─...}\\

我们把它写成二进制

┌──────────────────────┐    ...                                                1└──────────────────────┘    ...┌──────────┐                                ┌─...                         1      1      1      1      0         └──────────┘                                └─...┌────┐                ┌────┐                ┌─...        1      1       0      0      1      1      0         └────┘                └────┘                └─...┌─┐        ┌─┐        ┌─┐        ┌─┐        ┌─...1      0      1       0      1      0      1      0         └─┘        └─┘        └─┘        └─┘        └─...  \text{┌──────────────────────┐~~~~...} \\ \text{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~1} \\ \text{└──────────────────────┘~~~~...}\\ \text{┌──────────┐~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~┌─...} \\ \text{~~~~~~~~~~~~~~~~~~~~~~~~~1~~~~~~1~~~~~~1~~~~~~1~~~~~~0~~~~~~~~~} \\ \text{└──────────┘~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~└─...}\\ \text{┌────┐~~~~~~~~~~~~~~~~┌────┐~~~~~~~~~~~~~~~~┌─...} \\ \text{~~~~~~~~1~~~~~~1~~~~~~~0~~~~~~0~~~~~~1~~~~~~1~~~~~~0~~~~~~~~~} \\ \text{└────┘~~~~~~~~~~~~~~~~└────┘~~~~~~~~~~~~~~~~└─...}\\ \text{┌─┐~~~~~~~~┌─┐~~~~~~~~┌─┐~~~~~~~~┌─┐~~~~~~~~┌─...} \\ \text{1~~~~~~0~~~~~~1~~~~~~~0~~~~~~1~~~~~~0~~~~~~1~~~~~~0~~~~~~~~~} \\ \text{└─┘~~~~~~~~└─┘~~~~~~~~└─┘~~~~~~~~└─┘~~~~~~~~└─...}\\~~

可以发现以下特性:

  • a[x]a[x]的父节点是a[x+lowbit(x)]a[x+lowbit(x)]
  • a[x]a[x]的宽度是lowbit(x)lowbit(x)

所以:

  • add(x,val)add(x,val)为 a[x]a[x]+=valval a[x]a[x]的父节点=a[x+lowbit(x)]a[x+lowbit(x)]+=valval a[x]a[x]的父节点的父节点=a[x+lowbit(x)+lowbit(x+lowbit(x))]a[x+lowbit(x)+lowbit(x+lowbit(x))]+=valval …
  • sum(1,x)\quad sum(1,x) =a[x]+a[x−宽度]+...=a[x]+a[x-宽度]+... =a[x]+a[x−lowbit(x)]+...=a[x]+a[x-lowbit(x)]+...

由此我们可以写出树状数组代码(P3374):

C++
#include<bits/stdc++.h>
#define lowbit(x) (x&-x)
class tree {
	public:
		int64_t n, tree[500001];
		inline void add(int64_t x, int64_t val) {
			while (x <= n) tree[x] += val, x += lowbit(x);
		}
		inline int64_t sum(int64_t x) {
			int64_t ans = 0;
			while (x)ans += tree[x], x -= lowbit(x);
			return ans;
		}
};
tree a;
int main() {
	std::ios::sync_with_stdio(0);
	std::cin.tie(0), std::cout.tie(0);
	int64_t n, m;
	std::cin >> n >> m;
	a.n = n;
	for (int64_t i = 1, x = 0; i <= n; ++i) {
		std::cin >> x;
		a.tree[i] += x;
		if (i + lowbit(i) <= n)
			a.tree[i + lowbit(i)] += a.tree[i];
	}
	for (int64_t i = 0, op; i != m; ++i) {
		std::cin >> op;
		if (op == 1) {
			int64_t x, y;
			std::cin >> x >> y;
			a.add(x, y);
		} else {
			int64_t x, y;
			std::cin >> x >> y;
			std::cout << a.sum(y) - a.sum(x - 1) << "\n";
		}
	}
	return 0;
}

线段树

┌──────────────────────┐╔═...└──────────────────────┘╚═...┌──────────┐╔══════════╗┌─...└──────────┘╚══════════╝└─...┌────┐╔════╗┌────┐╔════╗┌─...└────┘╚════╝└────┘╚════╝└─...┌─┐╔═╗┌─┐╔═╗┌─┐╔═╗┌─┐╔═╗┌─...└─┘╚═╝└─┘╚═╝└─┘╚═╝└─┘╚═╝└─...\text{┌──────────────────────┐╔═...} \\ \text{└──────────────────────┘╚═...}\\ \text{┌──────────┐╔══════════╗┌─...} \\ \text{└──────────┘╚══════════╝└─...}\\ \text{┌────┐╔════╗┌────┐╔════╗┌─...} \\ \text{└────┘╚════╝└────┘╚════╝└─...}\\ \text{┌─┐╔═╗┌─┐╔═╗┌─┐╔═╗┌─┐╔═╗┌─...} \\ \text{└─┘╚═╝└─┘╚═╝└─┘╚═╝└─┘╚═╝└─...}\\

可以发现以下特性:

  • a[x][0]a[x][0]的子节点分别是 a[x−(lowbit(x)>>1)][0]a[x-(lowbit(x)>>1)][0]和a[x−(lowbit(x)>>1)][1]a[x-(lowbit(x)>>1)][1]
  • a[x][1]a[x][1]的子节点分别是 a[x+(lowbit(x)>>1)][0]a[x+(lowbit(x)>>1)][0]和a[x+(lowbit(x)>>1)][1]a[x+(lowbit(x)>>1)][1]
  • a[x][0]a[x][0]或a[x][1]a[x][1]的宽度都是lowbit(x)lowbit(x)

由此我们可以用两个数组模拟一棵树 然后

不干啦!

┌┐╔╗┏┓└┘╚╝┗┛╔╗┏┓┌┐╚╝┗┛└┘┌┐╔╗ ┏┓ \\ └┘╚╝ ┗┛\\ ╔╗ ┏┓┌┐ \\ ╚╝ ┗┛└┘\\ ┌─┐╔═╗ ┏━┓└─┘╚═╝ ┗━┛╔╗┏┓┌┐╚╝┗┛└┘\text{┌─┐╔═╗ ┏━┓} \\ \text{└─┘╚═╝ ┗━┛}\\ ╔╗ ┏┓┌┐ \\ ╚╝ ┗┛└┘\\ ┌┬┐├┼┤└┴┘┌┬┐ \\ ├┼┤ \\ └┴┘ \\ ╔═╗║╬║╚═╝╔═╗\\ ║╬║\\ ╚═╝\\ ┏┳┓┣╋┫┗┻┛┏┳┓\\ ┣╋┫\\ ┗┻┛