Problem p00729 (C++) — buggy submission, verdict: Memory Limit Exceeded

// 14:17
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

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

#define all(v) v.begin(), v.end()
#define INF 1 << 30
#define mp make_pair
#define pb push_back

#define fi first
#define se second

#define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vii;

typedef long long ll;
int per[10100][1000];
int ln[10100]; //???????????¨????????°?????°
int main() {

  // stu[m] [0]?????? t[1]PC n[2]?????°?????? s
  while (1) {
    vii stu[10100];
    int n, m;
    int r;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    cin >> r;
    rep(i, m) {
      ln[i] = 0;
      rep(j, 1000) { per[i][j] = 0; }
    }
    rep(i, r) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      m -= 1;
      stu[m].pb(make_pair(t, s));
      ln[m]++;
    }
    rep(i, m) {
      int ms = 0;
      int p = 0;
      rep(j, ln[i]) {
        if (stu[i][j].se == 1) {

          if (ms == 0) {
            per[i][p++] = stu[i][j].fi;
          }
          ms++;
        } else if (stu[i][j].se == 0) {
          if (ms == 1) {
            per[i][p++] = stu[i][j].fi;
          }
          ms--;
        }
      }
      ln[i] = p;
    }
    // 	cout << "debug" << endl;
    // 	rep(i,m){
    // 	rep(j,ln[i]){
    // 		cout << "j:"<<j<<""<< per[i][j] << " ";
    // 	}
    // 	cout << endl;
    // }
    // cout << endl;

    int q;
    cin >> q;
    rep(i, q) {
      int ans = 0;
      int ts, te, m;
      cin >> ts >> te >> m;
      m--;
      for (int j = 0; j < ln[m] / 2; j++) {
        ans += max(0, min(te, per[m][j * 2 + 1]) - max(ts, per[m][j * 2]));
      }
      // cout << "ans" << endl;
      cout << ans << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

// 14:17
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

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

#define all(v) v.begin(), v.end()
#define INF 1 << 30
#define mp
#define pb push_back

#define fi first
#define se second

#define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vp;

typedef long long ll;
int per[10100][1010];
int stu[10100][1010][3];
int ln[10100]; //???????????¨????????°?????°
int main() {

  // stu[m] [0]?????? t[1]PC n[2]?????°?????? s
  while (1) {
    int n, m;
    int r;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    cin >> r;
    rep(i, m) {
      ln[i] = 0;
      rep(j, 1010) {
        per[i][j] = 0;
        rep(k, 3) { stu[i][j][k] = 0; }
      }
    }
    rep(i, r) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      m -= 1;
      stu[m][ln[m]][0] = t;
      stu[m][ln[m]][1] = n;
      stu[m][ln[m]][2] = s;
      ln[m]++;
    }
    rep(i, m) {
      int ms = 0;
      int p = 0;
      rep(j, ln[i]) {
        if (stu[i][j][2] == 1) {

          if (ms == 0) {
            per[i][p++] = stu[i][j][0];
          }
          ms++;
        } else if (stu[i][j][2] == 0) {
          if (ms == 1) {
            per[i][p++] = stu[i][j][0];
          }
          ms--;
        }
      }
      ln[i] = p;
    }
    // 	cout << "debug" << endl;
    // 	rep(i,m){
    // 	rep(j,ln[i]){
    // 		cout << "j:"<<j<<""<< per[i][j] << " ";
    // 	}
    // 	cout << endl;
    // }
    // cout << endl;

    int q;
    cin >> q;
    rep(i, q) {
      int ans = 0;
      int ts, te, m;
      cin >> ts >> te >> m;
      m--;
      for (int j = 0; j < ln[m] / 2; j++) {
        ans += max(0, min(te, per[m][j * 2 + 1]) - max(ts, per[m][j * 2]));
      }
      // cout << "ans" << endl;
      cout << ans << endl;
    }
  }
  return 0;
}