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 <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

int dx[]={1, 0, -1, 0};
int dy[]={0, -1, 0, 1};

int main(){
    while(1){
        int N, M; cin>>N>>M;
        if(N == 0) return 0;

        vector<vector<bool>> use(M+10, vector<bool>(1300, false));
        vector<int> cnt(M+10, 0);
        vector<int> startTime(M+10, -1);
        int r; cin>>r;
        while(r--){
            int t, n, m, s; cin>>t>>n>>m>>s;
            if(s == 1){
                cnt[m]++;
                if(cnt[m] == 1){
                    startTime[m] = t;
                }
            }
            else{
                cnt[m]--;
            }

            if(cnt[m] == 0){
                int endTime = t;
                for(int i=startTime[m]; i<endTime; i++){
                    use[m][i] = true;
                }
            }
        }

        int q; cin>>q;
        while(q--){
            int s, t, m; cin>>s>>t>>m;
            int ans=0;
            for(int i=s; i<t; i++){
                if(use[m][i]) ans++;
            }

            cout << ans << endl;
        }
    }
}

C++: #include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio> 
#include <cstring>

#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define rangeRep(i, n, m) for(int (i) = (n); (i) < (m); (i)++)
struct Student {
	int t, n, m, s;
};

constexpr int timeOffset = 540;
//[PC][time] = Student
int dp[1000][1260 - timeOffset] = { 0 };


class Solve {
public:
	Solve() {};
	~Solve() {};

	void solver() {
		std::vector<std::vector<int>> results;
		while (1) {
			int n, m, r, q;
			std::vector<Student> students;
			std::cin >> n >> m;
			if (!n) break;

			std::memset(dp, 0, sizeof(dp));
			results.push_back(std::vector<int>());
			//login info
			std::cin >> r;
			Student s;
			rep(i, r) {
				//time, PC, student, isLogin
				std::cin >> s.t >> s.n >> s.m >> s.s;
				//login
				if (s.s) {
					//set login start time
					dp[s.n - 1][s.t - timeOffset] = s.m;
				}//logout
				else {
					//Fill until login start time
					int time = s.t - timeOffset - 1;
					while (dp[s.n - 1][time] == 0) {
						dp[s.n - 1][time--] = s.m;
					} 
				}
			}

			//question [ts, te]
			std::cin >> q;
			rep(i, q) {
				int num = 0;
				int ts, te, m;
				std::cin >> ts >> te >> m;
				ts -= timeOffset; te -= timeOffset;
				//Check if logged in on any PC.
				rangeRep(j, ts, te) {
					rep(k, n) {
						if (dp[k][j] == m) {
							num++;
							break;
						}
					}
				}
				results.back().push_back(num);
			}
		}

		for (auto& x : results) {
			for (auto& y : x) {
				std::cout << y << std::endl;
			}
		}
	}
private:

};

int main() {
	Solve solve;
	solve.solver();
	return 0;
}

C++: #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;
}

C++: #include <cstdio>

#pragma warning(disable: 4996)

#define MAX_N 1000
#define MAX_M 10000

int N, M, R, Q, t1, n1, m1, s1, s, e, m;

int record[MAX_M + 1][1260];

int cpu_[MAX_N + 1];

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

		if (N == 0 && M == 0) { break; }

		scanf("%d", &R);

		for (int i = 0; i < R; i++)
		{
			scanf("%d", &t1);
			scanf("%d", &n1);
			scanf("%d", &m1);
			scanf("%d", &s1);

			if (cpu_[n1] == 0)
			{
				cpu_[n1] = t1;
			}
			else
			{
				for (int j = cpu_[n1]; j < t1; j++)
				{
					record[m1][j] = 1;
				}

				cpu_[n1] = 0;
			}
		}

		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 <= M; i++)
		{
			for (int j = 540; j < 1260; j++)
			{
				record[i][j] = 0;
			}
		}
	}

	return 0;
}