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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, l, n) for (int i = l; i < n; i++)
#define rer(i, l, n) for (int i = l; i <= n; i++)
#define rrep(i, n, l) for (int i = n - 1; i >= l; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define pb(a) push_back(a);
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef vector<lint> vli;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int d[10010][1261];
// int e[10010][1261];

int main() {
  int N, M, r;
  while (1) {
    cin >> N >> M;
    if (N + M == 0)
      break;
    cin >> r;
    memset(d, 0, sizeof(d));
    // memset(e,0,sizeof(e));
    int t, n, m, s;
    rep(i, 0, r) {
      cin >> t >> n >> m >> s;
      if (s == 1) {
        d[m][t]++;
      } else {
        d[m][t]--;
      }
    }
    rep(j, 1, M + 1) {
      rep(i, 541, 1261) { d[j][i] += d[j][i - 1]; }
    }
    rep(j, 1, M + 1) {
      rep(i, 540, 1261) {
        if (i == 540) {
          if (d[j][i] > 0)
            d[j][i] = 1;
          else
            d[j][i] = 0;
        } else {
          if (d[j][i] > 0)
            d[j][i] = d[j][i - 1] + 1;
          else
            d[j][i] = d[j][i - 1];
        }
        // if(j==1 && 777<=i && i<=900) cout<<j<<" "<<i<<" "<<e[j][i]<<endl;
      }
      // cout<<endl;
    }
    int q;
    cin >> q;
    int ts, te, mm;
    rep(i, 0, q) {
      cin >> ts >> te >> mm;
      o(d[mm][te - 1] - d[mm][ts - 1]);
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, l, n) for (int i = l; i < n; i++)
#define rer(i, l, n) for (int i = l; i <= n; i++)
#define rrep(i, n, l) for (int i = n - 1; i >= l; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define pb(a) push_back(a);
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef vector<lint> vli;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int d[10010][1261];
int e[10010][1261];

int main() {
  int N, M, r;
  while (1) {
    cin >> N >> M;
    if (N + M == 0)
      break;
    cin >> r;
    memset(d, 0, sizeof(d));
    memset(e, 0, sizeof(e));
    int t, n, m, s;
    rep(i, 0, r) {
      cin >> t >> n >> m >> s;
      if (s == 1) {
        d[m][t]++;
      } else {
        d[m][t]--;
      }
    }
    rep(j, 1, M + 1) {
      rep(i, 541, 1261) { d[j][i] += d[j][i - 1]; }
    }
    rep(j, 1, M + 1) {
      rep(i, 540, 1261) {
        if (i != 540)
          e[j][i] += e[j][i - 1];
        if (d[j][i] > 0)
          e[j][i]++;
        // if(j==1 && 777<=i && i<=900) cout<<j<<" "<<i<<" "<<e[j][i]<<endl;
      }
      // cout<<endl;
    }
    int q;
    cin >> q;
    int ts, te, mm;
    rep(i, 0, q) {
      cin >> ts >> te >> mm;
      o(e[mm][te - 1] - e[mm][ts - 1]);
    }
  }
}