Problem p00729 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 1010

bool solve() {
  int n, m, r, q;
  int rec_t[SIZE], rec_m[SIZE], rec_n[SIZE], rec_s[SIZE];
  int memo[SIZE], c[SIZE] = {};
  vector<pair<int, int>> vec[SIZE];

  scanf("%d%d%d", &n, &m, &r);

  if (n == 0)
    return false;

  for (int i = 0; i < r; i++) {
    scanf("%d%d%d%d", rec_t + i, rec_n + i, rec_m + i, rec_s + i);

    if (rec_s[i] == 1) {
      if (c[rec_m[i]] == 0)
        memo[rec_m[i]] = rec_t[i];
      c[rec_m[i]]++;
    } else {
      c[rec_m[i]]--;

      if (c[rec_m[i]] == 0)
        vec[rec_m[i]].push_back({memo[rec_m[i]], rec_t[i]});
    }
  }

  scanf("%d", &q);

  for (int i = 0; i < q; i++) {
    int ts, te, m;
    int sum = 0;

    scanf("%d%d%d", &ts, &te, &m);

    for (int j = 0; j < vec[m].size(); j++) {
      if (!(te <= vec[m][j].first || vec[m][j].second <= ts)) {
        sum += min(te, vec[m][j].second) - max(ts, vec[m][j].first);
      }
    }

    printf("%d\n", sum);
  }

  return true;
}

int main() {

  while (solve())
    ;

  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 10010

bool solve() {
  int n, m, r, q;
  int rec_t[SIZE], rec_m[SIZE], rec_n[SIZE], rec_s[SIZE];
  int memo[SIZE], c[SIZE] = {};
  vector<pair<int, int>> vec[SIZE];

  scanf("%d%d%d", &n, &m, &r);

  if (n == 0)
    return false;

  for (int i = 0; i < r; i++) {
    scanf("%d%d%d%d", rec_t + i, rec_n + i, rec_m + i, rec_s + i);

    if (rec_s[i] == 1) {
      if (c[rec_m[i]] == 0)
        memo[rec_m[i]] = rec_t[i];
      c[rec_m[i]]++;
    } else {
      c[rec_m[i]]--;

      if (c[rec_m[i]] == 0)
        vec[rec_m[i]].push_back({memo[rec_m[i]], rec_t[i]});
    }
  }

  scanf("%d", &q);

  for (int i = 0; i < q; i++) {
    int ts, te, m;
    int sum = 0;

    scanf("%d%d%d", &ts, &te, &m);

    for (int j = 0; j < vec[m].size(); j++) {
      if (!(te <= vec[m][j].first || vec[m][j].second <= ts)) {
        sum += min(te, vec[m][j].second) - max(ts, vec[m][j].first);
      }
    }

    printf("%d\n", sum);
  }

  return true;
}

int main() {

  while (solve())
    ;

  return 0;
}