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 !!!
Can u please post links to both the submission?
find() : http://mirror.codeforces.com/contest/527/submission/10646660
upper_bound(){sorry it is not lower_bound() :P, but here do the same work) : http://mirror.codeforces.com/contest/527/submission/10646608
try using left=width.upper_bound(val); instead of upper_bound(ALL(width),val);
This issue was already discussed on codeforces several times. The problem is that
upper_bound
andlower_bound
require random-access iterators. Only in that case they work in O(logN). If iterators are not random-access (like instd::set
) these functions work in O(N) time. Usewidth.upper_bound(val)
instead ofupper_bound(ALL(width),val)
— it will work in O(logN) timeUse
myset.lowerbound(myset.begin(), myset.end())
O(logn)instead
std::lowerbound(myset.begin(), myset.end())
O(n)