Rust Tips
TL;DR
A collection of notes on things worth remembering in Rust.
Order
When you need to inspect ordering properly, using std::cmp::Ordering makes it easy to read. It also works for lexicographic ordering of String. It cannot be used with floats.
min, max of float
Since Ord is not implemented for floats, std::cmp::max and similar functions cannot be used.
cumsum
std::vec binary_search
This is quite useful on its own, but when the slice contains duplicate values, although you can match against all of them, the returned index corresponds to any one of the matches. So if you want a lower_bound, it is better to use superslice. Alternatively, you can implement it yourself.
The following is quoted from the docs:
If the value is found then Result::Ok is returned, containing the index of the matching element. If there are multiple matches, then any one of the matches could be returned. If the value is not found then Result::Err is returned, containing the index where a matching element could be inserted while maintaining sorted order.
After some quick testing, on the AtCoder version (1.42.0), i returns 4. Apparently, the behavior differs across versions. There has been discussion in the Rust community about this, but it is unclear when it will be addressed.
So if you want an upper_bound, you could do something like the following. That said, it is probably better to just implement it yourself.