Fenwick Trees

Fenwick Trees give O(log n) prefix sums and point updates. Simpler than segment trees for some tasks.

35 lessons
177 min

Lessons

1. Intro

Why Fenwick Trees matter

2m

2. The Core Idea

Numbers as sum of powers of 2

3m

3. Lowest Set Bit

The lowbit operation

3m

4. Fenwick Tree Structure

What each position stores

3m

5. Prefix Sum Query

Summing the tree

3m

6. Point Update

Propagating changes

3m

7. Building a Fenwick Tree

Initialization

3m

8. Problem - Range Sum Query Mutable

BIT vs Segment Tree

3m1 problems

9. BIT Implementation

Complete solution

4m

10. Range Sum BIT Solution

Implement your solution

12m1 problems

11. Range Update Point Query

Flipping the problem

4m

12. Range Update Range Query

Two BITs working together

5m

13. Problem - Count Inversions

Classic BIT application

3m1 problems

14. Inversions with BIT

Coordinate compression + BIT

4m

15. Count Inversions Solution

Implement your solution

15m1 problems

16. 2D Fenwick Tree

Extending to matrices

4m

17. 2D Range Sum Query

Rectangle queries

3m

18. Problem - Range Sum Query 2D Mutable

2D BIT application

3m1 problems

19. 2D BIT Solution

Implement your solution

15m1 problems

20. Order Statistics with BIT

Finding k-th element

4m

21. BIT for Offline Queries

Process in sorted order

4m

22. Problem - Count Smaller After Self

BIT for per-element counts

3m1 problems

23. Count Smaller BIT Solution

Implement your solution

12m1 problems

24. Problem - Reverse Pairs

Variant of inversion counting

3m1 problems

25. Reverse Pairs Solution

Implement your solution

15m1 problems

26. BIT vs Segment Tree

Choosing the right tool

3m

27. BIT for XOR Queries

Another invertible operation

3m

28. Finding First Position

Binary search on BIT

3m

29. Dynamic Frequency Queries

Multiset with BIT

3m

30. Problem - Global and Local Inversions

BIT for counting

3m1 problems

31. Global Local Solution

Implement your solution

10m1 problems

32. Common BIT Mistakes

Debugging tips

3m

33. Challenge: Implement Your Own

Write from memory

5m

34. Quiz: Fenwick Trees

Test your understanding

5m1 problems

35. Section Recap

What you learned

3m

Practice Problems

1.

Perfect introduction to BIT for range queries with frequency counting. Teaches the fundamental technique of answering offline range queries with BIT.

2.
MultisetCodeforcesmedium

Excellent for learning order statistics with BIT - finding k-th element with dynamic insertions/deletions using frequency arrays.

3.

Classic inversion counting that combines frequency calculation with BIT. Teaches non-trivial counting problems using Fenwick trees.

4.
Enemy is WeakCodeforcesmedium

Extends inversion counting to triplets. Teaches how to combine multiple BIT queries for complex counting problems.

5.
Babaei and Birthday CakeCodeforcesmedium

Brilliant combination of DP with BIT to find maximum weighted increasing subsequence. Shows how BIT can optimize DP from O(n²) to O(n log n).

6.
SubsequencesCodeforceshard

Advanced DP + BIT using multiple Fenwick trees to count increasing subsequences of specific lengths. Demonstrates multi-dimensional BIT concepts.

7.
Petya and ArrayCodeforcesmedium

Teaches coordinate compression with BIT for counting subarrays with sum less than threshold. Essential technique for large value ranges.

8.
Infinite InversionsCodeforceshard

Counting inversions with huge coordinates (up to 10^9). Masterclass in coordinate compression and offline query processing.

9.
Moving PointsCodeforceshard

Geometric problem with BIT - counting points with velocity constraints. Shows BIT's versatility beyond simple array operations.

10.
Nastya and King-ShamansCodeforcesmedium

Prefix sum queries with point updates. Great for understanding BIT's search capabilities for finding special positions.

11.
Curious ArrayCodeforceshard

Advanced range update with difference arrays and BIT. Teaches the powerful technique of using BIT for range updates.

12.
Duff in the ArmyCodeforceshard

Advanced BIT on trees with binary lifting. Combines LCA with BIT for efficient path queries maintaining top-k elements.

13.

The quintessential BIT problem - point updates and range sum queries. Perfect starting point for understanding Fenwick trees.

14.

Classic BIT application for counting inversions. Teaches coordinate compression and efficient counting while processing right-to-left.

15.
Reverse PairsLeetCodehard

Variant of inversion counting where condition is nums[i] > 2*nums[j]. Teaches adapting BIT techniques for modified conditions.

16.
Count of Range SumLeetCodehard

Advanced problem combining prefix sums with BIT to count subarrays in a range. Demonstrates BIT's power for complex counting.

17.

Dynamic frequency counting with BIT - tracks elements less than/greater than current during insertions. Real-world BIT application.

18.

Introduction to 2D BIT with point updates and rectangle sum queries. Essential for multi-dimensional Fenwick tree extensions.

19.

Transforms inequality into counting problem solvable with BIT. Teaches problem transformation and coordinate compression at scale.

20.

Optimizable with BIT to find k-th empty slot efficiently. Teaches using BIT for position tracking via binary search on BIT.

Ready to start learning?

Access all 35 lessons with interactive content and progress tracking.