Hepic_Antony_Skarlatos's blog

By Hepic_Antony_Skarlatos, 9 years ago, In English

I was trying to solve a problem here in codeforces,and I used "set". I wanted to search for a value so I used "lower_bound". I submitted and I took TLE. After I changed a bit the source,and I used find() instead of lower_bound() and I passed all testcases. Why happens that? Is find() faster than lower_bound()??

Thank you !!!

»
9 years ago, # |
  Vote: I like it +8 Vote: I do not like it

Can u please post links to both the submission?

»
9 years ago, # |
Rev. 2   Vote: I like it +4 Vote: I do not like it

This issue was already discussed on codeforces several times. The problem is that upper_bound and lower_bound require random-access iterators. Only in that case they work in O(logN). If iterators are not random-access (like in std::set) these functions work in O(N) time. Use width.upper_bound(val) instead of upper_bound(ALL(width),val) — it will work in O(logN) time

»
9 years ago, # |
  Vote: I like it +8 Vote: I do not like it

Use
myset.lowerbound(myset.begin(), myset.end()) O(logn)
instead
std::lowerbound(myset.begin(), myset.end()) O(n)