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.
0/16 solved
Recognise it
The signal
queries with a threshold (edges with weight < limit)
1Brute force
Answer each query independently from scratch.
2The observation
Independent recomputation: sorted queries let a DSU/BIT absorb data monotonically.
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
- 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
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
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