Despite other answers, you can use Fenwick trees for minimal range queries for any range. I posted a detailed explanation here:
How to adapt the Fenwick tree to meet the minimum range requirements
In short, you need to maintain
- array representing real values โโfor nodes [1, N]
- Fenwick tree with root 0, where the parent of any node i is
i-(i&-i) - Fenwick tree with N + 1 as the root, where the parent of any node i is
i+(i&-i)
To query any range in O (log n)
Query(int a, int b) { int val = infinity
To update any value in amortized O (log n), you need to update the real array and both trees. Single tree update:
while (node <= n+1) { if (v > tree[node]) { if (oldValue == tree[node]) { v = min(v, real[node]) for-each child { v = min(v, tree[child]) } } else break } if (v == tree[node]) break tree[node] = v node = parentOf(node, tree) }
source share