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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y))
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
int const MOD = 1000000007;

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int calc(int s, int e, int ts, int te) {
  int x = min(e, te) - max(s, ts);
  if (x < 0)
    x = 0;
  return x;
}

map<int, map<int, int>> logs;
int ruiseki[1010];
signed main() {
  int N, M;
  while (cin >> N >> M) {
    if (N == 0 && M == 0)
      break;
    vector<pii> v[10010];
    logs.clear();
    int r, t, n, m, s;
    cin >> r;
    rep(i, 0, r) {
      cin >> t >> n >> m >> s;
      t -= 540;
      m--;
      if (s == 0)
        v[m].pb(pii(logs[m][n], t));
      else
        logs[m][n] = t;
    }

    int q;
    cin >> q;
    rep(i, 0, q) {
      int ts, te, mm;
      cin >> ts >> te >> mm;
      ts -= 540;
      te -= 540;
      mm--;

      memset(ruiseki, 0, sizeof(ruiseki));
      rep(j, 0, v[mm].size()) {
        ruiseki[v[mm][j].fr]++;
        ruiseki[v[mm][j].sc + 1]--;
      }
      int ans = 0;
      rep(j, 1, 1000) {
        ruiseki[j] += ruiseki[j - 1];
        if (ruiseki[j] && ruiseki[j - 1] && ts < j && j <= te)
          ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y))
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
int const MOD = 1000000007;

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int calc(int s, int e, int ts, int te) {
  int x = min(e, te) - max(s, ts);
  if (x < 0)
    x = 0;
  return x;
}

vector<pii> v[10010];
int logs[10010][1010];
int ruiseki[1010];
signed main() {
  int N, M;
  while (cin >> N >> M) {
    if (N == 0 && M == 0)
      break;
    memset(logs, 0, sizeof(logs));
    int r, t, n, m, s;
    cin >> r;
    rep(i, 0, r) {
      cin >> t >> n >> m >> s;
      t -= 540;
      m--;
      if (s == 0)
        v[m].pb(pii(logs[m][n], t));
      else
        logs[m][n] = t;
    }

    int q;
    cin >> q;
    rep(i, 0, q) {
      int ts, te, mm;
      cin >> ts >> te >> mm;
      ts -= 540;
      te -= 540;
      mm--;

      memset(ruiseki, 0, sizeof(ruiseki));
      rep(j, 0, v[mm].size()) {
        ruiseki[v[mm][j].fr]++;
        ruiseki[v[mm][j].sc + 1]--;
      }
      int ans = 0;
      rep(j, 1, 1000) {
        ruiseki[j] += ruiseki[j - 1];
        if (ruiseki[j] && ruiseki[j - 1] && ts < j && j <= te)
          ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}