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 <algorithm>

#include <bitset>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <utility>
#include <array>
#include <complex>
#include <valarray>

#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <chrono>
#include <random>
#include <numeric>


using namespace std;
//#define int long long

typedef long long ll;
typedef unsigned long long ull;
//typedef unsigned __int128 HASH;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll,int> plli;
typedef pair<double, int> pdbi;
typedef pair<int,pii> pipii;
typedef pair<ll,pll> plpll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<vector<int>> mat;

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n);i>0;i--)
#define rrep2(i,a,b) for (int i=(a);i>b;i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()

const ll hmod1 = 999999937;
const ll hmod2 = 1000000000 + 9;
const int INF = 1<<30;
const ll mod = 1000000000 + 7;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

#define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod
#define inside(y, x, h, w) (0 <= (y) && (y) < (h) && 0 <= (x) && (x) < (w)) ? true : false

int pc[1005][1261], st[10005][1005];

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    while (true) {
        int N, M;
        cin >> N >> M;
        if (N == 0) break;
        rep(i, 1005)rep(j, 1261) pc[i][j] = -1;
        
        int r;
        cin >> r;
        rep(i, r) {
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            if (s == 1) {
                st[m][n] = t;
            }
            else {
                rep2(j, st[m][n], t) {
                    pc[n][j] = m;
                }
            }
        }
        int q;
        cin >> q;
        rep(i, q) {
            int ts, te, m;
            cin >> ts >> te >> m;
            int ans = 0;
            rep2(now, ts, te) {
                rep2(j, 1, N + 1) {
                    if (pc[j][now] == m) {
                        ans++;
                        break;
                    }
                }
            }
            cout << ans << endl;
        }
    }

}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int d[1000];
int e[1000];
int f[1000];
int g[1000];
int main(){
	int a,b;
	while(scanf("%d%d",&a,&b),a+b){
		int c;
		scanf("%d",&c);
		for(int i=0;i<c;i++){
			scanf("%d%d%d%d",d+i,e+i,f+i,g+i);
		}
		int p;
		scanf("%d",&p);
		for(int i=0;i<p;i++){
			int q,r,s;
			scanf("%d%d%d",&q,&r,&s);
			int last=0;
			int ret=0;
			int now=0;
			for(int j=0;j<c;j++){
				if(f[j]==s){
					if(g[j]){
						if(now==0)last=d[j];
						now++;
					}else{
						now--;
						if(now==0){
							if(q<=d[j]&&d[j]<=r)ret+=d[j]-max(q,last);
							if(d[j]>r&&last<=r)ret+=r-max(q,last);
						}
					}
				}
			}
			printf("%d\n",ret);
		}
	}
}

C++: #include <iostream>
#include <vector>

using namespace std;

class student {
public:
    int st[51], ed[51];
    int num, cnt;
    student() { num = 0; cnt = 0; }
};

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

    while (cin >> N >> M, N || M) {
        int r;  // 利用記録数
        int q;  // 質問数
        vector<student> std(N);

        // 登録
        cin >> r;
        for (int i = 0; i < r; i++) {
            int t, n, m, s;  // s(1: ログイン, 0: ログアウト), 時刻tにPCnで学生mが
            cin >> t >> n >> m >> s;

            if (s == 1) { // ログイン
                if (std[m].cnt == 0) {  // まだログインしていない
                    std[m].st[std[m].num] = t;
                }
                std[m].cnt++;
            }
            else { // ログアウト
                if (std[m].cnt == 1) 
                    std[m].ed[std[m].num++] = t;
                std[m].cnt--;
            }
        }

        // 質問
        cin >> q;
        for (int i = 0; i < q; i++) {
            int ts, te, m;
            int ans = 0;

            cin >> ts >> te >> m;

            for (int j = 0; j < std[m].num; j++) {
                if (std[m].ed[j] <= ts)
                    continue;
                if (std[m].st[j] >= te)
                    break;
                
                if (std[m].st[j] <= ts && std[m].ed[j] >= te) {
                    ans += te - ts;
                    break;
                }
                if (std[m].st[j] <= ts && std[m].ed[j] < te)
                    ans += std[m].ed[j] - ts;
                else if (std[m].st[j] >= ts && std[m].ed[j] >= te) {
                    ans += te - std[m].st[j];
                    break;
                }
                else if (std[m].st[j] >= ts && std[m].ed[j] < te) 
                    ans += std[m].ed[j] - std[m].st[j];
            }
            cout << ans << endl;
        }
    }

    return 0;
}