How to optimize this simple algorithm further? : <p><i>Note: this is a programming challenge</i>
<hr>
This challenge requires usage of <code>std::set</code>.</p>

<p><b>Input</b></p>

<ul>
<li>A number <code>n</code></li>
<li><code>n</code> lines with each <code>j</code> and <code>k</code></li>
</ul>

<p>Sample input:</p>

<pre><code>5
1 4
2 1
1 6
3 4
1 2
</code></pre>

<p><code>j</code> is for the operation: <code>1</code> to insert <code>k</code>, <code>2</code> to delete <code>k</code> (if there is a <code>k</code>), <code>3</code> find <code>k</code></p>

<p>For <code>j == 3</code>, output <code>Yes</code> or <code>No</code> if <code>k</code> is in the <code>std::set</code>.
<hr>
I made different versions of the algorithm, but all are way too slow. I tried different <code>std</code> functions, but <code>std::find</code> seems the fastest one, but is still too slow. Implementing my own would be even worse, so maybe I missed a function from the library. I really have no idea how to optimize it further. Currently I have this:</p>

<pre><code>int main()
{
    std::set&lt;int&gt; set{};
    int Q = 0;
    std::cin &gt;&gt; Q;

    int type = 0;
    int x = 0;
    for (int i = 0; i &lt; Q; ++i)
    {
        std::cin &gt;&gt; type;
        std::cin &gt;&gt; x;

        if (type == 1)
            set.insert(x);
        else if (type == 2)
            set.erase(x);
        else if (type == 3)
            std::cout &lt;&lt; (std::find(std::begin(set), std::end(set), x) != std::end(set) ? "Yes" : "No") &lt;&lt; '\n';
            //Condition may be std::find, std::count or std::binary_search
    }

    return 0;
}
</code></pre>

<p><hr>
The challenge requires it to run under 2 seconds. Here are the results of mine:</p>

<pre><code>Function              Time           % over
std::find          -&gt; 7.410s         370.50%
std::count         -&gt; 7.881s         394.05%
std::binary_search -&gt; 14.050s        702.50%
</code></pre>

<p>As you can see my algorithm is 3x slower than the required algorithm. The bottlenecks are clearly those functions:</p>

<pre><code>Function               % of total time 
std::find          -&gt;  77.61%
std::count         -&gt;  80.80%
std::binary_search -&gt;  87.59%
</code></pre>

<p>But currently I have no better idea than to use <code>std::find</code> or similar functions. Does someone have a way/idea to optimize my code further? Or are there some obvious bottlenecks that I'm missing?</p>
