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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define pb push_back
#define mk make_pair
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define REP1(i, n) for (int(i) = 1; (i) <= (int)(n); (i)++)
#define fastio ios::sync_with_stdio(0), cin.tie(0)
#define PI 3.1415926535897932
const ll mod = 1000000007;
const int INF = 2123456789;
const double eps = 1e-7;

template <class T> T gcd(T a, T b) {
  if (!b)
    return a;
  return gcd(b, a % b);
}
const int maxn = 501000;
int now[maxn];

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  int n;
  scanf("%d", &n);
  int dep = 0, top = 0;
  bool full = false;
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    if (x >= maxn) {
      printf("Yes\\n");
      continue;
    } else if (full) {
      printf("No\\n");
      continue;
    } else {
      if (x < top) {
        printf("No\\n");
        continue;
      } else if (x == top && dep != top) {
        printf("No\\n");
        continue;
      } else {
        printf("Yes\\n");
        dep = max(dep, x);
        now[x]++;
        while (now[x] >= 2) {
          now[x] = 0;
          if (dep == x)
            dep--;
          x--;
          now[x]++;
          if (x == 0) {
            full = 1;
            break;
          }
        }
        for (int i = top + 1; now[i] == 1; i++) {
          top = i;
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define pb push_back
#define mk make_pair
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define fi first
#define se second
#define ALL(A) A.begin(), A.end()
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define REP1(i, n) for (int(i) = 1; (i) <= (int)(n); (i)++)
#define fastio ios::sync_with_stdio(0), cin.tie(0)
#define PI 3.1415926535897932
const ll mod = 1000000007;
const int INF = 2123456789;
const double eps = 1e-7;

template <class T> T gcd(T a, T b) {
  if (!b)
    return a;
  return gcd(b, a % b);
}
const int maxn = 5001000;
int now[maxn];

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  int n;
  scanf("%d", &n);
  int dep = 0, top = 0;
  bool flag = true;
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    bool cnt = true;
    if (x >= maxn && flag) {
      printf("Yes\\n");
      continue;
    }
    if (!flag || x < top || (x == top && x != dep)) {
      printf("No\\n");
      continue;
    }
    dep = max(dep, x);
    now[x]++;
    while (now[x] >= 2) {
      now[x] = 0;
      if (x == dep)
        dep--;
      x--;
      now[x]++;
      if (x == 0) {
        flag = false;
        break;
      }
    }
    printf("Yes\\n");
    for (int j = 1;; j++) {
      if (now[j] == 0)
        break;
      top = j;
    }
  }
  return 0;
}