Problem p02692 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vii;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb push_back
#define odd(x) ((x)&1)
#define even(x) (!odd(x))
#define ff it->first
#define ss it->second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (auto i = 0; i < n; ++i)
#define LOOP_IT(it, s) for (auto it = (s).begin(); it != (s).end(); ++it)
#define FASTIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define TEST_CASE                                                              \
  int tc;                                                                      \
  cin >> tc;                                                                   \
  while (tc--)

#ifdef LOCAL
#define cerr cout
#else
#endif

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif

int main(int argv, char *argc[]) {
  FASTIO

  freopen("in.txt", "r", stdin);
  // freopen("out.txt","w",stdout);

  int n, v[3];
  bool f = 1;
  cin >> n >> v[0] >> v[1] >> v[2];
  vector<set<int>> mp(3);
  vector<char> res(n);
  vector<string> s(n);
  rep(i, n) {
    cin >> s[i];
    int x = s[i][0] - 'A', y = s[i][1] - 'A';
    mp[x].insert(i);
    mp[y].insert(i);
  }

  rep(i, n) {
    int x = s[i][0] - 'A', y = s[i][1] - 'A';
    if (v[x] > v[y]) {
      ++v[y], --v[x];
      res[i] = (s[i][1]);
    } else if (v[x] == 1 && v[y] == 1) {
      auto it1 = mp[x].upper_bound(i);
      auto it2 = mp[y].upper_bound(i);
      if ((it2 == mp[y].end() || *it1 < *it2) && (it1 != mp[x].end())) {
        ++v[x], --v[y];
        res[i] = (s[i][0]);
      } else {
        --v[x], ++v[y];
        res[i] = (s[i][1]);
      }
    } else {
      --v[y], ++v[x];
      res[i] = (s[i][0]);
    }

    if (v[x] < 0 || v[y] < 0) {
      f = 0;
      break;
    }
  }

  if (f) {
    cout << "Yes\n";
    for (auto i : res)
      cout << i << "\n";
  } else {
    cout << "No\n";
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vii;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb push_back
#define odd(x) ((x)&1)
#define even(x) (!odd(x))
#define ff it->first
#define ss it->second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (auto i = 0; i < n; ++i)
#define LOOP_IT(it, s) for (auto it = (s).begin(); it != (s).end(); ++it)
#define FASTIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define TEST_CASE                                                              \
  int tc;                                                                      \
  cin >> tc;                                                                   \
  while (tc--)

#ifdef LOCAL
#define cerr cout
#else
#endif

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif

int main(int argv, char *argc[]) {
  FASTIO

  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);

  int n, v[3];
  bool f = 1;
  cin >> n >> v[0] >> v[1] >> v[2];
  vector<set<int>> mp(3);
  vector<char> res(n);
  vector<string> s(n);
  rep(i, n) {
    cin >> s[i];
    int x = s[i][0] - 'A', y = s[i][1] - 'A';
    mp[x].insert(i);
    mp[y].insert(i);
  }

  rep(i, n) {
    int x = s[i][0] - 'A', y = s[i][1] - 'A';
    if (v[x] > v[y]) {
      ++v[y], --v[x];
      res[i] = (s[i][1]);
    } else if (v[x] == 1 && v[y] == 1) {
      auto it1 = mp[x].upper_bound(i);
      auto it2 = mp[y].upper_bound(i);
      if ((it2 == mp[y].end() || *it1 < *it2) && (it1 != mp[x].end())) {
        ++v[x], --v[y];
        res[i] = (s[i][0]);
      } else {
        --v[x], ++v[y];
        res[i] = (s[i][1]);
      }
    } else {
      --v[y], ++v[x];
      res[i] = (s[i][0]);
    }

    if (v[x] < 0 || v[y] < 0) {
      f = 0;
      break;
    }
  }

  if (f) {
    cout << "Yes\n";
    for (auto i : res)
      cout << i << "\n";
  } else {
    cout << "No\n";
  }

  return 0;
}