I have just discovered, that std::set::lower_bound for larger sets is much-much faster than std::lower_bound.
Can anyone give explanation why does this happen?
Also, use std::set::lower_bound instead of std::lower_bound, I struggled on a problem for like 2 hours because of getting TLE for this.
std::lower_bound это бинпоиск по элементу и в данном случае он работает за N * log2, в то время как std::set::lower_bound это проходу по дереву который суммарно ищет за log
У std::set нет итератора произвольного доступа
http://www.cplusplus.com/reference/algorithm/lower_bound/:
http://www.cplusplus.com/reference/set/set/:
As far as i know std::lower_bound works with complexity O(log n) * O(access_of_element), it is O(log n) for vectors, arrays, but for sets it is something like O(n * log^2 n) (because you can get element in set in O(n log n)). std::set::lower_bound is function specified for using in sets, it has complexity O(log n).
Actually,See this StackOverflow question and, especially, WaltK's comment.std::lower_bound
on aset
is (not much better anyway).Yes, i just thought that we need about O(log n) operations to go to the next element in rb-tree every time, but it is O(n) in total, so final complexity is O(n * log n).
I'm pretty sure it's implemented in O(n)
I'm not so sure.
It is a binary search, after all, and generally cannot be replaced with a linear one, because the comparison function might have insanely huge complexity.
In my STL implementation:
It still does O(n) advances
You're right, sorry.