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

#include <iostream>
#include <list>
#include <vector>
using namespace std;

int main() {

  int n, m, q;

  cin >> n >> m >> q;

  int size = n;

  bool table[n];

  vector<int> vec;
  list<int> l;

  for (int i = 0; i < n; i++) {
    table[i] = true;
    vec.push_back(i);
    l.push_back(i);
  }

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

  for (int i = 0; i < m; i++) {
    int tmp = 0, j = 0;
    cin >> tmp;
    /*for(list<int>::iterator i = l.begin();i!=l.end();i++){
      cout << *i << " ";
    }
    cout << endl;*/
    if (tmp % 2 == 0) {
      while (j != tmp) {
        index++;
        if (index == l.end()) {
          index = l.begin();
        }

        j++;
        // cout << j << " " << *index<<endl;
      }
      table[*index] = false;
      list<int>::iterator tmp = index;
      index++;
      if (index == l.end()) {
        index = l.begin();
      }
      l.erase(tmp);

    } else {
      while (j != tmp) {
        if (index == l.begin()) {
          index = l.end();
          index--;
        } else {
          index--;
        }
        j++;
      }
      table[*index] = false;
      list<int>::iterator tmp = index;
      index++;
      if (index == l.end()) {
        index = l.begin();
      }
      l.erase(tmp);
    }
    /*for(int i=0;i < n;i++){
      cout << table[i] << " ";
    }
    cout << endl;*/
  }

  index = l.begin();

  /*for(int i=0;i < l.size();i++){
    table[*index] = true;
  }*/
  // cout << "ok" << endl;
  for (int i = 0; i < q; i++) {
    int ansindex, ans;
    cin >> ansindex;
    ans = (table[ansindex]) ? 1 : 0;
    cout << ans << endl;
  }

  return 0;
}

/*
     0
        1
9          2
8            3
  7        4
    6    5







*/

Error: -6
double free or corruption (out)


Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {

  int n, m, q;

  cin >> n >> m >> q;

  int size = n;

  bool table[n];

  vector<int> vec;

  for (int i = 0; i < n; i++) {
    table[i] = false;
    vec.push_back(i);
  }

  int index = 0;

  for (int i = 0; i < m; i++) {
    int tmp = 0;
    cin >> tmp;
    if (tmp % 2 == 0) {
      /*while(j!=tmp){
        do{
          index++;
          if(index==n){
            index = 0;
          }
        }while(!table[index]);

        j++;
      }*/
      index += tmp;
      if (index > vec.size() - 1) {
        index = index % vec.size();
      }
      vec.erase(vec.begin() + index);
      /*table[index] = false;
      do{
        index++;
        if(index==n){
          index = 0;
        }
      }while(!table[index]);*/
      index++;
      if (index == n) {
        index = 0;
      }

    } else {
      /*while(j!=tmp){
        do{
          index--;
          if(index==-1){
            index = n-1;
          }
        }while(!table[index]);
        j++;
      }*/
      // table[index] = false;
      index -= tmp;

      while (index < 0) {
        index += vec.size();
      }
      vec.erase(vec.begin() + index - 1);

      /*do{
        index++;
        if(index==n){
          index = 0;
        }
      }while(!table[index]);*/
      index++;
      if (index == vec.size()) {
        index = 0;
      }
    }
    for (int i = 0; i < vec.size(); i++) {
      cout << vec[i] << " ";
    }
    cout << endl;
  }

  for (int i = 0; i < vec.size(); i++) {
    table[vec[i]] = true;
  }
  // cout << "ok" << endl;
  for (int i = 0; i < q; i++) {
    int ansindex, ans;
    cin >> ansindex;
    ans = (table[ansindex]) ? 1 : 0;
    cout << ans << endl;
  }

  return 0;
}