Problem p01905 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
/*{{{*/ // template
#define REP(i, n) for (int i = 0; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 29
#define LINF LLONG_MAX / 3
#define MP make_pair
#define PB push_back
#define pb push_back
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define all(v) ALL(v)
#define debug(x) cerr << #x << ":" << x << endl
#define debug2(x, y) cerr << #x << "," << #y ":" << x << "," << y << endl
#define CININIT cin.tie(0), ios::sync_with_stdio(false)
// struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double {
  double d;
  explicit Double(double x) : d(x) {}
};
ostream &operator<<(ostream &os, const Double x) {
  os << fixed << setprecision(20) << x.d;
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &st) {
  os << "[";
  for (auto a : st)
    cout << a << ",";
  os << "]";
}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

constexpr ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
/*}}}*/

int N, M;
vector<bool> ex;

int main() {
  cin >> N >> M;
  ex = vector<bool>(N, true);
  rep(i, M) {
    int a;
    cin >> a;
    ex[a] = false;
  }

  int NN = N - 1;
  vector<int> tree(N + 10, -1);
  int ans = 0;

  for (int i = NN - (N / 2), j = 0; i < NN; i++, j += 2) {
    int l = j;
    int r = j + 1;
    if (ex[l] == false and ex[r] == false) {
      continue;
    } else if (ex[l] == false or ex[r] == false) {
      tree[i] = 1;
    } else {
      tree[i] = 1;
      ans++;
    }
  }

  for (int i = NN - (N / 2) - 1; i >= 0; i--) {
    int l = i * 2 + 1;
    int r = i * 2 + 2;
    //        cerr << i << " " << l << " " << r << endl;
    if (tree[l] == -1 and tree[r] == -1) {
      tree[i] = -1;
    } else if (tree[l] == -1 or tree[r] == -1) {
      tree[i] = 1;
    } else {
      tree[i] = 1;
      ans++;
    }
  }

  // rep(i,NN){
  //     cout << i << " " << tree[i] << endl;
  // }

  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/*{{{*/ // template
#define REP(i, n) for (int i = 0; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 29
#define LINF LLONG_MAX / 3
#define MP make_pair
#define PB push_back
#define pb push_back
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define all(v) ALL(v)
#define debug(x) cerr << #x << ":" << x << endl
#define debug2(x, y) cerr << #x << "," << #y ":" << x << "," << y << endl
#define CININIT cin.tie(0), ios::sync_with_stdio(false)
// struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double {
  double d;
  explicit Double(double x) : d(x) {}
};
ostream &operator<<(ostream &os, const Double x) {
  os << fixed << setprecision(20) << x.d;
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &st) {
  os << "[";
  for (auto a : st)
    cout << a << ",";
  os << "]";
}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

constexpr ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
/*}}}*/

int N, M;
vector<bool> ex;

int main() {
  cin >> N >> M;
  ex = vector<bool>(N, true);
  rep(i, M) {
    int a;
    cin >> a;
    ex[a] = false;
  }

  int NN = N - 1;
  vector<int> tree(NN, -1);

  for (int i = NN - (N / 2), j = 0; i < NN; i++, j += 2) {
    int l = j;
    int r = j + 1;
    if (ex[l] == false or ex[r] == false) {
      continue;
    } else {
      tree[i] = true;
    }
  }

  for (int i = NN - (N / 2) - 1; i >= 0; i--) {
    int l = i * 2 + 1;
    int r = r * 2 + 2;
    if (tree[l] == -1 or tree[r] == -1) {
      tree[i] = -1;
    } else {
      tree[i] = 0;
    }
  }

  int ans = 0;
  for (int i = 0; i < NN; i++) {
    if (tree[i] != -1) {
      ans++;
    }
  }
  cout << ans << endl;
}