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

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
#define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define erep(i, m, n) for (int(i) = (m); (i) <= (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrev(i, m, n) for (int(i) = (n)-1; (i) >= (m); (i)--)
#define erev(i, m, n) for (int(i) = (n); (i) >= (m); (i)--)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define vrep(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
template <class T, class S> inline pair<T, S> mp(T x, S y) {
  return make_pair(x, y);
}
template <class T, class S> inline bool minup(T &m, S x) {
  return m > (T)x ? (m = (T)x, true) : false;
}
template <class T, class S> inline bool maxup(T &m, S x) {
  return m < (T)x ? (m = (T)x, true) : false;
}

static const int INF = 1000000000;
static const ll MOD = 1000000007LL;
static const double EPS = 1E-12;

const int MAX_M = 11111;
const int MAX_R = 1111;
const int MAX_T = 800;
int id[MAX_M][MAX_T];
int N, M;
int r, q;
struct _R {
  int t, m, n, s;
} R[MAX_R];
int ts, te, m;

int main() {
  while (cin >> N >> M, N) {
    rep(i, M) rep(j, 777) id[i][j] = 0;
    cin >> r;
    rep(i, r) cin >> R[i].t >> R[i].n >> R[i].m >> R[i].s;
    rep(i, r) if (!R[i].s) R[i].s = -1;

    rep(i, r) id[R[i].m - 1][R[i].t - 540] += R[i].s;
    rep(i, M) rrep(j, 1, 777) id[i][j] += id[i][j - 1];

    cin >> q;
    rep(i, q) {
      cin >> ts >> te >> m;
      int res = 0;
      rrep(j, ts, te) if (id[m - 1][j - 540]) res += 1;
      cout << res << endl;
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
#define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define erep(i, m, n) for (int(i) = (m); (i) <= (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrev(i, m, n) for (int(i) = (n)-1; (i) >= (m); (i)--)
#define erev(i, m, n) for (int(i) = (n); (i) >= (m); (i)--)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define vrep(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
template <class T, class S> inline pair<T, S> mp(T x, S y) {
  return make_pair(x, y);
}
template <class T, class S> inline bool minup(T &m, S x) {
  return m > (T)x ? (m = (T)x, true) : false;
}
template <class T, class S> inline bool maxup(T &m, S x) {
  return m < (T)x ? (m = (T)x, true) : false;
}

static const int INF = 1000000000;
static const ll MOD = 1000000007LL;
static const double EPS = 1E-12;

const int MAX_M = 11111;
const int MAX_R = 1111;
const int MAX_Q = 55;
const int MAX_T = 2000;
int id[MAX_M][MAX_T];
int N, M;
int r, q;
struct _R {
  int t, n, m, s;
} R[MAX_R];
struct _Q {
  int ts, te, m;
} Q[MAX_Q];

int main() {
  while (cin >> N >> M, N) {
    rep(i, M) rrep(j, 530, 1270) id[i][j] = 0;
    cin >> r;
    rep(i, r) cin >> R[i].t >> R[i].n >> R[i].m >> R[i].s;
    rep(i, r) if (!R[i].s) R[i].s = -1;
    cin >> q;
    rep(i, q) cin >> Q[i].ts >> Q[i].te >> Q[i].m;

    rep(i, r) id[R[i].m - 1][R[i].t] = R[i].s;
    rep(i, M) rrep(j, 535, 1266) id[i][j] += id[i][j - 1];

    rep(i, q) {
      int res = 0;
      rrep(j, Q[i].ts, Q[i].te) if (id[Q[i].m - 1][j]) res += 1;
      cout << res << endl;
    }
  }

  return 0;
}