Lix2002's blog

By Lix2002, history, 13 months ago, In English

Well I just found this wierd thing when I was doing the problem Codeforces Round 905 (Div. 3) Prob.D When I use std::find(set.begin(), set.end(), value) or std::lower_bound(set.begin(), set.end(), value), I would get a TLE. But instead of using that, when I replace them with set.find(value) or set.lower_bound(value) it passed smoothly and worked as expected. Why would this happened? Is there any difference between these two approaches?

  • Vote: I like it
  • +14
  • Vote: I do not like it

| Write comment?
»
13 months ago, # |
  Vote: I like it +22 Vote: I do not like it

If you look closely on documentation on cppreference you will see that the function std::lower_bound works in linear time if container iterators are not RandomAccess. Set iterators are Bidirectional. std::find works always in linear time. So set.find and set.lower_bound are coded specifically for set and they work in $$$O(\log n)$$$ time.