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 <vector>
#include <algorithm>

using namespace std;

int main() {
	
	while (1) {
		int M, N, r, t, n, m, s, q, ts, te, b[10001] = {}, ans;
		vector< vector<int> > a(10001);
		cin >> N >> M;
		if (N == 0 && M == 0) {
			break;
		}
		cin >> r;
		for (int i = 0; i < r; i++) {
			cin >> t >> n >> m >> s;
			if (s == 1) {
				if (b[m] == 0) {
					a[m].push_back(t);
					b[m]++;
				}
				else {
					b[m]++;
				}
			}
			else {
				if (b[m] == 1) {
					a[m].push_back(t);
					b[m]--;
				}
				else {
					b[m]--;
				}
			}
		}/*
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < a[i].size(); j+=2) {
				cout << a[i][j] << ' ' << a[i][j + 1] << endl;
			}
		}*/
		cin >> q;
		for (int i = 0; i < q; i++) {
			ans = 0;
			cin >> ts >> te >> m;
			for (int j = 0; j < a[m].size(); j += 2) {
				if ((ts <= a[m][j] && a[m][j] <= te) || (ts <= a[m][j + 1] && a[m][j + 1] <= te)) {
					int ss = max(ts, a[m][j]);
					int se = min(te, a[m][j + 1]);
					ans += se - ss;
				}
				else if (a[m][j] <= ts && te <= a[m][j+1]) {
					ans += te - ts;
				}
			}
			cout << ans << endl;
		}
	}
	return 0;
}

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

using namespace std;

inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}

typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

const double EPS = 1e-10;

const int ENDTIME = 1260;

int main() {
	int N, M;

	while(cin >> N >> M, N) {
		vvi pc(N, vi(ENDTIME, 0));
		vvi user(M, vi(ENDTIME, 0));

		int r;
		cin >> r;
		REP(i, r) {
			int t, m, n, s;
			cin >> t >> n >> m >> s;

			if(s == 1) {
				FOR(j, t, ENDTIME) {
					pc[n-1][j] = m;
				}
			}
			else {
				FOR(j, t, ENDTIME) {
					pc[n-1][j] = 0;
				}
			}

		}

		REP(i,N) {
			FOR(j, 0, ENDTIME) {
				if(pc[i][j]) {
					user[pc[i][j]-1][j] = 1;
				}
			}
		}

		int q;
		cin >> q;
		REP(i, q) {
			int ts, te, m;
			cin >> ts >> te >> m;

			int riyoujikan = 0;

			FOR(t, ts, te) {
				if(user[m-1][t]) {
					riyoujikan++;
				}
			}

			cout << riyoujikan << endl;
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#include <queue>
#include <functional>

using namespace std;

typedef pair<int, int> P;

int main() {
	int n, m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		vector< vector<P> > hyou(m+1);
		vector< vector<P> > hyou2(m+1);
		vector<int> com(n+1);
		int r;
		cin >> r;
		int t, nn, mm, s;
		for (int i = 0; i < r; i++) {
			cin >> t >> nn >> mm >> s;
			if (s == 1) {
				com[nn] = t;
			} else {
				P p;
				p.first = com[nn];
				p.second = t;
				hyou2[mm].push_back(p);
			}
		}
		for (int i = 1; i < m+1; i++) {
			if (!hyou2[i].empty()) {
				priority_queue< P, vector<P>, greater<P> > pq;
				for (int j = 0; j < hyou2[i].size(); j++) {
					pq.push(hyou2[i][j]);
				}
				while (!pq.empty()) {
					P p = pq.top();
					pq.pop();
					if (hyou[i].empty()) {
						hyou[i].push_back(p);
					} else {
						if (hyou[i][hyou[i].size()-1].second < p.first) {
							hyou[i].push_back(p);
						} else if (hyou[i][hyou[i].size()-1].second > p.second) {
						} else {
							hyou[i][hyou[i].size()-1].second = p.second;
						}
					}
				}
			}
		}
		int q;
		cin >> q;
		int ts, te;
		for (int i = 0; i < q; i++) {
			cin >> ts >> te >> mm;
			int time = 0;
			if (!hyou[mm].empty()) {
				for (int j = hyou[mm].size()-1; j >= 0; j--) {
					if (hyou[mm][j].first < te) {
						time += min(te, hyou[mm][j].second) - max(ts, hyou[mm][j].first);
					}
				}
			}
			cout << time << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <array>
#include <map>
using namespace std;
map<int,array<int,1260-540+1>> imos;

int main(){
    int N,M;
    while(cin>>N>>M,N){
        imos.clear();
        int r;
        cin>>r;
        for(int i=0;i<r;i++){
            int t,n,m,s;
            cin>>t>>n>>m>>s;
            t-=540;
            if(imos.find(m)==imos.end()){
                imos[m];
            }
            if(s){
                imos[m][t]++;
            }else{
                imos[m][t]--;
            }
        }

        for(auto &e:imos){
            auto &v=e.second;
            for(int i=1;i<=1260-540;i++){
                v[i]+=v[i-1];
            }
        }

        int q;
        cin>>q;
        for(int i=0;i<q;i++){
            int ts,te,m;
            cin>>ts>>te>>m;
            ts-=540; te-=540;
            int cnt = 0;
            for(int t=ts;t<te;t++) cnt+=!!imos[m][t];
            cout<<cnt<<endl;
        }
    }
}