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>
#include<iostream>
#include<stdio.h>
#include<bitset>
#include<algorithm>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<string>
#include<string.h>
#include<cmath>
#include<utility>
#include<functional>
#include<map>
#include<set>
#include<random>


#define FOR(i, a, b) for(long long int i=(a);i<=(b);i++)
#define RFOR(i, a, b) for(long long int i=(a);i>=(b);i--)
#define MOD 1000000007
#define INF 1000000000 //2000000000
#define LLINF 1000000000000000000 //9000000000000000000
#define PI 3.14159265358979

using namespace std;
typedef long long int ll;
typedef pair< long long int, long long  int> P;


int main(void) {

	while (1) {
		int N, M, r, q;
		int t, n, m, s;
		int ts, te;

		cin >> N >> M;
		vector <vector <int> > dp(M + 1, vector<int>(723, 0));

		cin >> r;
		FOR(i, 1, r) {
			cin >> t >> n >> m >> s;
			if (s == 0) {
				dp[m][t - 538]--;
			}
			else {
				dp[m][t - 539]++;
			}
		}
		FOR(i, 1, M) {
			FOR(j, 1, 722) {
				dp[i][j] += dp[i][j - 1];
			}
		}
		/*cout << endl;
		FOR(i, 1, 722) {
			cout << i+539 << " " << dp[1][i] << endl;
		}*/
		cin >> q;
		FOR(i, 1, q) {
			int total = 0;
			int first = -1, end = -1;
			cin >> ts >> te >> m;
			FOR(j, ts - 539, te - 539) {
				if (first == -1 && dp[m][j] >= 1) {
					first = j;
				}
				if (first != -1 && dp[m][j] <= 0) {

					end = j;
					//cout << "a" << first + 539 << " " << end + 539 << endl;
					total += end - first - 1;
					first = -1;
					end = -1;
				}
				if (j == te - 539 && first != -1) {
					//cout << "b" << first + 539 << " " << j + 539 << endl;
					total += j - first;
				}
			}
			cout << total << endl;
		}
	}

	return 0;
}

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

int main()
{
	int N,M; //N…PC数 M…学生数

	cin>>N>>M;
	while(N!=0 || M!=0){
		int member[10000][1260]={},log_in[10000]={},ans[1000]={};
		int r=0,q=0,t[1000]={},n[1000]={},m[1000]={},s[1000]={};
		//入力 r…記録数 q…質問数 t…利用時間 n…利用PC m…学生番号 s…ログイン(1)ログアウト(0)
		int qts[1000]={},qte[1000]={},qm[1000]={};
			//質問 qts…検索開始時間 qte…検索終了時間 qm…検索学籍番号
		
		cin>>r; //入力の数
		for(int i=0;i<r;i++) cin>>t[i]>>n[i]>>m[i]>>s[i];

		cin>>q; //質問の数
		for(int i=0;i<q;i++) cin>>qts[i]>>qte[i]>>qm[i];

		for(int time=540;time<=1260;time++){
			for(int input_num=0;input_num<r;input_num++){
				if(t[input_num]==time){
					if(s[input_num]==1) log_in[m[input_num]]++;
					if(s[input_num]==0) log_in[m[input_num]]--;
					if(log_in[m[input_num]]>0) member[m[input_num]][time]=1;
				}
			}
			for(int i=0;i<M;i++){
				if(log_in[i]>0) member[i][time]=1;
			}
		}
		for(int input_num=0;input_num<q;input_num++){
			for(int i=qts[input_num];i<=qte[input_num];i++){
				if(member[qm[input_num]][i]==1) ans[input_num]++;
			}
			cout<<ans[input_num]<<endl;
		}

		cin>>N>>M;
	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cmath>
#include <cassert>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <complex>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <utility>
#include <functional>
#include <iterator>

using namespace std;

#define dump(n) cerr<<"# "<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define iter(c) __typeof__((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

int main()
{
	for(int N,M;cin>>N>>M,N|M;){
		vvi a(M+1,vi(1261));
		int r; cin>>r;
		rep(i,r){
			int t,n,m,s;cin>>t>>n>>m>>s;
			if(s==0)
				a[m][t]--;
			else
				a[m][t]++;
		}
		repi(i,1,M+1) repi(j,541,1261)
			a[i][j]+=a[i][j-1];
		int q; cin>>q;
		rep(i,q){
			int ts,te,m; cin>>ts>>te>>m;
			int res=0;
			repi(j,ts,te)
				res+=!!a[m][j];
			cout<<res<<endl;
		}
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    while (1) {
        int N, M, r, q;
        cin >> N >> M;
        if (!N & !M) break;
        vector<vector<int>> imosu(M, vector<int>(1000, 0));
        vector<vector<int>> question;
        cin >> r;
        for (int i = 0; i < r; i++) {
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            if (s)
                imosu[m - 1][t - 540] += 1;
            else
                imosu[m - 1][t - 540] -= 1;
        }
        for (int i = 0; i < imosu.size(); i++) {
            for (int j = 1; j < imosu[i].size(); j++) {
                imosu[i][j] = imosu[i][j] + imosu[i][j - 1];
            }
        }

        cin >> q;
        for (int i = 0; i < q; i++) {
            int Ts, Te, m;
            cin >> Ts >> Te >> m;
            int ans = 0;
            for (int j = Ts - 540; j < Te - 540; j++) {
                if (imosu[m - 1][j] > 0) ans++;
            }
            cout << ans << endl;
        }
    }
}