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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define repp(i, k, n) for (int i = k; i < (int)n; i++)
#define re(n) for (int i = 0; i < (int)n; i++)

string fizz(int now) {
  if (now % 15 == 0)
    return "FizzBuzz";
  if (now % 5 == 0)
    return "Buzz";
  if (now % 3 == 0)
    return "Fizz";
  stringstream st;
  st << now;
  return st.str();
}

int main() {
  int m, n;
  while (cin >> m >> n && n) {
    vector<int> a;
    re(m) a.push_back(i + 1);
    vector<int>::iterator it = a.begin();

    for (int now = 1; now <= n; now++) {
      string st;
      cin >> st;
      if (a.size() >= 2 && st != fizz(now)) {
        it = a.erase(it);
        if (it == a.end())
          it = a.begin();
      } else {
        if (it == a.end() - 1)
          it = a.begin();
        else
          it++;
      }
    }
    for (int i = 0; i < a.size(); i++)
      cout << (i == 0 ? "" : " ") << a[i];
    cout << endl;
  }
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <iostream>

#include <vector>

#include <algorithm>

#include <sstream>

#include <string>

#include <cmath>

#include <list>

using namespace std;

template <typename T>
class SegTree

{

public:
  int n;

  T T_max;

  vector<T> dat;

  SegTree(int n_, T t_max)

  {

    int n = 1;

    T_max = t_max;

    while (n < n_)
      n *= 2;

    for (int i = 0; i < 2 * n - 1; i++)
      dat.push_back(t_max);
  }

  void update(int k, T a)

  {

    k += n - 1;

    dat[k] = a;

    while (k > 0)

    {

      k = (k - 1) / 2;

      dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  //[a,b)

  T query(int a, int b)

  {

    return _query(a, b, 0, 0, n);
  }

private:
  T _query(int a, int b, int k, int l, int r)

  {

    if (r <= a || b <= l)
      return T_max;

    if (a <= l && r <= b)
      return dat[k];

    else

    {

      T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2);

      T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);

      return min(vr, vl);
    }
  }
};

string hoge(int a)

{

  if (a % 15 == 0)

  {

    return "FizzBuzz";

  }

  else if (a % 3 == 0)

  {

    return "Fizz";

  }

  else if (a % 5 == 0)

  {

    return "Buzz";
  }

  stringstream s;

  s << a;

  return s.str();
}

int main()

{

  int m, n;

  while (cin >> m >> n && (m != 0 || n != 0))

  {

    list<int> l;

    for (int i = 1; i <= m; i++)

    {

      l.push_back(i);
    }

    list<int>::iterator it = l.begin();

    for (int i = 1; i <= n; i++)

    {

      if (it == l.end())

      {

        it = l.begin();
      }

      string a;

      getline(cin, a);

      if (a != hoge(i))

      {

        it = l.erase(it);

      }

      else
        it++;
    }

    while (it != l.begin())

    {

      it--;
    }

    for (int i = 0; i < l.size(); i++)

    {

      cout << *it;

      it++;

      if (i != l.size() - 1)

      {

        cout << " ";
      }
    }

    cout << endl;
  }
}