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<stdio.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <string>

using namespace std;

int n, m, r, q, t[10000][1300];

int main() {

	
	while (1) {

		for (int i = 0; i < 10000; i++) {
			for (int j = 0; j < 1300; j++) {
				t[i][j] = 0;
			}
		}
		cout << "fin1" << endl;

		cin >> n >> m;
		if (n==0 && m==0) break;

		cin >> r;
		for (int i = 0; i < r; i++) {
				int tt, tn, tm, ts;
				cin >> tt >> tn >> tm >> ts;
				if (ts == 1) t[tm-1][tt]++;
				else t[tm - 1][tt+1]--;
		}
		cout << "fin2" << endl;

		for (int i = 0; i < m; i++) {
			for (int j = 1; j <1300; j++) {
				//if(750<j && j<800) cout << "t[i][j-1]" << t[i][j - 1] << endl;
				t[i][j] += t[i][j-1];
			}
		}

		cin >> q;
		for (int i = 0; i < q; i++) {
			int ts, te, tm;
			cin >> ts >> te >> tm;
			int ans=0;
			bool flag = false;
			for (int j = ts; j < te; j++) {
				if (t[tm - 1][j]>0) ans++, flag = true;
				if (flag && t[tm - 1][j]==0) ans--,flag = false;
			}
			cout << ans << endl;
		}
	}

	return 0;
}

C++: #include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <tuple>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <iterator>
#include <array>
#include <memory>
#include <stack>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;
#include<string.h>
int main(){
    int N,M;
    while(cin>>N>>M, N+M){
        int r; cin>>r;
        vector<vector<pii>>v(M+1,vector<pii>());
        vvi pclog(N+1,vi(M+1,0));
        rep(i,r){
            int t,n,m,s; cin>>t>>n>>m>>s;
            if(s){
                pclog[n][m]=t;
            }else{
                v[m].push_back(pii(pclog[n][m],t));
                pclog[n][m]=0;
            }
        }
        int q; cin>>q;
        rep(j,q){
            int ts,te,m; cin>>ts>>te>>m;
            ll ans=0;
            pii gotiusa;
            int imos[100000]={0};
            memset(imos, 0,sizeof(imos));
            rep(hoge,v[m].size()){
                imos[v[m][hoge].first]++;
                imos[v[m][hoge].second]--;
            }
            for(int i=0; i<10000;i++)
                if(i)imos[i]+=imos[i-1];
            for(int i=ts; i<te;i++)
                if(imos[i]>0)ans++;
            cout<<ans<<endl;
        }
    }
}

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

using namespace std;

int main() {
	int N, M;
	int r, t, n, m, s, q, ts, te;
	while (cin >> N >> M && (N || M)) {
		vector<vector<int> > cnt(M, vector<int>(750, 0));
		vector<vector<int> > ans(M, vector<int>(750, 0));
		cin >> r;
		for (int i = 0; i < r; ++i) {
			cin >> t >> n >> m >> s;
			m--, t -= 539;
			if (s) cnt[m][t]++;
			else cnt[m][t]--;
		}
		for (int i = 0; i < M; ++i) {
			for (int j = 0; j < 750; ++j) {
				if (j) cnt[i][j] += cnt[i][j - 1];
				if (cnt[i][j]) ans[i][j]++;
				if (j) ans[i][j] += ans[i][j - 1];
			}
		}
		cin >> q;
		for (int i = 0; i < q; ++i) {
			cin >> ts >> te >> m;
			m--, ts -= 540, te -= 540;
			cout << ans[m][te] - ans[m][ts] << endl;
		}
	}

	return 0;
}