Problem p00768 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void swapp(int *, int *);
int main() {
  while (1) {
    int M, T, P, R;
    cin >> M >> T >> P >> R;
    if (M == 0 && T == 0 && P == 0 && R == 0)
      break;
    int te[51];
    int tr[51];
    int tp[51] = {};
    int tt[51] = {};
    vector<vector<int>> ts(T + 1, vector<int>(P, 0));
    for (int i = 0; i < R; i++) {
      int m, t, p, j;
      cin >> m >> t >> p >> j;
      if (j == 0) {
        tt[t] += ts[t][p] + m;
        tp[t]++;
      } else {
        ts[t][p] += 20;
      }
    }
    for (int i = 1; i <= T; i++) {
      //
      // cout << i<<" "<<tp[i]<< " "<< tt[i]<<endl;
      //
      tr[i] = i;
      te[i] = 0;
    }
    // sort
    for (int i = 1; i <= T; i++) {
      for (int j = 1; j < T; j++) {
        if (tp[j] < tp[j + 1]) {
          //  cout << tr[j]<<" "<<tr[j+1]<<endl;
          swapp(tr + j, tr + j + 1);
          swapp(te + j, te + j + 1);
          swapp(tt + j, tt + j + 1);
          swapp(tp + j, tp + j + 1);
          //  cout << tr[j]<<" "<<tr[j+1]<<endl;
        } else if (tp[j] == tp[j + 1]) {
          if (tt[j] > tt[j + 1]) {
            swapp(tr + j, tr + j + 1);
            swapp(te + j, te + j + 1);
            swapp(tt + j, tt + j + 1);
            swapp(tp + j, tp + j + 1);
          } else if (tt[j] == tt[j + 1]) {
            if (tr[j] < tr[j + 1]) {
              swapp(tr + j, tr + j + 1);
              swapp(te + j, te + j + 1);
              swapp(tt + j, tt + j + 1);
              swapp(tp + j, tp + j + 1);
            }
            te[j] = 1;
          }
        }
      }
    }
    // for(int i=1;i<=T;i++)  cout << i<<" "<<tp[i]<< " "<< tt[i]<<endl;
    for (int i = 1; i <= T; i++) {

      cout << tr[i];
      if (i < T) {
        if (te[i] == 1) {
          cout << '=';
        } else
          cout << ',';
      }
    }
    cout << endl;
  }
  return 0;
}
void swapp(int *m, int *a) {
  int b;
  b = *m;
  *m = *a;
  *a = b;
  return;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void swapp(int *, int *);
int main() {
  while (1) {
    int M, T, P, R;
    cin >> M >> T >> P >> R;
    if (M == 0 && T == 0 && P == 0 && R == 0)
      break;
    int te[51];
    int tr[51];
    int tp[51] = {};
    int tt[51] = {};
    vector<vector<int>> ts(T + 1, vector<int>(P + 1, 0));
    for (int i = 0; i < R; i++) {
      int m, t, p, j;
      cin >> m >> t >> p >> j;
      if (j == 0) {
        tt[t] += ts[t][p] + m;
        tp[t]++;
      } else {
        ts[t][p] += 20;
      }
    }
    for (int i = 1; i <= T; i++) {
      //
      // cout << i<<" "<<tp[i]<< " "<< tt[i]<<endl;
      //
      tr[i] = i;
      te[i] = 0;
    }
    // sort
    for (int i = 1; i <= T; i++) {
      for (int j = 1; j < T; j++) {
        if (tp[j] < tp[j + 1]) {
          //  cout << tr[j]<<" "<<tr[j+1]<<endl;
          swapp(tr + j, tr + j + 1);
          swapp(te + j, te + j + 1);
          swapp(tt + j, tt + j + 1);
          swapp(tp + j, tp + j + 1);
          //  cout << tr[j]<<" "<<tr[j+1]<<endl;
        } else if (tp[j] == tp[j + 1]) {
          if (tt[j] > tt[j + 1]) {
            swapp(tr + j, tr + j + 1);
            swapp(te + j, te + j + 1);
            swapp(tt + j, tt + j + 1);
            swapp(tp + j, tp + j + 1);
          } else if (tt[j] == tt[j + 1]) {
            if (tr[j] < tr[j + 1]) {
              swapp(tr + j, tr + j + 1);
              swapp(te + j, te + j + 1);
              swapp(tt + j, tt + j + 1);
              swapp(tp + j, tp + j + 1);
            }
            te[j] = 1;
          }
        }
      }
    }
    // for(int i=1;i<=T;i++)  cout << i<<" "<<tp[i]<< " "<< tt[i]<<endl;
    for (int i = 1; i <= T; i++) {

      cout << tr[i];
      if (i < T) {
        if (te[i] == 1) {
          cout << '=';
        } else
          cout << ',';
      }
    }
    cout << endl;
  }
  return 0;
}
void swapp(int *m, int *a) {
  int b;
  b = *m;
  *m = *a;
  *a = b;
  return;
}