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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (int(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (int(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (int(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int i = 0; i < (n); i++)                                              \\
      cerr << v[i] << ' ';                                                     \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int aaa = 0; aaa < i; aaa++) {                                        \\
      for (int bbb = 0; bbb < j; bbb++)                                        \\
        cerr << v[aaa][bbb] << ' ';                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

int mp[500010];
int cant, snum, si, cou;
bool par = true;

int main() {
  int n;
  cin >> n;

  REP(a, n) {
    int tmp;
    cin >> tmp;

    if (!par) {
      cout << "No" << endl;
      continue;
    }

    if (tmp > 500000) {
      cout << "Yes" << endl;
      cou++;
      continue;
    }

    if (tmp <= cant || tmp <= si) {
      cout << "No" << endl;
      continue;
    }

    mp[tmp]++;

    bool ok = true;
    bool tmpp = true;
    int ssi = 0, ssnum = 0;
    for (ll i = si + 1, k = 2, num = snum; i < 500001; i++) {
      k -= mp[i];
      num += mp[i];
      if (k < 0) {
        ok = false;
        break;
      }
      if (k == 0) {
        tmpp = false;
      }
      if (k == 1 && num < cou) {
        ssi = max(si, (int)i);
        ssnum = num;
      }
      if (k > 500000) {
        break;
      }
      k *= 2;
    }
    if (ok) {
      cout << "Yes" << endl;
      cou++;
      par &= tmpp;
      si = max(si, ssi);
      snum = max(snum, ssnum);
    } else {
      cout << "No" << endl;
      mp[tmp]--;
      cant = max(cant, tmp);
    }
    // cout << si << ' ' << snum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define DBG cerr << '!' << endl;
#define REP(i, n) for (int(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (int(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (int(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int i = 0; i < (n); i++)                                              \\
      cerr << v[i] << ' ';                                                     \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int aaa = 0; aaa < i; aaa++) {                                        \\
      for (int bbb = 0; bbb < j; bbb++)                                        \\
        cerr << v[aaa][bbb] << ' ';                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

int mp[500010];
int cant, snum, si, cou;
bool par = true;

int main() {
  int n;
  cin >> n;

  REP(a, n) {
    int tmp;
    cin >> tmp;

    if (!par) {
      cout << "No" << endl;
      continue;
    }

    if (tmp > 500000) {
      cout << "Yes" << endl;
      cou++;
      continue;
    }

    if (tmp <= cant) {
      cout << "No" << endl;
      continue;
    }

    mp[tmp]++;

    bool ok = true;
    bool tmpp = true;
    for (ll i = si + 1, k = 2; i < 500001; i++) {
      k -= mp[i];
      k *= 2;
      if (k < 0) {
        ok = false;
        break;
      }
      if (k == 0) {
        tmpp = false;
      }
      if (k == 1) {
        si = max(si, (int)i);
      }
      if (k > 500000) {
        break;
      }
    }
    if (ok) {
      cout << "Yes" << endl;
      cou++;
      par &= tmpp;
    } else {
      cout << "No" << endl;
      mp[tmp]--;
      cant = max(cant, tmp);
    }
  }

  return 0;
}