Problem p02457 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cassert>

#include <ccomplex>
#include <cctype>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <cinttypes>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstdbool>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <ctime>
#include <cwchar>
#include <cwctype>

// C++
#include <algorithm>
#include <array>
#include <atomic>
#include <bitset>
#include <chrono>
#include <complex>
#include <condition_variable>
#include <deque>
#include <exception>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
// #include <boost/foreach.hpp>
// #include <boost/range/algorithm.hpp>
#define rep(i, j, k) for (int i = (int)j; i < (int)k; i++)
#define ll long long
#define Sort(v) sort(all(v))
#define INF 1000000000
#define END return 0
#define pb push_back
#define se second
#define fi first
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define MP make_pair
#define MOD 1000000007LL
using namespace std;
int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main() {
  int q;
  cin >> q;
  set<int> st;
  rep(i, 0, q) {
    int a, b;
    cin >> a >> b;
    if (a == 0) {
      st.insert(b);
      cout << st.size() << endl;
    } else if (a == 1) {
      cout << ((st.find(b) == st.end()) ? 0 : 1) << endl;
    } else if (a == 2) {
      st.erase(b);
    } else {
      int r;
      cin >> r;
      auto it = st.lower_bound(b);
      for (; it != st.end() && *it <= r; it++) {
        cout << *it << endl;
      }
    }
  }
}

/*

*/


Error: TLE

Fixed submission:

#include <cassert>

#include <ccomplex>
#include <cctype>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <cinttypes>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstdbool>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <ctime>
#include <cwchar>
#include <cwctype>

// C++
#include <algorithm>
#include <array>
#include <atomic>
#include <bitset>
#include <chrono>
#include <complex>
#include <condition_variable>
#include <deque>
#include <exception>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
// #include <boost/foreach.hpp>
// #include <boost/range/algorithm.hpp>
#define rep(i, j, k) for (int i = (int)j; i < (int)k; i++)
#define ll long long
#define Sort(v) sort(all(v))
#define INF 1000000000
#define END return 0
#define pb push_back
#define se second
#define fi first
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define MP make_pair
#define MOD 1000000007LL
using namespace std;
int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main() {
  int q;
  cin >> q;
  set<int> st;
  rep(i, 0, q) {
    int a, b;
    cin >> a >> b;
    if (a == 0) {
      st.insert(b);
      cout << st.size() << endl;
    } else if (a == 1) {
      cout << ((st.find(b) == st.end()) ? 0 : 1) << endl;
    } else if (a == 2) {
      st.erase(b);
    } else {
      int r;
      cin >> r;
      auto it = lower_bound(all(st), b);
      for (; it != st.end() && *it <= r; it++) {
        cout << *it << endl;
      }
    }
  }
}

/*

*/