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

Lazy Propagation Segment Tree

Store pending range updates at nodes and push them down only when a child is visited, so range assign/add + range query is O(log n).

Learn firstSegment Tree / Fenwick (BIT)UnlocksTreap / Implicit Treap
0%
mastery
0/8 solved
Practise nextNagini
IdeaPracticePitfalls

Recognise it

The signal

range add/assign plus range sum/min

  1. 1Brute force

    Point-update every element in the range: O(n) per update.

  2. 2The observation

    Per-element range updates: a fully covered node absorbs the update as a tag.

  3. 3Why it works

    Each node's value is correct given all tags on its ancestors have been applied.

Reach for it when

  • +interval painting with queries
  • +toggle ranges

Not when

  • ×only point queries after range adds (Fenwick with diff-array)
  • ×updates that do not compose (segment tree beats or sqrt)

Practice

Next

Nagini

0/8 solved
  • Range Updates and SumsCSESMedium
  • NaginiCodeforces3100
  • StationsCodeforces3400
  • Bear and Bad Powers of 42Codeforces3100
  • Polynomial QueriesCSESHard
  • Physical Education LessonsCodeforces2300
  • A Simple TaskCodeforces2300
  • Please, another Queries on Array?Codeforces2400

Pitfalls

Misconceptions

  • !wrong order of composing assign and add tags
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