🐐
LeetGoat
Interview Prep
Competitive Programming
Discord
Range Query Picker
Range queries
2D range queries
Sets
Union find
Strings
Tree queries
Tree helpers
Graphs
Matrices
Math
Primes
Bit ops & Bitset
Geometry
Misc.
Golden References
Generic seg trees ▾
Values are chars
No sparse
Include sparse
Only sparse
Unselect all
Useful
getAll
()
Get the whole array (for final answer)
size
()
How many elements the array has right now
Point operations
pointGet
(i)
Read a[i]
pointAssign
(i, v)
a[i] = v
pointAdd
(i, v)
a[i] += v
pointChmax
(i, v)
a[i] = max(a[i], v)
pointChmin
(i, v)
a[i] = min(a[i], v)
Range updates
rangeAdd
(l, r, v)
a[i] += v
rangeAssign
(l, r, v)
a[i] = v
rangeChmax
(l, r, v)
a[i] = max(a[i], v)
rangeChmin
(l, r, v)
a[i] = min(a[i], v)
Uncommon
rangeReplace
(l, r, x, y)
Every a[i] = x in l..r becomes y (mass change)
rangeSortAsc
(l, r)
Sort l..r ascending in place
rangeSortDesc
(l, r)
Sort l..r descending in place
rangeAddArithSeq
(l, r, start, step)
a[l] += start, a[l+1] += start+step, …
rangeMod
(l, r, mod)
a[i] %= mod (needs a[i] ≥ 0, mod > 0)
rangeFloorDiv
(l, r, divisor)
a[i] = ⌊a[i] / divisor⌋ (needs a[i] ≥ 0, divisor > 0)
rangeFloorSqrt
(l, r)
a[i] = ⌊√a[i]⌋ (needs a[i] ≥ 0)
rangeAddMod
(l, r, v)
a[i] = (a[i] + v) % MOD
rangeAssignMod
(l, r, v)
a[i] = v % MOD
rangeMultMod
(l, r, m)
a[i] = (a[i] · m) % MOD
rangeAffineMod
(l, r, mult, add)
a[i] = ((a[i] · mult) + add) % MOD
rangeAddArithSeqMod
(l, r, start, step)
a[l] += start, a[l+1] += start+step, … (% MOD)
Search (tree walks)
leftmostIndexOf
(x)
First i with a[i] = x
rightmostIndexOf
(x)
Last i with a[i] = x
leftmostIndexOfInRange
(l, r, x)
First i in [l, r] with a[i] = x
rightmostIndexOfInRange
(l, r, x)
Last i in [l, r] with a[i] = x
leftmostGteX
(l, r, x)
First i in [l, r] with a[i] ≥ x
leftmostGtX
(l, r, x)
First i in [l, r] with a[i] > x
rightmostGteX
(l, r, x)
Last i in [l, r] with a[i] ≥ x
rightmostGtX
(l, r, x)
Last i in [l, r] with a[i] > x
leftmostLteX
(l, r, x)
First i in [l, r] with a[i] ≤ x
leftmostLtX
(l, r, x)
First i in [l, r] with a[i] < x
rightmostLteX
(l, r, x)
Last i in [l, r] with a[i] ≤ x
rightmostLtX
(l, r, x)
Last i in [l, r] with a[i] < x
Uncommon
kthGteX
(l, r, k, x)
Index of the k-th i in [l, r] with a[i] ≥ x
kthGtX
(l, r, k, x)
Index of the k-th i in [l, r] with a[i] > x
kthLteX
(l, r, k, x)
Index of the k-th i in [l, r] with a[i] ≤ x
kthLtX
(l, r, k, x)
Index of the k-th i in [l, r] with a[i] < x
Range queries
rangeSum
(l, r)
a[l] + … + a[r]
sumAll
()
a[0] + … + a[n-1]
rangeMin
(l, r)
min of l..r
minAll
()
min of the whole array
rangeMax
(l, r)
max of l..r
maxAll
()
max of the whole array
rangeGcd
(l, r)
gcd of l..r
gcdAll
()
gcd of the whole array
lcmAll
()
lcm of the whole array
maxSubarraySum
(l, r)
Max sum of non-empty subarray within l..r
minSubarraySum
(l, r)
Min sum of non-empty subarray within l..r
Uncommon
countOccurrences
(l, r, x)
# of i in [l, r] with a[i] = x
countDistinct
(l, r)
# of distinct values in l..r
allDistinct
(l, r)
Are all values in l..r distinct?
rangeMode
(l, r)
(mode, frequency) of l..r
costMonoIncreasing
(l, r)
Min +1 ops to make l..r non-decreasing
costMonoDecreasing
(l, r)
Min +1 ops to make l..r non-increasing
costStrictIncreasing
(l, r)
Min +1 ops to make l..r strictly increasing
costStrictDecreasing
(l, r)
Min +1 ops to make l..r strictly decreasing
maxAltSubseqSum
(l, r)
Max alt subsequence sum +a − b + c … in l..r
longestEqualRun
(l, r)
Longest run of equal adjacent values in l..r
doAllElementsAppearEvenFrequency
(l, r)
All vals in l..r even frq?
rangeSumMod
(l, r)
(a[l] + … + a[r]) % MOD
productAllMod
()
(a[0] · … · a[n-1]) % MOD
Treap-esque operations
append
(val)
Add val at the end
appendLeft
(val)
Add val at the front (every index shifts up by 1)
pop
()
Remove and return the last value
popLeft
()
Remove and return the first value (every index shifts down by 1)
split
(k)
Split into two arrays: 0..k−1 and a[k]… (the original is left empty)
eraseRange
(l, r)
Remove l..r
cut
(l, r)
Remove l..r and get it back as a separate array of the same type
trisect
(l, r)
Split into three arrays: 0..l−1, l..r, a[r+1]… (the original is left empty)
merge
(treap)
Append all of another treap of the same type (that treap ends up empty)
copy
(l, r)
A copy of l..r as a separate treap (this one is unchanged)
paste
(i, treap)
Insert all of another treap of the same type starting at index i (that treap ends up empty)
insertAt
(i, val)
Insert val so it becomes a[i] (i = n appends)
find
(x)
Index of x right now, or −1 (the values must be distinct)
rangeReverse
(l, r)
Reverse l..r in place
Uncommon
rangeRotateLeft
(l, r, k)
Cyclic shift l..r left by k (a[l+k] moves to l)
rangeRotateRight
(l, r, k)
Cyclic shift l..r right by k (a[r−k+1…r] move to the front)
insertCopiesAt
(i, val, k)
Insert k copies of val so they become i..i+k−1
rangeTwinSpin
(l, r)
Swap a[l]↔a[l+1], a[l+2]↔a[l+3], … (an odd last one stays)
Sorted view of a[l..r]
Rank
kthSmallest
(l, r, k)
k-th smallest of l..r (k = 1 is the min)
kthLargest
(l, r, k)
k-th largest of l..r (k = 1 is the max)
medianLow
(l, r)
Lower median of l..r
medianHigh
(l, r)
Upper median of l..r
sumAbsDiffToMedian
(l, r)
Σ |a[i] − median| over l..r
Nearest to x
smallestGteX
(l, r, x)
Smallest value ≥ x in l..r
smallestGtX
(l, r, x)
Smallest value > x in l..r
largestLteX
(l, r, x)
Largest value ≤ x in l..r
largestLtX
(l, r, x)
Largest value < x in l..r
Count
countLtX
(l, r, x)
# of values < x in l..r
countLteX
(l, r, x)
# of values ≤ x in l..r
countGtX
(l, r, x)
# of values > x in l..r
countGteX
(l, r, x)
# of values ≥ x in l..r
countInValRange
(l, r, lo, hi)
# of values in l..r with lo ≤ value ≤ hi
Sum
sumLtX
(l, r, x)
Sum of the values < x in l..r
sumLteX
(l, r, x)
Sum of the values ≤ x in l..r
sumGtX
(l, r, x)
Sum of the values > x in l..r
sumGteX
(l, r, x)
Sum of the values ≥ x in l..r
sumInValRange
(l, r, lo, hi)
Sum of values in l..r with lo ≤ value ≤ hi
Sum by rank
sumSmallestK
(l, r, k)
Sum of the k smallest values in l..r
sumLargestK
(l, r, k)
Sum of the k largest values in l..r
sumKthSmallestRange
(l, r, kFrom, kTo)
Sum of the kFrom-th … kTo-th smallest values in l..r (1-indexed ranks)
sumKthLargestRange
(l, r, kFrom, kTo)
Sum of the kFrom-th … kTo-th largest values in l..r (1-indexed ranks)
Distance
sumAbsDiffToX
(l, r, x)
Σ |a[i] − x| over l..r
Mex
rangeMex
(l, r)
Smallest integer ≥ 0 missing from l..r
▶
Prefix / suffix sums
▶
Hashing (strings)
▶
LIS
▶
Brackets
▶
XOR
▶
OR
▶
AND
▶
Strided queries (every step-th element)