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 <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
 
#define rep(i, n) for(int i=0; i<(n); ++i)

class history {
public:
    int pc, start, end;
    history(int p, int s, int e) {
        pc = p; start = s; end = e;
    }
};
int main(void){
    int N, M;
    int r, t, n, m, s, ts, te, q;
    vector<history> hist[1001];
    while(cin >> N >> M && N) {
        rep(i, 1001) hist[i].clear();
        cin >> r;
        rep(i, r) {
            cin >> t >> n >> m >> s;
            n--; m--;
            if(s==1) {
                hist[m].push_back(history(n, t, -1));
            } else {
                rep(j, hist[m].size()) {
                    if(hist[m][j].pc == n && hist[m][j].end == -1) {
                        hist[m][j].end = t;
                        break;
                    }
                }
            }
        }
        cin >> q;
        rep(i, q) {
            int ans = 0;
            cin >> ts >> te >> m;
            m--;
            int tmp[1261];
            rep(j, 1261) tmp[j] = 0;
            rep(j, hist[m].size()) {
                if(hist[m][j].end >= ts && hist[m][j].start <= te) {
                    for(int t=max(ts, hist[m][j].start); 
                            t <= min(te, hist[m][j].end); ++t) tmp[t] = 1;
                }
            }
            for(int j=540; j<1261; j++) {
                int state = 0;
                if(tmp[j] == 1) {
                    int start = j;
                    while(tmp[j]==1) j++;
                    ans += j - start - 1;
                }
            }
            cout << ans << endl;
        }
    }
}

C++: 
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <cctype>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <cassert>
#include <iomanip>

typedef long long ll;
using namespace std;



struct Slog{
	int pc;
	int time;
	int inout;
	
	Slog(){}
	Slog(int n, int t, int s) : pc(n), time(t), inout(s) {}
};

struct Slog2{
	int in;
	int out;
	
	bool operator< (const Slog2 &s) const{
		return (*this).in == s.in ? (*this).out > s.out : (*this).in < s.in;
	}
};

struct Sstu{
	int num;
	vector<Slog> log;
	vector<Slog2> log2;
	
	Sstu(){}
	void MakeLog2(int N){
		vector<Slog2> list(N);
		for(int i=0;i<log.size();i++){
			if(log[i].inout == 1){
				list[log[i].pc-1].in = log[i].time;
				list[log[i].pc-1].out = log[i].time;
			}
			if(log[i].inout == 0){
				list[log[i].pc-1].out = log[i].time;
				log2.push_back(list[log[i].pc-1]);
			}
		}
		sort(log2.begin(), log2.end());
	}
};

bool solve(){
	int N, M, r, q;
	cin>> N>> M;
	if(N==0 && M==0) return false;
	
	vector<Sstu> stu(M);
	for(int i=0;i<M;i++) stu[i].num = i+1;
	cin>> r;
	for(int i=0;i<r;i++){
		int t, n, m, s;
		cin>> t>> n>> m>> s;
		Slog in(n,t,s);
		stu[m-1].log.push_back(in);
	}
	for(int i=0;i<M;i++) stu[i].MakeLog2(N);
	
	cin>> q;
	for(int i=0;i<q;i++){
		int ans = 0;
		int ts, te, num;
		cin>> ts>> te>> num;
		Sstu *s = &stu[num-1];
		int end = ts;
		for(int j=0; j < s->log2.size(); j++){
			if(s->log2[j].out < end || s->log2[j].in > te) continue;
			ans += min(s->log2[j].out, te) - max(s->log2[j].in, end);
			end = s->log2[j].out;
		}
		cout<< ans<< endl;
	}
	
	return true;
}

int main(){
	cout.setf(ios::fixed);
	cout.precision(10);
	while(solve());

	return 0;
}

C++: #include <iostream>

using namespace std;

#define REP(i, n) for(int i=0; i<n; i++)

int inout[10010][1300];
int use[10010][1300];

int main(){
    while(true){
        int N, M, r;
        cin >> N >> M;
        if (N==0) break;

        cin >> r;

        REP(i, M){
            REP(j, 1261){
                inout[i][j] = 0;
                use[i][j] = 0;
            }
        }

        REP(i, r){
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            if (s==1) {
                inout[m-1][t] += 1;
            } else {
                inout[m-1][t] += -1;
            }
        }

        // syakutori method
        REP(i, M) {
            int status = 0;
            for (int t=540; t<=1260; t++) {
                status += inout[i][t];
                use[i][t] = status;
            }
        }

        int q;
        cin >> q;
        REP(i, q) {
            int ts, te, m;
            cin >> ts >> te >> m;
            int output = 0;
            for (int t=ts; t<te; t++) {
                if (use[m-1][t] > 0) {
                    output += 1;
                }
            }
            printf("%d\n", output);
        }
    }
}