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

#include <algorithm>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>

#define FOR(k, m, n) for (int(k) = (m); (k) < (n); (k)++)
#define REP(i, n) FOR((i), 0, (n))
#define LL long long
#define CLR(a) memset((a), 0, sizeof(a))
#define SZ(x) (int((x).size()))
#define WAITING(str)                                                           \\
  int str;                                                                     \\
  std::cin >> str;
#define DEBUGING(str) cout << str << endl
using namespace std;

const LL MOD = 1000000007; // 10^9+7
const int INF = (1 << 30);
const int MAX = 510000;

// 変数
LL n;
vector<LL> x;
vector<bool> exist;
bool bigThere = false;

// サブ関数
// 入力
void input() {
  cin >> n;
  exist.resize(MAX);
}

// 枝刈
int failNo() {
  static int id = 0;
  while (exist[id + 1])
    id++;
  return id;
}

bool isSafeFail(int now) {
  if (bigThere)
    return false;

  FOR(i, now + 1, MAX) {
    if (exist[i])
      return false;
  }
  return true;
}

void insert(vector<bool> &exist, LL now) {
  if (exist[now]) {
    exist[now] = false;
    insert(exist, now - 1);
  } else {
    exist[now] = true;
  }
}

// 計算
void calc() {
  REP(i, n) {
    LL now;
    cin >> now;
    if (n < now)
      bigThere = true;

    if (exist[0] || (now <= failNo() && !isSafeFail(now))) {
      if (n < now)
        bigThere = true;
      cout << "No" << endl;
    } else {
      if (now <= n) {
        insert(exist, now);
      }
      cout << "Yes" << endl;
    }
  }
}

// 出力
void output() {}

// デバッグ
void debug() {
  int N;
  cin >> N;
}

// メイン関数
int main() {
  input();
  calc();
  // output();
  // debug();

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>

#define FOR(k, m, n) for (int(k) = (m); (k) < (n); (k)++)
#define REP(i, n) FOR((i), 0, (n))
#define LL long long
#define CLR(a) memset((a), 0, sizeof(a))
#define SZ(x) (int((x).size()))
#define WAITING(str)                                                           \\
  int str;                                                                     \\
  std::cin >> str;
#define DEBUGING(str) cout << str << endl
using namespace std;

const LL MOD = 1000000007; // 10^9+7
const int INF = (1 << 30);

// 変数
LL n;
vector<LL> x;
set<LL> exist;

// サブ関数
// 入力
void input() {
  cin >> n;
  /*
  REP(i, n) {
          LL tmp;
          cin >> tmp;
          x.push_back(tmp);
  }
  */
}

bool isYes_impl(const set<LL> &exist, const LL &now, const LL &start) {
  if (now == 0) {
    for (auto num : exist) {
      if (num > start)
        return false;
    }
    return true;
  } else {
    if (exist.find(now) == exist.end()) {
      return true;
    } else {
      return isYes_impl(exist, now - 1, start);
    }
  }
}

bool isYes(const set<LL> exist, LL now) {
  if (exist.find(0) != exist.end())
    return false;
  return isYes_impl(exist, now, now);
}

void insert(set<LL> &exist, LL now) {
  auto itr = exist.find(now);
  if (itr == exist.end()) {
    exist.insert(now);
  } else {
    exist.erase(itr);
    insert(exist, now - 1);
  }
}

// 計算
void calc() {
  REP(i, n) {
    LL now;
    cin >> now;

    if (isYes(exist, now)) {
      insert(exist, now);
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}

// 出力
void output() {}

// デバッグ
void debug() {
  int N;
  cin >> N;
}

// メイン関数
int main() {
  input();
  calc();
  // output();
  debug();

  return 0;
}