Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Advanced & Niche/Segment Tree / Fenwick (BIT)

Technique

Persistent Segment Tree

On each update copy only the O(log n) changed nodes, keeping every past version queryable.

Learn firstSegment Tree / Fenwick (BIT)
0%
mastery
0/1 solved
Practise nextRange Queries and Copies
IdeaPracticePitfalls

Recognise it

The signal

queries on historical versions

  1. 1Brute force

    Copy the whole array per version.

  2. 2The observation

    Full copies: unchanged subtrees are shared.

  3. 3Why it works

    Version t's root sees exactly the state after the first t updates.

Reach for it when

  • +k-th smallest in a subarray (versions over prefixes)
  • +copy-and-modify arrays

Not when

  • ×offline answers suffice (sort queries and use a normal BIT)

Practice

Next

Range Queries and Copies

0/1 solved
  • Range Queries and CopiesCSESHard

Pitfalls

Misconceptions

  • !memory blow-up from allocating children with new in tight limits
Go deeperresources

Resources

  • Segment Treecp-algorithms · article
  • Fenwick Treecp-algorithms · article
  • ITMO Segment Tree courseCodeforces EDU · course
  • Efficient and easy segment treesAl.cash · blog
  • Meet in the MiddleUSACO Guide · course
  • Matrix ExponentiationUSACO Guide · course
  • Reservoir samplingWikipedia · article
TodayReviewPatternsProblems