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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD7 1000000007
#define MOD9 1000000009

#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, n) for (int i = (a); i <= (n); i++)
#define all(a) (a).begin(), (a).end()

using namespace std;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};

int nextInt() {
  int a;
  cin >> a;
  return a;
}
char nextChar() {
  char a;
  cin >> a;
  return a;
}
double nextDouble() {
  double a;
  cin >> a;
  return a;
}
string nextString() {
  string a;
  cin >> a;
  return a;
}

void inputVector(vector<int> &v, int &n) {
  rep(i, n) { v.push_back(nextInt()); }
}
void inputVector(vector<double> &v, int &n) {
  rep(i, n) { v.push_back(nextDouble()); }
}
void inputVector(vector<string> &v, int &n) {
  rep(i, n) { v.push_back(nextString()); }
}

int memo[1270][10010];

signed main() {
  int N, M;
  while (cin >> N >> M) {
    if (!N)
      break;

    memset(memo, 0, sizeof(memo));

    int r;
    cin >> r;
    rep(i, r) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      m--;

      if (s == 0)
        memo[t][m]--;
      else
        memo[t][m]++;
    }
    REP(i, 500, 1265) {
      rep(j, M) { memo[i][j] += memo[i - 1][j]; }
    }
    REP(i, 500, 1265) {
      rep(j, M) { memo[i][j] = memo[i][j] > 0; }
    }
    REP(i, 500, 1265) {
      rep(j, M) { memo[i][j] += memo[i - 1][j]; }
    }

    int q;
    cin >> q;
    rep(i, q) {
      int ts, te, m;
      cin >> ts >> te >> m;
      m--;

      cout << memo[te - 1][m] - memo[ts - 1][m] << endl;
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define int long long
#define MOD7 1000000007
#define MOD9 1000000009

#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, n) for (int i = (a); i <= (n); i++)
#define all(a) (a).begin(), (a).end()

using namespace std;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};

int nextInt() {
  int a;
  cin >> a;
  return a;
}
char nextChar() {
  char a;
  cin >> a;
  return a;
}
double nextDouble() {
  double a;
  cin >> a;
  return a;
}
string nextString() {
  string a;
  cin >> a;
  return a;
}

void inputVector(vector<int> &v, int &n) {
  rep(i, n) { v.push_back(nextInt()); }
}
void inputVector(vector<double> &v, int &n) {
  rep(i, n) { v.push_back(nextDouble()); }
}
void inputVector(vector<string> &v, int &n) {
  rep(i, n) { v.push_back(nextString()); }
}

int memo[1270][10010];

signed main() {
  int N, M;
  while (cin >> N >> M) {
    if (!N)
      break;

    memset(memo, 0, sizeof(memo));

    int r;
    cin >> r;
    rep(i, r) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      m--;

      if (s == 0)
        memo[t][m]--;
      else
        memo[t][m]++;
    }
    REP(i, 500, 1265) {
      rep(j, M) { memo[i][j] += memo[i - 1][j]; }
    }
    REP(i, 500, 1265) {
      rep(j, M) { memo[i][j] = memo[i][j] > 0; }
    }
    REP(i, 500, 1265) {
      rep(j, M) { memo[i][j] += memo[i - 1][j]; }
    }

    int q;
    cin >> q;
    rep(i, q) {
      int ts, te, m;
      cin >> ts >> te >> m;
      m--;

      cout << memo[te - 1][m] - memo[ts - 1][m] << endl;
    }
  }

  return 0;
}