All the values that will ever be added are known in advance. Build a FenwickMultiset that answers ordered queries in O(log m) by compressing the values and keeping counts in a Fenwick tree.
new FenwickMultiset(universe): universe is an array of every value that may be added; it may be unsorted and contain duplicatesadd(x) adds one copy of x; if x is not in the universe, throw a RangeErrorremove(x) removes one copy and returns true, or returns false if there is none (also when x is outside the universe)size is a getterrank(x): how many stored values are smaller than x; countAtMost(x): how many are <= x. Both accept any number, inside the universe or notat(i): the i-th smallest value counting from 0, or undefined if out of rangefloor(x), lower(x), ceiling(x), higher(x): the usual neighbours, undefined if there is none