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

Offline Query Processing

Read all queries first and answer them in an order (by threshold, right endpoint, or reversed time) that turns a hard online problem into incremental updates.

Learn firstUnion-Find (Disjoint Set)Segment Tree / Fenwick (BIT)UnlocksMo's Algorithm (offline range queries)DSU with Rollback / Offline Dynamic ConnectivityParallel Binary SearchCDQ Divide and Conquer
0%
mastery
0/16 solved
Practise nextDynamic Connectivity
IdeaPracticePitfalls

Recognise it

The signal

queries with a threshold (edges with weight < limit)

  1. 1Brute force

    Answer each query independently from scratch.

  2. 2The observation

    Independent recomputation: sorted queries let a DSU/BIT absorb data monotonically.

  3. 3Why it works

    When query i is answered, the structure contains exactly the data relevant to it.

Reach for it when

  • +distinct values in [l, r]
  • +deletions that become insertions when reversed

Not when

  • ×interactive / forced-online queries (encoded with last answer)

Practice

Next

Dynamic Connectivity

0/16 solved
  • Distinct Values QueriesCSESMedium
  • Network BreakdownCSESMedium
  • Existence of a Path with Edge LimitGoogleOAMedium
  • Dynamic ConnectivityCSESHard
  • Extending Set of PointsCodeforces2600
  • Bipartite CheckingCodeforces2500
  • Maximum Sum QueriesHard
  • Minimum Interval to Include Each QueryHard
  • One OccurrenceCodeforces2400
  • Checking Existence of Edge Length Limited PathsHard
  • Maximum XOR With an Element From ArrayHard
  • Maximum Genetic Difference QueryHard

Pitfalls

Misconceptions

  • !forgetting to store original query indices
Go deeper5 combinations · resources

Combinations

  • Offline Query Processing+Segment Tree / Fenwick (BIT)

    distinct values / one-occurrence queries on ranges

    Offline Query Processing:
    sorts queries by right endpoint
    Segment Tree / Fenwick (BIT):
    BIT keeps only the latest occurrence of each value

    Distinct Values QueriesOne Occurrence

  • Offline Query Processing+Union-Find (Disjoint Set)

    queries 'connected using edges with weight < limit'; reverse deletions

    Offline Query Processing:
    sorts edges and queries by threshold
    Union-Find (Disjoint Set):
    adds edges incrementally

    Checking Existence of Edge Length Limited PathsNetwork Breakdown

  • Binary (XOR) Trie+Offline Query Processing

    max XOR with an element <= m_i

    Binary (XOR) Trie:
    max XOR with inserted numbers
    Offline Query Processing:
    sorts queries by limit so only allowed numbers are inserted

    Maximum XOR With an Element From Array

  • DSU with Rollback / Offline Dynamic Connectivity+Segment Tree / Fenwick (BIT)+Offline Query Processing

    add edge / remove edge / count components (or check bipartiteness) with all operations given upfront

    DSU with Rollback / Offline Dynamic Connectivity:
    unite on entry, roll back on exit
    Segment Tree / Fenwick (BIT):
    a segment tree over time storing each edge in the O(log q) nodes covering its lifetime
    Offline Query Processing:
    every add/remove/query must be known before processing

    Dynamic ConnectivityExtending Set of PointsBipartite Checking

  • Binary (XOR) Trie+Tree DFS (Paths & Recursion)+Offline Query Processing

    max XOR of a value with any ancestor of a node, many queries

    Binary (XOR) Trie:
    the max XOR against values on the root path (counts per trie node allow removal)
    Tree DFS (Paths & Recursion):
    on entry insert the node's value, on exit remove it
    Offline Query Processing:
    attach the queries to their nodes

    Maximum Genetic Difference Query

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