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++: //#define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

// ?????¬???????????¬??????
#pragma region MACRO
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define PED cout << "\n"
#define rep(i,n) for(int i=0; i<(int)n; ++i)
#define REP(i,x,n) for(int i=x; i<(int)n; ++i)
#define repi(i,n) for(int i=0; i<=(int)n; ++i)
#define REPI(i,x,n) for(int i=x; i<=(int)n; ++i)
#define ILP while(true)
#define FOR(i,c) for(__typeof((c).begin())!=(c).begin(); i!=(c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#pragma endregion

// ???
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long double> vld;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<string> vs;
#pragma endregion

// Effective std
#pragma region ESTD
template<typename C, typename T> int count(C& c, T t) { return count(ALL(c), t); }
template<typename C, typename F> int count_if(C& c, F f) { return count_if(ALL(c), f); }
template<typename C, typename T> void erase(C& c, T t) { remove(ALL(c), t), c.end(); }
template<typename C> void remove(vector<C>& c, unsigned int index) { c.erase(c.begin()+index); }
template<typename C, typename T, typename U> void replace(C& c, T t, U u) { replace(ALL(c), t, u); }
template<typename C, typename F, typename U> void replace_if(C& c, F f, U u) { (ALL(c), f, u); }
template<typename C> void reverse(C& c) { reverse(ALL(c)); }
template<typename C> void sort(C& c) { sort(ALL(c)); }
template<typename C, typename Pred> void sort(C& c, Pred p) { sort(ALL(c), p); }
#pragma endregion

// ?????°
#pragma region CONST_VAL
constexpr int PI = (2*acos(0.0));
constexpr int EPS = (1e-9);
constexpr int MOD = (int)(1e9+7);
constexpr int INF = 100000000;
#pragma endregion

#define int long long


signed main()
{
    int N, M;
    while(cin>>N>>M, N) {
        vector<pair<int, int> > members[10001];
        int r;
        cin >> r;
        for(int i=0; i<r; ++i) {
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            members[m].push_back(pair<int,int>(t,s));
        }
        for(int i=1; i<=M; ++i) sort(members[i].begin(), members[i].end());
        int q;
        cin >> q;
        for(int i=0; i<q; ++i) {
            int s, e, m;
            cin >> s >> e >> m;
            int pc_count = 0;
            int current_time = s;
            int ans = 0;
            vector<pair<int, int> > member = members[m];
            for(int i=0; i<member.size(); ++i) {
                pair<int, int> p = member[i];
                if(current_time <= p.first && pc_count > 0)
                    ans += min(e, p.first) - current_time;

                current_time = max(s, min(e, p.first));
                pc_count += p.second ? 1 : -1;

                if(current_time == e) break;
            }
            cout << ans << endl;
        }
    }
    return 0;
}

C++: #include<iostream>

using namespace std;

#define MMAX 100001
#define tMAX 1261
int main(){

	int N, M;
	int r;
	int t, n, m1, s;
	int q;
	int ts, te, m2;
	int num,cnt;
	int data[MMAX+1][tMAX+1];

	while (1){
		cin >> N >> M;
		if (N == 0 && M == 0) break;
		for (int i = 1; i <= M; i++){
			for (int j = 0; j <= tMAX; j++){
				data[i][j] = 0;
			}
		}
		cin >> r;
		for (int i = 0; i < r; i++){
			cin >> t >> n >> m1 >> s;
			if (s == 0) data[m1][t]=-1;
			else data[m1][t]=1;
		}
		cin >> q;
		for (int i = 1; i <= M; i++){
			num = 0;
			for (int j = 0; j <= tMAX; j++){
				num += data[i][j];
				data[i][j] = num;
			}
		}
		for (int i = 0; i < q; i++){
			cin >> ts >> te >> m2;
			cnt = 0;
			for (int j = ts; j < te; j++){
				if (data[m2][j] >= 1) cnt++;
			}
			cout << cnt << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <cstring>
using namespace std;

typedef pair <int, int> PII;
typedef vector<PII> VPII;
typedef vector<VPII> VVPII;
bool U[2000];
int L[1001];

int main( void )
{
    int N, M;
    while ( cin >> N >> M && N && M ) {
        VVPII T( 10001 );
        memset( L, -1, sizeof(L) );
        int r, q;
        int t, n, m, s, ts, te;
        cin >> r;
        for ( int i = 0; i < r; i++ ) {
            cin >> t >> n >> m >> s;
            if ( s ) {
                L[n] = t;
            } else {
                T[m].push_back( PII( L[n], t ) );
                L[n] = -1;
            }
        }
        cin >> q;
        for ( int i = 0; i < q; i++ ) {
            memset( U, false, sizeof(U) );
            int answer = 0;
            cin >> ts >> te >> m;
            for ( VPII ::iterator it = T[m].begin(); it != T[m].end(); it++ ) {
                PII r = *it;
                if ( r.second < ts || r.first > te ) continue;
                for ( int j = r.first; j < r.second; j++ ) U[j] = true;
            }
            for ( int j = ts; j < te; j++ ) if ( U[j] ) answer++;
            cout << answer << endl;
        }
    }
    return 0;
}