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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#include <stdio.h>
#include <string.h>

// #include <unordered_set>
// #include <unordered_map>

#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())

#define pb push_back
#define mp make_pair

int gcd(int a, int b) {
  int c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
int lcm(int a, int b) {
  int c = gcd(a, b);
  a /= c;
  return a * b;
}

typedef std::pair<int, int> P;
typedef long long ll;
typedef long double ld;

const int MOD = 1000000007;
const int INF = 1e9;
const ll LINF = INF * 1ll * INF;
const ld DINF = 1e200;
const double EPS = 1e-10;
const double PI = acos(-1.0);

using namespace std;

// #define int long long

const int MAX_V = 1000;
// int memo[1000001]={0};
int dist[MAX_V][MAX_V];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cin?????????

  int a, b;
  int r, q;
  int t, n, m, s;
  int ts, tr, M;
  int ans;

  while (cin >> a >> b) {
    // cin>>a>>b;

    if (a == 0 && b == 0)
      break;

    int table[10002][782];
    // 782=1262-540

    memset(table, 0, sizeof(table));

    cin >> r;

    REP(i, r) {

      // cout<<"t,n,m,s??\\???"<<endl;

      cin >> t >> n >> m >> s;
      t -= 540;

      if (s == 1) {
        table[m][t]++;
      } else if (s == 0) {
        table[m][t]--;
      }
    }

    REP(i, 10002) {
      REP(j, 782) {
        if (table[i][j] >= 0)
          table[i][j + 1] += table[i][j];
      }
    }

    cin >> q;

    REP(i, q) {
      ans = 0;
      // cout<<"ts,tr,M??\\???"<<endl;

      cin >> ts >> tr >> M;
      ts -= 540;
      tr -= 540;

      for (int l = ts; l < tr; l++) {

        // cout<<table[M][l]<<" ";

        if (table[M][l] >= 1)
          ans++;
      }
      // cout<<endl;
      // cout<<"??????"<<M<<"????????¨?????????"<<ans<<endl;

      cout << ans << endl;
    }
    // REP(i,30){
    //     cout<<i<<" ";
    // }cout<<endl;

    // REP(i,5){
    //     REP(j,30){
    //         cout<<table[i][j]<<" ";
    //     }cout<<endl;
    // }
  }
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#include <stdio.h>
#include <string.h>

// #include <unordered_set>
// #include <unordered_map>

#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())

#define pb push_back
#define mp make_pair

int gcd(int a, int b) {
  int c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
int lcm(int a, int b) {
  int c = gcd(a, b);
  a /= c;
  return a * b;
}

typedef std::pair<int, int> P;
typedef long long ll;
typedef long double ld;

const int MOD = 1000000007;
const int INF = 1e9;
const ll LINF = INF * 1ll * INF;
const ld DINF = 1e200;
const double EPS = 1e-10;
const double PI = acos(-1.0);

using namespace std;

#define int long long

const int MAX_V = 1000;
// int memo[1000001]={0};
int dist[MAX_V][MAX_V];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cin?????????

  int a, b;
  int r, q;
  int t, n, m, s;
  int ts, tr, M;
  int ans;

  while (cin >> a >> b) {
    // cin>>a>>b;

    if (a == 0 && b == 0)
      break;

    int table[10002][1262] = {0};

    cin >> r;

    REP(i, r) {

      // cout<<"t,n,m,s??\\???"<<endl;

      cin >> t >> n >> m >> s;
      // t-=540;

      if (s == 1) {
        table[m][t]++;
      } else if (s == 0) {
        table[m][t]--;
      }
    }

    REP(i, 10002) {
      REP(j, 1262) {
        if (table[i][j] >= 0)
          table[i][j + 1] += table[i][j];
      }
    }

    cin >> q;

    REP(i, q) {
      ans = 0;
      // cout<<"ts,tr,M??\\???"<<endl;

      cin >> ts >> tr >> M;
      // ts-=540;
      // tr-=540;

      for (int l = ts; l < tr; l++) {

        // cout<<table[M][l]<<" ";

        if (table[M][l] >= 1)
          ans++;
      }
      // cout<<endl;
      // cout<<"??????"<<M<<"????????¨?????????"<<ans<<endl;

      cout << ans << endl;
    }
    // REP(i,30){
    //     cout<<i<<" ";
    // }cout<<endl;

    // REP(i,5){
    //     REP(j,30){
    //         cout<<table[i][j]<<" ";
    //     }cout<<endl;
    // }
  }
}