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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define clear(arr, val) memset(arr, val, sizeof(arr))

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

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

int main() {
  int n, m, r, q;
  while (scanf("%d%d", &n, &m), n) {
    scanf("%d", &r);
    static int stu[10010][750];
    for (int i = 0; i < m; ++i)
      for (int j = 0; j < 750; ++j)
        stu[i][j] = 0;
    while (r--) {
      int t, n, m, s;
      scanf("%d%d%d%d", &t, &n, &m, &s);
      if (s == 0)
        s = -1;
      stu[m][t - 540] += s;
    }

    scanf("%d", &q);
    while (q--) {
      int s, e, m, res = 0;
      scanf("%d%d%d", &s, &e, &m);
      s -= 540, e -= 540;
      for (int t = 0, in = 0; t < e; ++t) {
        in += stu[m][t];
        if (t >= s && in > 0)
          ++res;
      }
      printf("%d\\n", res);
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define clear(arr, val) memset(arr, val, sizeof(arr))

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

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

int main() {
  int n, m, r, q;
  while (cin >> n >> m, n) {
    cin >> r;
    static int stu[10010][2048];
    clear(stu, 0);
    while (r--) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      if (s == 0)
        s = -1;
      stu[m][t] += s;
    }

    cin >> q;
    while (q--) {
      int s, e, m, res = 0;
      cin >> s >> e >> m;
      for (int t = 540, in = 0; t < e; ++t) {
        in += stu[m][t];
        if (t >= s && in > 0)
          ++res;
      }
      cout << res << endl;
    }
  }
}