alexjercan/bugnet huggingface dataset:
Problem p00729 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

int ans[1261][10001] = {};

int main() {
  int N, M;
  while (cin >> N >> M && !(N == 0 && N == 0)) { // N=pc  M=students

    REP(j, 1261) {
      REP(k, 10001) { ans[j][k] = 0; }
    }

    int r;
    cin >> r;
    for (int i = 0; i < r; i++) {
      int t, m, n, s;
      cin >> t >> n >> m >> s;
      m--;
      n--;

      if (s == 1) {
        for (int j = t; j < 1260; j++) {
          ans[j][m]++;
        }
      } else {
        for (int j = t; j < 1260; j++) {
          ans[j][m]--;
        }
      }
    }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
      int s, e, m;
      cin >> s >> e >> m;
      m--;
      int tmp(0);
      for (int j = s; j < e; j++) {
        if (ans[j][m] > 0) {
          tmp++;
        }
      }
      cout << tmp << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

int main() {
  int N, M;
  while (cin >> N >> M && !(N == 0 && N == 0)) { // N=pc  M=students
    int r;
    cin >> r;
    int ans[1261][1000] = {};
    for (int i = 0; i < r; i++) {
      int t, m, n, s;
      cin >> t >> n >> m >> s;
      m--;
      n--;
      if (s == 1) {
        for (int j = t; j < 1260; j++) {
          ans[j][m]++;
        }
      } else {
        for (int j = t; j < 1260; j++) {
          ans[j][m]--;
        }
      }
    }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
      int s, e, m;
      cin >> s >> e >> m;
      m--;
      int tmp(0);
      for (int j = s; j < e; j++) {
        if (ans[j][m] > 0) {
          tmp++;
        }
      }
      cout << tmp << endl;
    }
  }
  return 0;
}
Problem p00729 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
typedef long long ll;

#define INF 1000000000
#define ST 540

int t[10000][1000];

int main() {
  int n, m;
  while (cin >> n >> m, n | m) {
    //        vector<vector<int> > t(m, vector<int>(800, 0));
    fill(t[0], t[10000], 0);
    int r;
    cin >> r;
    rep(i, r) {
      int ti, a, b, s;
      scanf("%d %d %d %d\\n", &ti, &a, &b, &s);
      if (s == 0)
        t[b - 1][ti - ST] += -1;
      else
        t[b - 1][ti - ST] += 1;
    }

    // imosu
    rep(i, 10000) {
      rep(j, 750) { t[i][j + 1] += t[i][j]; }
    }

    int q;
    cin >> q;
    rep(i, q) {
      int s, e, a;
      scanf("%d%d%d", &s, &e, &a);
      s -= ST;
      e -= ST;
      int res = 0;
      for (int j = s; j < e; j++) {
        if (t[a - 1][j] > 0)
          res++;
      }
      cout << res << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
typedef long long ll;

#define INF 1000000000
#define ST 540

short t[1000][1000];

int main() {
  int n, m;
  while (cin >> n >> m, n | m) {
    fill(t[0], t[1000], 0);
    int r;
    cin >> r;
    rep(i, r) {
      int ti, a, b, s;
      scanf("%d %d %d %d\\n", &ti, &a, &b, &s);
      if (s == 0)
        t[b - 1][ti - ST] += -1;
      else
        t[b - 1][ti - ST] += 1;
    }

    // imosu
    rep(i, 1000) {
      rep(j, 800) { t[i][j + 1] += t[i][j]; }
    }

    int q;
    cin >> q;
    rep(i, q) {
      int s, e, a;
      scanf("%d%d%d", &s, &e, &a);
      s -= ST;
      e -= ST;
      int res = 0;
      for (int j = s; j < e; j++) {
        if (t[a - 1][j] > 0)
          res++;
      }
      cout << res << endl;
    }
  }
  return 0;
}
Problem p00729 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main() {
  int n;
  while (cin >> n >> n, n) {
    vector<vector<bool>> in(n, vector<bool>(2000));
    int m;
    cin >> m;
    vvi tmp(m, vi(4));
    rep(i, m) rep(j, 4) cin >> tmp[i][j];
    rep(i, m) if (!tmp[i][3]) {
      for (int j = i - 1; j >= 0; j--) {
        if (tmp[i][1] == tmp[j][1] && tmp[i][2] == tmp[j][2]) {
          loop(k, tmp[j][0] + 1, tmp[i][0] + 1) in[tmp[i][2] - 1][k] = true;
          break;
        }
      }
    }
    // while(cin>>m,m)cout<<in[0][m]<<endl;
    cin >> m;
    while (m--) {
      int a, b, c;
      cin >> a >> b >> c;
      int out = 0;
      loop(i, a + 1, b + 1) if (in[c - 1][i]) out++;
      cout << out << endl;
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main() {
  int n;
  while (cin >> n >> n, n) {
    vvi in(n, vi(2000));
    int m;
    cin >> m;
    vvi tmp(m, vi(4));
    rep(i, m) rep(j, 4) cin >> tmp[i][j];
    rep(i, m) if (!tmp[i][3]) {
      for (int j = i - 1; j >= 0; j--) {
        if (tmp[i][1] == tmp[j][1] && tmp[i][2] == tmp[j][2]) {
          loop(k, tmp[j][0] + 1, tmp[i][0] + 1) in[tmp[i][2] - 1][k]++;
          break;
        }
      }
    }
    // while(cin>>m,m)cout<<in[0][m]<<endl;
    cin >> m;
    while (m--) {
      int a, b, c;
      cin >> a >> b >> c;
      int out = 0;
      loop(i, a + 1, b + 1) if (in[c - 1][i]) out++;
      cout << out << endl;
    }
  }
}
Problem p00729 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <ctype.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main() {

  int pc, stu;

  while (1) {

    cin >> pc >> stu;
    if (pc == 0 && stu == 0)
      break;

    int i, j, k, srec, sque, t, n, m, s, ts, te, sum;
    int record[pc][721];

    for (i = 0; i < pc; i++) {
      for (j = 0; j < 721; j++) {
        record[i][j] = 0;
      }
    }

    // record入力
    cin >> srec;
    for (i = 0; i < srec; i++) {
      cin >> t >> n >> m >> s;
      n--;

      if (s == 1) {
        // ログイン
        for (j = t - 540; j < 721; j++) {
          // if (j==19)cout<<"19"<<endl;
          record[n][j] = m;
          // cout <<"n="<<n<<" j="<<j<<" record="<<record[n][j] <<endl;
        }
      } else {
        // ログアウト
        for (j = t - 540; j < 721; j++) {
          record[n][j] = 0;
        }
      }
    }

    // 記録チェック
    cin >> sque;
    for (i = 0; i < sque; i++) {
      sum = 0;
      cin >> ts >> te >> m;
      for (j = ts - 540; j < te - 540; j++) {
        for (k = 0; k < pc; k++) {
          if (record[k][j] == m) {
            sum += 1;
            break;
          }
        }
      }
      cout << sum << endl;
    }
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <ctype.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main() {

  int pc, stu;

  while (1) {

    cin >> pc >> stu;
    if (pc == 0 && stu == 0)
      break;

    int i, j, k, srec, sque, t, n, m, s, ts, te, sum;
    int record[pc][721];

    for (i = 0; i < pc; i++) {
      for (j = 0; j < 721; j++) {
        record[i][j] = 0;
      }
    }

    // record入力
    cin >> srec;
    for (i = 0; i < srec; i++) {
      cin >> t >> n >> m >> s;

      if (s == 1) {
        // ログイン
        for (j = t - 540; j < 721; j++) {
          record[n][j] = m;
        }
      } else {
        // ログアウト
        for (j = t + 1 - 540; j < 721; j++) {
          record[n][j] = 0;
        }
      }
    }

    // 記録チェック
    cin >> sque;
    for (i = 0; i < sque; i++) {
      sum = 0;
      cin >> ts >> te >> m;
      for (j = ts - 540; j < te + 1 - 540; j++) {
        for (k = 0; k < pc; k++) {
          if (record[k][j] == m) {
            sum += 1;
            break;
          }
        }
      }
      cout << sum << endl;
    }
  }

  return 0;
}
Problem p00729 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
int main() {
  int N, M;
  while (cin >> N >> M, N) {
    int r, t, n, m, s;
    cin >> r;
    vector<vector<pii>> V(M);
    map<pii, int> L;
    for (int i = 0; i < r; ++i) {
      cin >> t >> n >> m >> s;
      --m;
      if (s)
        L[pii(n, m)] = t;
      else
        V[m].push_back(pii(L[pii(n, m)], t));
    }
    int q, ts, te;
    cin >> q;
    for (int i = 0; i < q; ++i) {
      cin >> ts >> te >> m;
      --m;
      sort(V[m].begin(), V[m].end());
      int ans = te - ts;
      for (int j = 0; j < V[m].size(); ++j) {
        if (ts >= V[m][j].first)
          ts = max(ts, V[m][j].second);
        else if (V[m][j].first > te)
          break;
        else {
          ans += ts - V[m][j].first;
          ts = V[m][j].second;
        }
        if (ts >= te)
          break;
      }
      if (ts < te)
        ans += ts - te;
      cout << ans << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
int main() {
  int N, M, inf = 1000000;
  while (cin >> N >> M, N) {
    int r, t, n, m, s;
    cin >> r;
    vector<vector<pii>> V(M);
    int L[N][M];
    for (int i = 0; i < r; ++i) {
      cin >> t >> n >> m >> s;
      --m;
      if (s)
        L[n][m] = t;
      else
        V[m].push_back(pii(L[n][m], t));
    }
    int q, ts, te;
    cin >> q;
    for (int i = 0; i < q; ++i) {
      cin >> ts >> te >> m;
      --m;
      sort(V[m].begin(), V[m].end());
      int ans = te - ts;
      for (int j = 0; j < V[m].size(); ++j) {
        if (ts >= V[m][j].first)
          ts = max(ts, V[m][j].second);
        else {
          ans += ts - V[m][j].first;
          ts = V[m][j].second;
        }
        if (ts >= te)
          break;
      }
      if (ts < te)
        ans += ts - te;
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p00729 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>

std::map<int, int> use[10001];
std::map<int, bool> stu[10001];
int N, M, r, t, n, m, s;

int solve(int s, int e, int m) {
  int ans = 0;
  int tmp = 0;

  for (int i = 540; i <= 1260; i++) {
    if (tmp && i > s && i <= e)
      ans++;

    if (use[m][i])
      tmp += use[m][i];
  }

  return ans;
}

int main() {
  using namespace std;
  while (1) {
    cin >> N >> M;
    if (!N && !M)
      break;

    cin >> r;
    for (int i = 0; i < r; i++) {
      cin >> t >> n >> m >> s;
      if (s)
        use[m][t]++;
      else
        use[m][t]--;
    }

    cin >> r;
    for (int i = 0; i < r; i++) {
      cin >> s >> t >> m;
      cout << solve(s, t, m) << endl;
    }

    for (int i = 1; i <= M; i++) {
      use[i].clear();
      stu[i].clear();
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>

std::map<int, int> PC[1001];
int N, M, r, t, n, m, s;

int solve(int s, int e, int m) {
  int ans = 0;
  bool usd[1001] = {false};

  for (int i = 540; i <= 1260; i++) {
    for (int j = 1; j <= N; j++) {
      if (usd[j] && i > s && i <= e) {
        ans++;
        break;
      }
    }

    for (int j = 1; j <= N; j++) {
      if (PC[j][i] == m)
        usd[j] = !usd[j];
    }
  }

  return ans;
}

int main() {
  using namespace std;
  while (1) {
    cin >> N >> M;
    if (!N && !M)
      break;

    cin >> r;
    for (int i = 0; i < r; i++) {
      cin >> t >> n >> m >> s;
      PC[n][t] = m;
    }

    cin >> r;
    for (int i = 0; i < r; i++) {
      cin >> s >> t >> m;
      cout << solve(s, t, m) << endl;
    }

    for (int i = 1; i <= N; i++)
      PC[i].clear();
  }

  return 0;
}
Problem p00729 (C++) — buggy submission, verdict: Runtime Error

#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#define MAX 10010
#include <queue>
using namespace std;
typedef pair<int, int> P;
int main() {
  int e, q, t, n, m, s, p;
  while (cin >> e >> q, e || q) {
    vector<P> tab[MAX];
    cin >> p;
    for (int i = 0; i < p; i++) {
      cin >> t >> n >> m >> s;
      tab[m - 1].push_back(P(t, s));
    }
    int now, t1, t2;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> t1 >> t2 >> m;
      now = t1;
      int ans = 0, sum = 0;
      int j;
      m--;
      for (j = 0; j < tab[m].size() && tab[m][j].first < t2; j++) {
        if (!tab[m][j].second) {
          if (sum > 0 && tab[m][j].first >= t1 && tab[m][j].first < t2) {
            ans += tab[m][j].first - now;
          }
          sum--;
          if (tab[m][j].first > t1 && tab[m][j].first < t2)
            now = tab[m][j].first;
        } else if (tab[m][j].second) {
          if (sum > 0 && tab[m][j].first > t1 && tab[m][j].first < t2) {
            ans += tab[m][j].first - now;
          }
          sum++;
          if (tab[m][j].first >= t1 && tab[m][j].first < t2)
            now = tab[m][j].first;
        }
      }
      if (sum > 0)
        ans += t2 - now;
      cout << ans << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#define MAX 1010
#include <queue>
using namespace std;
typedef pair<int, int> P;
int main() {
  int e, q, t, n, m, s, p;
  while (cin >> e >> q, e || q) {
    vector<P> tab[MAX];
    cin >> p;
    for (int i = 0; i < p; i++) {
      cin >> t >> n >> m >> s;
      tab[m - 1].push_back(P(t, s));
    }
    int now, t1, t2;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> t1 >> t2 >> m;
      now = t1;
      int ans = 0, sum = 0;
      int j;
      m--;
      for (j = 0; j < tab[m].size() && tab[m][j].first < t2; j++) {
        if (!tab[m][j].second) {
          if (sum > 0 && tab[m][j].first >= t1 && tab[m][j].first < t2) {
            ans += tab[m][j].first - now;
          }
          sum--;
          if (tab[m][j].first > t1 && tab[m][j].first < t2)
            now = tab[m][j].first;
        } else if (tab[m][j].second) {
          if (sum > 0 && tab[m][j].first > t1 && tab[m][j].first < t2) {
            ans += tab[m][j].first - now;
          }
          sum++;
          if (tab[m][j].first >= t1 && tab[m][j].first < t2)
            now = tab[m][j].first;
        }
      }
      if (sum > 0)
        ans += t2 - now;
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p00729 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int MAX = 1300;
struct Info {
  int t, n, m, s;
};
typedef pair<int, int> P;
vector<Info> V;
bool used[MAX];
int N, M;

int solve(int ts, int te, int m) {

  int ans = 0;

  vector<P> login;
  vector<P> logout;

  for (int i = 0; i < V.size(); i++) {
    if (V[i].m == m && V[i].s == 1)
      login.push_back(P(V[i].n, V[i].t));

    if (V[i].m == m && V[i].s == 0)
      logout.push_back(P(V[i].n, V[i].t));
  }

  sort(login.begin(), login.end());
  sort(logout.begin(), logout.end());

  for (int i = 0; i < login.size(); i++) {
    int ss = max(ts, login[i].second);
    int ee = min(te, logout[i].second);
    //    fill(used+ss,used+ee,true);
    for (int i = ss; i < ee; i++)
      used[i] = true;
  }

  for (int i = ts; i < te; i++)
    if (used[i])
      ans++;

  return ans;
}

void init() {
  V.clear();
  memset(used, false, sizeof(used));
}

int main() {

  while (cin >> N >> M && N + M) {
    init();
    int r;
    cin >> r;
    while (r--) {
      Info f;
      cin >> f.t >> f.n >> f.m >> f.s;
      V.push_back(f);
    }

    int q;
    cin >> q;
    while (q--) {
      memset(used, false, sizeof(used));
      int ts, te, m;
      cin >> ts >> te >> m;
      cout << solve(ts, te, m) << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int MAX = 1300;
struct Info {
  int t, n, m, s;
};
typedef pair<int, int> P;
vector<Info> V;
bool used[MAX];
int N, M;

int solve(int ts, int te, int m) {

  int ans = 0;

  vector<P> login;
  vector<P> logout;

  for (int i = 0; i < V.size(); i++)
    if (V[i].m == m && V[i].s == 1)
      login.push_back(P(V[i].n, V[i].t));

  for (int i = 0; i < V.size(); i++)
    if (V[i].m == m && V[i].s == 0)
      logout.push_back(P(V[i].n, V[i].t));

  sort(login.begin(), login.end());
  sort(logout.begin(), logout.end());

  for (int i = 0; i < login.size(); i++) {
    int s = max(ts, login[i].second);
    int e = min(te, logout[i].second);
    fill(used + s, used + e, true);
  }

  for (int i = ts; i < te; i++)
    if (used[i])
      ans++;

  return ans;
}

void init() {
  V.clear();
  memset(used, false, sizeof(used));
}

int main() {

  while (cin >> N >> M && N + M) {
    init();
    int r;
    cin >> r;
    while (r--) {
      Info f;
      cin >> f.t >> f.n >> f.m >> f.s;
      V.push_back(f);
    }
    int q;
    cin >> q;
    while (q--) {
      int ts, te, m;
      cin >> ts >> te >> m;
      cout << solve(ts, te, m) << endl;
    }
  }
  return 0;
}
Problem p00729 (C++) — buggy submission, verdict: Runtime Error


// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1148&lang=jp

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

int main() {
  int N, M;

  while (cin >> N >> M && N) {
    vector<int> PC(N + 1, 0);
    vector<vector<int>> record(M + 1, vector<int>(1261, 0));

    int R;
    cin >> R;
    rep(nth, R) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      if (s == 1) {
        PC[n] = t;
      } else {
        int ts = PC[n];
        REP(i, ts, t) { record[m][i] = 1; }
        PC[n] = 0;
      }
    }

    int Q;
    cin >> Q;
    rep(nth, Q) {
      int ts, te, m;
      cin >> ts >> te >> m;
      int ans = 0;
      REP(i, ts, te) {
        if (record[m][i]) {
          ans += 1;
        }
      }
      cout << ans << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:



// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1148&lang=jp

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

int main() {
  int N, M;

  while (cin >> N >> M && N) {
    int PC[N + 1];
    int record[M + 1][1261];
    fill(PC, PC + N + 1, 0);
    fill_n((int *)record, sizeof(record) / sizeof(int), 0);

    int R;
    cin >> R;
    rep(nth, R) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      if (s == 1) {
        PC[n] = t;
      } else {
        int ts = PC[n];
        REP(i, ts, t) { record[m][i] = 1; }
        PC[n] = 0;
      }
    }

    int Q;
    cin >> Q;
    rep(nth, Q) {
      int ts, te, m;
      cin >> ts >> te >> m;
      int ans = 0;
      REP(i, ts, te) {
        if (record[m][i]) {
          ans += 1;
        }
      }
      cout << ans << endl;
    }
  }

  return 0;
}