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

#include <bits/stdc++.h>
#define REP(a, b, c) for (int a = b; a < (int)c; a++)
#define REPE(a, b, c) for (int a = b; a <= (int)c; a++)
#define REPD(a, b, c) for (int a = b; a > (int)c; a--)
#define rep(a, b) REP(a, 0, b)
#define repd(a, b) REPD(a, b, 0)
class _datas {
public:
  int m;
  int t;
  int p;
  int j;
};
class _score {
public:
  int p;
  int t;
  _score(int p, int t) {
    this->p = p;
    this->t = t;
  }
};
typedef long long int ll;
using namespace std;

int main() {
  while (true) {
    int M, T, P, R;
    cin >> M >> T >> P >> R;
    if (M == 0)
      break;
    vector<_datas> datas(R);
    rep(i, R) cin >> datas[i].m >> datas[i].t >> datas[i].p >> datas[i].j;
    // rep(i,R) cout << datas[i].m << " "<< datas[i].t << " "<< datas[i].p << "
    // "<< datas[i].j << " " << endl;
    vector<_score> scores(T + 1, _score(0, 0));
    REPE(i, 1, T) {
      vector<int> miss_cnt(P + 1, 0);
      rep(j, R) {
        _datas data = datas[j];
        if (data.t == i) {
          if (data.j == 0) {
            scores[i].p++;
            scores[i].t += data.m + miss_cnt[data.p] * 20;
          } else {
            miss_cnt[data.p]++;
          }
        }
      }
    }
    /*cout << "score" << endl;
    REPE(i,1,T){
      cout<<i<<":" << scores[i].p << " " << scores[i].t << endl;
      }*/
    _score b_score = _score(0, 0);
    REPE(i, 1, T) {
      int max_idx = 1;
      REPE(j, 2, T) {
        if (scores[j].p > scores[max_idx].p ||
            (scores[j].p == scores[max_idx].p &&
             scores[j].t <= scores[max_idx].t)) {
          max_idx = j;
        }
      }
      if (i != 1) {
        if (b_score.p == scores[max_idx].p && b_score.t == scores[max_idx].t)
          cout << "=";
        else
          cout << ",";
      }
      cout << max_idx;
      b_score = scores[max_idx];
      scores[max_idx] = _score(-1, 0);
    }
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(a, b, c) for (int a = b; a < (int)c; a++)
#define REPE(a, b, c) for (int a = b; a <= (int)c; a++)
#define REPD(a, b, c) for (int a = b; a > (int)c; a--)
#define rep(a, b) REP(a, 0, b)
#define repd(a, b) REPD(a, b, 0)
class _datas {
public:
  int m;
  int t;
  int p;
  int j;
};
class _score {
public:
  int p;
  int t;
  _score(int p, int t) {
    this->p = p;
    this->t = t;
  }
};
typedef long long int ll;
using namespace std;

int main() {
  while (true) {
    int M, T, P, R;
    cin >> M >> T >> P >> R;
    if (M == 0)
      break;
    vector<_datas> datas(R);
    rep(i, R) cin >> datas[i].m >> datas[i].t >> datas[i].p >> datas[i].j;
    // rep(i,R) cout << datas[i].m << " "<< datas[i].t << " "<< datas[i].p << "
    // "<< datas[i].j << " " << endl;
    vector<_score> scores(T + 1, _score(0, 0));
    REPE(i, 1, T) {
      vector<int> miss_cnt(P, 0);
      rep(j, R) {
        _datas data = datas[j];
        if (data.t == i) {
          if (data.j == 0) {
            scores[i].p++;
            scores[i].t += data.m + miss_cnt[data.p] * 20;
          } else {
            miss_cnt[data.p]++;
          }
        }
      }
    }
    /*cout << "score" << endl;
    REPE(i,1,T){
      cout<<i<<":" << scores[i].p << " " << scores[i].t << endl;
    }*/
    _score b_score = _score(0, 0);
    REPE(i, 1, T) {
      int max_idx = 1;
      REPE(j, 2, T) {
        if (scores[j].p > scores[max_idx].p ||
            (scores[j].p == scores[max_idx].p &&
             scores[j].t <= scores[max_idx].t)) {
          max_idx = j;
        }
      }
      if (i != 1) {
        if (b_score.p == scores[max_idx].p && b_score.t == scores[max_idx].t)
          cout << "=";
        else
          cout << ",";
      }
      cout << max_idx;
      b_score = scores[max_idx];
      scores[max_idx] = _score(-1, 0);
    }
    cout << endl;
  }
  return 0;
}