Segment Tree Problems
Problem Difficulty Levels
- Beginner - For those just starting with segment trees
- Easy - Simple applications of segment trees
- Medium - Requires deeper understanding of segment tree concepts
- Hard - Complex applications with multiple operations
- Expert - Advanced segment tree techniques
Problems Collection
Problem 1: Query Minimum Value
Difficulty: Beginner
Find the minimum value in a range [l,r].
Problem 2: Binary Simulation
Difficulty: Easy
Two operations:
- Invert bits from range [i,j]
- Query whether the ith bit is 0 or 1
Problem 3: Curious Robin Hood
Difficulty: Easy
Three operations:
- Query index id and update index id to zero
- Add value v to index id
- Query sum in range [l,r]
Problem 4: Xenia and Bit Operations
Difficulty: Medium
Two operations:
- Assignment: ap = b
- Output value ‘v’ (calculated by alternate ‘or’ and ‘xor’)
Problem 5: Shoot and Kill
Difficulty: Medium
Two operations:
- Query with two integers L and R, denoting time hunter will be in forest
- Find maximum number of segments that could be hit by any point from L to R
Problem 6: Horrible Queries
Difficulty: Easy
Two operations:
- Add v to all numbers in range [l,r]
- Query sum in range [l,r]
Problem 7: GSS1 - Max Subarray Sum
Difficulty: Medium
Query(x,y) = Max { a[i]+a[i+1]+…+a[j] ; x ≤ i ≤ j ≤ y }
Problem 8: GSS3 - Max Subarray Sum with Updates
Difficulty: Medium
Two operations:
- Modify the i-th element with value v
- Query(x,y) = Max { a[i]+a[i+1]+…+a[j] ; x ≤ i ≤ j ≤ y }
Problem 9: GSS5 - Complex Max Subarray Sum
Difficulty: Hard
Query(x1,y1,x2,y2) = Max {A[i]+A[i+1]+…+A[j]} where x1 ≤ i ≤ y1, x2 ≤ j ≤ y2 and x1 ≤ x2, y1 ≤ y2
Problem 10: Maximum Sum
Difficulty: Medium
Two operations:
- Set the value of A[i] to x
- Find i and j such that x ≤ i, j ≤ y and i != j, to maximize sum A[i]+A[j]
Problem 11: XOR on Segment
Difficulty: Hard
Two operations:
- Apply XOR with value x to each element in segment [l, r]
- Query sum in range [l,r]
Problem 12: Election Posters
Difficulty: Medium
Given ranges of form [li, ri], denoting leftmost and rightmost sections covered by the i-th poster,
output the number of posters with visible sections.
Problem 13: Brackets
Difficulty: Medium
Two operations:
- Change the i-th bracket to the opposite one
- Check if the word is a correct bracket expression
Problem 14: Light Switching
Difficulty: Medium
Two operations:
- Invert switches between [l,r]
- Count how many lights are on in range [l,r]
Problem 15: K-Query Online
Difficulty: Hard
For each k-query (i, j, k), return the number of elements greater than k in the subsequence ai, ai+1, …, aj.
Problem 16: Counting Primes
Difficulty: Medium
Two operations:
- Change all numbers in range [x,y] to value v
- Count the number of primes between index x and y
Problem 17: Sereja and Brackets
Difficulty: Medium
Given a bracket sequence, count the length of the maximum correct bracket subsequence.
Problem 18: Ant Colony
Difficulty: Hard
Given n ants with strengths si, where ant i gets one battle point only if si divides sj.
An ant with vi battle points is freed only if vi = r - l.
Count how many ants will be eaten if those ants fight.
Problem 19: Multiple of 3
Difficulty: Medium
Answer how many numbers between indices A and B are divisible by 3.
Problem 20: GM Plants
Difficulty: Hard
Given a cuboidal box of size Nx × Ny× Nz made of unit cubes:
- Range update with val = 1 in X, Y, Z axis
- Report the number of red plants (1) in the cuboidal region with coordinates (x1,y1,z1) and (x2,y2,z2)