Segment Tree Problems

A segment tree is a binary tree over an array where each node stores the answer (sum, min, GCD…) for one range, so range queries and updates both run in O(log n).

20 problems from LightOJ, SPOJ and Codeforces, ordered from first segment tree to hard. Each one is tagged with the technique it teaches and links to my solution. Tick off the ones you solve; your progress is saved in this browser.

A segment tree built over an array, each node holding the answer for its range
0 / 20 solved
  1. 1

    Array Queries

    Beginner LightOJ 1082

    Find the minimum value in a range [l, r].

  2. 2

    Binary Simulation

    Easy LightOJ 1080

    Support 2 operations:

    1. Invert every bit in [i, j]
    2. Report whether the i-th bit is 0 or 1
  3. 3

    Curious Robin Hood

    Easy LightOJ 1112

    Support 3 operations:

    1. Report a[id], then set it to zero
    2. Add v to a[id]
    3. Sum of [l, r]
  4. 4

    Horrible Queries

    Easy SPOJ HORRIBLE

    Support 2 operations:

    1. Add v to every number in [l, r]
    2. Sum of [l, r]
  5. 5

    Xenia and Bit Operations

    Medium Codeforces 339D

    Support 2 operations:

    1. Assign ap = b
    2. Print v, obtained by alternately OR-ing and XOR-ing adjacent pairs up the tree
  6. 6

    Shoot and Kill

    Medium SPOJ BGSHOOT

    Animals are present during time intervals. For each query [L, R] (the hunter's time in the forest), find the most animals a single shot at any moment in [L, R] can hit.

  7. 7

    Can you answer these queries I

    Medium SPOJ GSS1

    Query(x, y) = max { a[i] + … + a[j] : x ≤ i ≤ j ≤ y }.

  8. 8

    Can you answer these queries III

    Medium SPOJ GSS3

    Support 2 operations:

    1. Set the i-th element to v
    2. Query(x, y) = max { a[i] + … + a[j] : x ≤ i ≤ j ≤ y }
  9. 9

    Maximum Sum

    Medium SPOJ KGSS

    Support 2 operations:

    1. Set A[i] = x
    2. Find i ≠ j in [x, y] maximising A[i] + A[j]
  10. 10

    Election Posters

    Medium SPOJ POSTERS

    Posters are glued in order, the i-th covering sections [li, ri]. Count the posters that are still at least partly visible.

  11. 11

    Brackets

    Medium SPOJ BRCKTS

    Support 2 operations:

    1. Flip the i-th bracket
    2. Check whether the whole word is a correct bracket sequence
  12. 12

    Light Switching

    Medium SPOJ LITE

    Support 2 operations:

    1. Toggle every switch in [l, r]
    2. Count lights that are on in [l, r]
  13. 13

    Counting Primes

    Medium SPOJ CNTPRIME

    Support 2 operations:

    1. Set every number in [x, y] to v
    2. Count primes in [x, y]
  14. 14

    Sereja and Brackets

    Medium Codeforces 380C

    For each query [l, r], find the length of the longest correct bracket subsequence of s[l..r].

  15. 15

    Multiples of 3

    Medium SPOJ MULTQ3

    Support 2 operations:

    1. Add 1 to every number in [A, B]
    2. Count numbers in [A, B] divisible by 3
  16. 16

    Can you answer these queries V

    Hard SPOJ GSS5

    Query(x1, y1, x2, y2) = max { A[i] + … + A[j] : x1 ≤ i ≤ y1, x2 ≤ j ≤ y2 }, with x1 ≤ x2 and y1 ≤ y2. The two ranges may overlap.

  17. 17

    XOR on Segment

    Hard Codeforces 242E

    Support 2 operations:

    1. XOR every element in [l, r] with x
    2. Sum of [l, r]
  18. 18

    K-Query Online

    Hard SPOJ KQUERYO

    For each query (i, j, k), count the elements of ai, …, aj greater than k. Queries are encoded, so they must be answered online.

  19. 19

    Ant Colony

    Hard Codeforces 474F

    In [l, r] every pair of ants fights; ant i scores a point when si divides sj. Ants with exactly r − l points are freed. Count how many ants get eaten.

  20. 20

    GM Plants

    Hard SPOJ IOPC1207

    An Nx × Ny × Nz box of unit cubes. Toggle whole slabs along the X, Y or Z axis, then count red cubes inside a cuboid (x1, y1, z1)–(x2, y2, z2).

Frequently asked questions

What is a segment tree?

A segment tree is a binary tree built over an array where each node stores an aggregate (sum, minimum, GCD, and so on) of a contiguous range. It answers range queries and applies point updates in O(log n) time, using O(n) memory.

What is lazy propagation in a segment tree?

Lazy propagation defers range updates: instead of updating every element in [l, r], you tag the O(log n) nodes that cover the range and push the pending update down to children only when a later query or update visits them. This keeps range updates at O(log n).

In what order should I solve these segment tree problems?

Top to bottom. The list starts with plain range-minimum and range-sum queries, moves to lazy propagation, then to nodes that store custom merged state (such as the GSS maximum-subarray series), and ends with harder combinations like inclusion–exclusion over 3D toggles.