Problem p00768 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <utility>
#include <vector>
using P = std::pair<int, int>;
using namespace std;

struct team_data {
  int team_num = -1;
  int solved_num = 0;
  int sum_time = 0; // minute
  // vector<int> solved_time;
  vector<int> penalty;
};

int main() {
  int M, T, P, R;
  while (cin >> M >> T >> P >> R, M) {
    vector<team_data> v(T);
    for (int i = 0; i < v.size(); ++i) {
      v[i].team_num = i + 1;
      v[i].penalty.resize(P);
    }
    for (int i = 0; i < R; ++i) {
      int m, t, p, j;
      cin >> m >> t >> p >> j;
      if (j != 0) {
        v[t - 1].penalty[p - 1] += 20;
      } else {
        v[t - 1].sum_time += m;
        v[t - 1].solved_num += 1;
        v[t - 1].sum_time += v[t - 1].penalty[p - 1];
      }
    }
    vector<vector<team_data>> v2(P);
    for (auto &x : v) {
      v2[x.solved_num].push_back(x);
    }
    for (auto &x : v2) {
      sort(x.begin(), x.end(), [](team_data const &t1, team_data const &t2) {
        return t1.team_num > t2.team_num;
      });
      stable_sort(x.begin(), x.end(),
                  [](team_data const &t1, team_data const &t2) {
                    return t1.sum_time < t2.sum_time;
                  });
    }
    vector<team_data> ret;
    for (int i = v2.size() - 1; i >= 0; --i) {
      for (auto &x2 : v2[i]) {
        ret.push_back(x2);
      }
    }
    for (int i = 0; i < ret.size(); ++i) {
      if (i != 0) {
        if (ret[i - 1].solved_num == ret[i].solved_num) {
          if (ret[i - 1].sum_time == ret[i].sum_time)
            cout << '=';
          else
            cout << ',';
        } else {
          cout << ',';
        }
      }
      cout << ret[i].team_num;
    }
    cout << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <utility>
#include <vector>
using P = std::pair<int, int>;
using namespace std;

struct team_data {
  int team_num = -1;
  int solved_num = 0;
  int sum_time = 0; // minute
  // vector<int> solved_time;
  vector<int> penalty;
};

int main() {
  int M, T, P, R;
  while (cin >> M >> T >> P >> R, M) {
    vector<team_data> v(T);
    for (int i = 0; i < v.size(); ++i) {
      v[i].team_num = i + 1;
      v[i].penalty.resize(P);
    }
    for (int i = 0; i < R; ++i) {
      int m, t, p, j;
      cin >> m >> t >> p >> j;
      if (j != 0) {
        v[t - 1].penalty[p - 1] += 20;
      } else {
        v[t - 1].sum_time += m;
        v[t - 1].solved_num += 1;
        v[t - 1].sum_time += v[t - 1].penalty[p - 1];
      }
    }
    vector<vector<team_data>> v2(P + 1);
    for (auto &x : v) {
      v2[x.solved_num].push_back(x);
    }
    for (auto &x : v2) {
      sort(x.begin(), x.end(), [](team_data const &t1, team_data const &t2) {
        return t1.team_num > t2.team_num;
      });
      stable_sort(x.begin(), x.end(),
                  [](team_data const &t1, team_data const &t2) {
                    return t1.sum_time < t2.sum_time;
                  });
    }
    vector<team_data> ret;
    for (int i = v2.size() - 1; i >= 0; --i) {
      for (auto &x2 : v2[i]) {
        ret.push_back(x2);
      }
    }
    for (int i = 0; i < ret.size(); ++i) {
      if (i != 0) {
        if (ret[i - 1].solved_num == ret[i].solved_num) {
          if (ret[i - 1].sum_time == ret[i].sum_time)
            cout << '=';
          else
            cout << ',';
        } else {
          cout << ',';
        }
      }
      cout << ret[i].team_num;
    }
    cout << endl;
  }
}