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

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
bool tt[1300];
vector<P> student[10005];
struct LOG {
  int t, n, m, s;
};

int main() {
  //	cut here before submit
  //	freopen ("testcase.ALLR", "r", stdin );
  int N, M;
  while (scanf("%d %d", &N, &M), N, M) {
    rep(i, M) student[i].clear();
    int r;
    scanf("%d", &r);

    LOG log[r];
    rep(i, r) {
      int t, n, m, s;
      scanf("%d %d %d %d", &t, &n, &m, &s);
      n--;
      m--;
      log[i].t = t, log[i].n = n, log[i].m = m, log[i].s = s;
    } // end rep

    rep(i, r) {
      if (log[i].s == 0)
        continue;
      int cn = log[i].n, cm = log[i].m;
      int st = log[i].t;
      for (int j = i + 1; j < r; j++) {
        if (log[j].n == cn && log[j].m == cm && log[j].s == 0) {
          int et = log[j].t;
          student[cm].push_back(P(st, et));
          break;
        } // end if
      }   // end for
    }     // end rep

    int q;
    scanf("%d", &q);
    vector<int> ans(q, 0);

    rep(i, q) {
      int ts, te, m;
      scanf("%d %d %d", &ts, &te, &m);
      m--;
      memset(tt, false, sizeof(tt));
      if (!student[m].empty()) {
        rep(j, student[m].size()) {
          for (int t = student[m][j].first; t < student[m][j].second; t++) {
            tt[t] = true;
          } // end for
        }   // end rep
      }     // end if

      int res = 0;
      for (int t = ts; t < te; t++) {
        if (tt[t])
          res++;
      } // end if
      ans[i] = res;
    }                                     // end rep
    rep(i, q) { printf("%d\\n", ans[i]); } // end rep

  } // end loop

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
bool tt[1300];

int main() {
  //	cut here before submit
  //	freopen ("testcase.ALLR", "r", stdin );
  int N, M;
  while (scanf("%d %d", &N, &M), N, M) {
    vector<vector<vector<int>>> login(M, vector<vector<int>>(N));
    vector<vector<vector<int>>> logout(M, vector<vector<int>>(N));

    int r;
    scanf("%d", &r);
    rep(i, r) {
      int t, n, m, s;
      scanf("%d %d %d %d", &t, &n, &m, &s);
      n--;
      m--;
      if (s == 1) {
        login[m][n].push_back(t);
      } else {
        logout[m][n].push_back(t);
      } // end if
    }   // end rep

    int q;
    scanf("%d", &q);
    vector<int> ans(q, 0);

    rep(i, q) {
      int ts, te, m;
      scanf("%d %d %d", &ts, &te, &m);
      m--;
      memset(tt, false, sizeof(tt));
      rep(j, N) {
        if (!login[m][j].empty()) {
          rep(k, login[m][j].size()) {
            for (int l = login[m][j][k]; l < logout[m][j][k]; l++) {
              tt[l] = true;
            } // end for
          }   // end rep
        }     // end if
      }       // end rep
      int res = 0;
      for (int t = ts; t < te; t++) {
        if (tt[t])
          res++;
      } // end if
      ans[i] = res;
    }                                     // end rep
    rep(i, q) { printf("%d\\n", ans[i]); } // end rep

  } // end loop

  return 0;
}