Problem p00729 (C++) — buggy submission, verdict: Runtime Error

// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

int main(void) {

  while (1) {
    int N, M, r, q;
    int t, n, m, s;
    int ts, te;

    cin >> N >> M;
    if (N == 0) {
      break;
    }
    vector<vector<int>> dp(M + 1, vector<int>(723, 0));

    cin >> r;
    FOR(i, 1, r) {
      cin >> t >> n >> m >> s;
      if (s == 0) {
        dp[m][t - 538]--;
      } else {
        dp[m][t - 539]++;
      }
    }
    FOR(i, 1, M) {
      FOR(j, 1, 722) { dp[i][j] += dp[i][j - 1]; }
    }
    cin >> q;
    FOR(i, 1, q) {
      int total = 0;
      int first = -1, end = -1;
      cin >> ts >> te >> m;
      FOR(j, ts - 539, te - 539) {
        if (first == -1 && dp[m][j] >= 1) {
          first = j;
        }
        if (first != -1 && dp[m][j] <= 0) {

          end = j;
          // cout << "a" << first + 539 << " " << end + 539 << endl;
          total += end - first - 1;
          first = -1;
          end = -1;
        }
        if (j == te - 539 && first != -1) {
          // cout << "b" << first + 539 << " " << j + 539 << endl;
          total += j - first;
        }
      }
      cout << total << endl;
    }
  }

  return 0;
}


Error: -11

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

int main(void) {

  while (1) {
    int N, M, r, q;
    int t, n, m, s;
    int ts, te;

    cin >> N >> M;
    vector<vector<int>> dp(M + 1, vector<int>(723, 0));

    cin >> r;
    FOR(i, 1, r) {
      cin >> t >> n >> m >> s;
      if (s == 0) {
        dp[m][t - 538]--;
      } else {
        dp[m][t - 539]++;
      }
    }
    FOR(i, 1, M) {
      FOR(j, 1, 722) { dp[i][j] += dp[i][j - 1]; }
    }
    /*cout << endl;
    FOR(i, 1, 722) {
            cout << i+539 << " " << dp[1][i] << endl;
    }*/
    cin >> q;
    FOR(i, 1, q) {
      int total = 0;
      int first = -1, end = -1;
      cin >> ts >> te >> m;
      FOR(j, ts - 539, te - 539) {
        if (first == -1 && dp[m][j] >= 1) {
          first = j;
        }
        if (first != -1 && dp[m][j] <= 0) {

          end = j;
          // cout << "a" << first + 539 << " " << end + 539 << endl;
          total += end - first - 1;
          first = -1;
          end = -1;
        }
        if (j == te - 539 && first != -1) {
          // cout << "b" << first + 539 << " " << j + 539 << endl;
          total += j - first;
        }
      }
      cout << total << endl;
    }
  }

  return 0;
}