Question regarding the count function of std::multiset

Revision en2, by lucky_clover_, 2025-01-21 09:06:17

Today I was attempting to hack this submission for 2061B. In this solution, the code uses s1.count(x) inside the input-reading loop. Since the time complexity of multiset's count function is $$$O(\log n + k)$$$, where $$$k$$$ is the number of occurrences of the element, the code's time complexity should become $$$O(n^2)$$$ if all elements $$$a_i$$$ are set to $$$1$$$.

However, when I submitted the hack, this code only took 109 ms. Local testing showed that it indeed results in a TLE without -O2, but runs very quickly with -O2 enabled. Does this indicate that the compiler optimizes std::multiset.count() when it is used as boolean?

Tags multiset, time complexity, o2, boolean

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English lucky_clover_ 2025-01-21 09:06:17 0 (published)
en1 English lucky_clover_ 2025-01-21 08:47:32 791 Initial revision (saved to drafts)