Zkw线段树 ((install)) -

The ZKW segment tree works by maintaining a balance between the value and lazy attributes of each node. When an update is made to a range, the lazy attribute of the corresponding node is updated. Then, when a query is made, the lazy attribute is propagated to the child nodes, and the value attribute is updated accordingly.

int query(int l, int r) int ans = 0; for (l += M - 1, r += M + 1; l ^ r ^ 1; l >>= 1, r >>= 1) if (~l & 1) ans += tree[l ^ 1]; // l 是左儿子,包含右兄弟 if (r & 1) ans += tree[r ^ 1]; // r 是右儿子,包含左兄弟 return ans; Use code with caution. 三、 优缺点对比 Efficient and easy segment trees - Codeforces zkw线段树

查询区间 $[l, r]$ 的和。 这是 ZKW 线段树最精妙的部分。传统写法需要递归判断左右儿子,而 ZKW 使用了一种类似于“双指针撞墙”的方法。 The ZKW segment tree works by maintaining a

int prefix(int r) r += N; int res = 0; while (r) if (!(r & 1)) res += tree[r]; r >>= 1; int query(int l, int r) int ans =