Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	for {
		_, M := io.NextInt(), io.NextInt()
		if M == 0 {
			break
		}
		r := io.NextInt()
		t, m, s := make([]int, r), make([]int, r), make([]int, r)
		for i := 0; i < r; i++ {
			t[i], _, m[i], s[i] = io.NextInt(), io.NextInt(), io.NextInt()-1, io.NextInt()
		}
		q := io.NextInt()
		for i := 0; i < q; i++ {
			ss, ee, mm := io.NextInt(), io.NextInt(), io.NextInt()-1
			x := make([]int, 1300)
			for j := 0; j < r; j++ {
				if m[j] == mm {
					if s[j] == 1 {
						x[t[j]]++
					} else {
						x[t[j]]--
					}
				}
			}
			for j := 1; j < 1300; j++ {
				x[j] = x[j] + x[j-1]
			}
			ans := 0
			for j := ss; j < ee; j++ {
				if x[j] > 0 {
					ans++
				}
			}
			io.Println(ans)
		}
	}
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 10e15+9
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int N, M, r, q;

int t, n, m, s, ts, te;

int atime[1010][10010];
bool used[1000][10010];

int main(){
	while (cin >> N >> M&&N + M){
		cin >> r;
		REP(i, r){
			cin >> t >> n >> m >> s;
			t -= 540;
			if (s == 1){
				atime[n][m] = t;
			}
			else{
				FOR(j, atime[n][m], t){
					used[j][m] = 1;
				}
			}
		}
		cin >> q;
		REP(i, q){
			int ans = 0, t = -1;
			cin >> ts >> te >> m;
			ts -= 540;
			te -= 540;
			FOR(j, ts, te + 1){
				if (t == -1){
					if (used[j][m])t = j;
				}
				else{
					if (!used[j][m]){
						//cout << "!" << endl;
						//cout << j - 1 << " " << t << endl;
						ans += j - t;
						t = -1;
					}
				}
			}
			if (t != -1){
				ans += te - t;
			}
			cout << ans << endl;
		}
	}
}

C++: #include<stdio.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <string>

using namespace std;

int n, m, r, q, t[10000][1300];

int main() {

	
	while (1) {

		for (int i = 0; i < 10000; i++) {
			for (int j = 0; j < 1300; j++) {
				t[i][j] = 0;
			}
		}

		cin >> n >> m;
		if (n==0 && m==0) break;

		cin >> r;
		for (int i = 0; i < r; i++) {
				int tt, tn, tm, ts;
				cin >> tt >> tn >> tm >> ts;
				if (ts == 1) t[tm-1][tt]++;
				else t[tm - 1][tt+1]--;
		}

		for (int i = 0; i < m; i++) {
			for (int j = 1; j <1300; j++) {
				//if(750<j && j<800) cout << "t[i][j-1]" << t[i][j - 1] << endl;
				t[i][j] += t[i][j-1];
			}
		}

		cin >> q;
		for (int i = 0; i < q; i++) {
			int ts, te, tm;
			cin >> ts >> te >> tm;
			int ans=0;
			bool flag = false;
			for (int j = ts; j < te; j++) {
				if (t[tm - 1][j]>0) ans++, flag = true;
				if (flag && t[tm - 1][j]==0) ans--,flag = false;
			}
			cout << ans << endl;
		}
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll ,P> P3;
typedef pair<P ,P> PP;
const ll MOD = 998244353;
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(3e5 + 5);
const double EPS = 1e-6;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()

int n, m, r, q, cum[10005][1300];

int main() {
    while(cin >> n >> m, n){
        for(int i=0;i<m;i++){
            fill(cum[i],cum[i]+2000,0);
        }
        cin >> r;
        for(int i=0;i<r;i++){
            int t, a, b, s;
            cin >> t >> a >> b >> s;
            b--;
            if(s){
                cum[b][t+1]++;
            }
            else{
                cum[b][t+1]--;
            }
        }
        for(int i=0;i<m;i++){
            for(int j=0;j<1290;j++){
                cum[i][j+1] += cum[i][j];
            }
            for(int j=0;j<1290;j++){
                cum[i][j+1] = min(1, cum[i][j+1]);
                cum[i][j+1] += cum[i][j];
            }
        }
        cin >> q;
        for(int i=0;i<q;i++){
            int ts, te, b;
            cin >> ts >> te >> b;
            b--;
            cout << cum[b][te] - cum[b][ts] << endl;
        }
    }

    return 0;
}

C++: #include <cstdio>

#pragma warning(disable: 4996)

#define MAX_N 1000

int N, M, R, Q, t1, n1, m1, s1, t2, m2, n2, s2, s, e, m;

int record[MAX_N + 1][1260];

int main()
{
	while (true)
	{
		scanf("%d", &N);
		scanf("%d", &M);
		scanf("%d", &R);

		for (int i = 0; i < (R / 2); i++)
		{
			scanf("%d", &t1);
			scanf("%d", &n1);
			scanf("%d", &m1);
			scanf("%d", &s1);
			scanf("%d", &t2);
			scanf("%d", &n2);
			scanf("%d", &m2);
			scanf("%d", &s2);

			for (int j = t1; j < t2; j++)
			{
				record[m1][j] = 1;
			}
		}

		scanf("%d", &Q);

		for (int i = 0; i < Q; i++)
		{
			scanf("%d", &s);
			scanf("%d", &e);
			scanf("%d", &m);

			int count = 0;

			for (int j = s; j < e; j++)
			{
				count += record[m][j];
			}

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

		for (int i = 1; i <= N; i++)
		{
			for (int j = 540; j < 1260; j++)
			{
				record[i][j] = 0;
			}
		}
	}

	return 0;
}