Zkw线段树
int prefix(int r) r += N; int res = 0; while (r) if (!(r & 1)) res += tree[r]; r >>= 1; return res;
Prefix sum [0, r] :
int lower_bound(int k) int pos = 1; while (pos < N) if (tree[pos<<1] < k) k -= tree[pos<<1]; pos = pos<<1 else pos = pos<<1; return pos - N; zkw线段树
Abstract The segment tree is a fundamental data structure for range queries and point updates. While recursive implementations are intuitive, they suffer from high constant factors due to function call overhead and conditional branching. This paper describes the zkw segment tree , a non‑recursive alternative introduced by Zhang Kunwei (zkw). By storing data in a perfect binary tree indexed from the bottom layer, it eliminates recursion entirely. The resulting implementation is shorter, faster, and particularly well‑suited for competitive programming and low‑latency systems. 1. Introduction A standard segment tree is usually built as an array tree[] of size about 4*N . Recursive functions traverse the tree to answer range sum/min/max queries or apply point updates. Despite its asymptotic $O(\log N)$ performance, recursion overhead and repeated bounds checking slow down execution. int prefix(int r) r += N; int res = 0; while (r) if (