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(){

	int N,M,r;
	while(cin >> N >> M && !(N == 0 && M == 0)){
		// Ô
		vector<pair<int,int> > Gakusei[10001];
		cin >> r;
		pair<int,int> pc[1001];
		for(int i = 0; i < r; i++){
			int t;
			int n;
			int m;
			int s;
			cin >> t >> n >> m >> s;
			if(s == 1){
				pc[n].first = t;
			}
			else{
				pc[n].second = t;
				Gakusei[m].push_back(pc[n]);
			}
		}

		int q;
		cin >> q;
		for(int i = 0; i < q; i++){
			int cnt = 0;
			int ts,te,m;
			cin >> ts >> te >> m;
			sort(Gakusei[m].begin(),Gakusei[m].end());
			int cTime = ts;
			for(int j = 0; j < Gakusei[m].size(); j++){
				// æÃ~ÉJnÔª¢©ç©Ä¢­
				if(cTime >= Gakusei[m][j].second || Gakusei[m][j].first >= te)
					continue;
				else{
					cnt += (min(te,Gakusei[m][j].second) - max(cTime,Gakusei[m][j].first));
					cTime = min(te,Gakusei[m][j].second);
				}
			}
			cout << cnt << endl;
		}

	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y) (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z) (cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y << ", "<< z << ")" << endl)

typedef long long ll;
typedef pair<int, int> pii;

int n, m, r, q;

#define MAX_N 1001
#define MAX_M 10001

#define MAX_T 1261
int timeforcalc[MAX_M][MAX_T];

int main(void)
{
	while (cin >> n >> m, n|m) {
		memset(timeforcalc, 0, sizeof(timeforcalc));
		vector< vector<pii> > time_of_students = vector< vector<pii> >(m, vector<pii>());
		cin >> r;
		rep(t, r) {
			int tt, nn, mm, ss;
			cin >> tt >> nn >> mm >> ss;
			time_of_students[mm-1].push_back(pii(nn, tt));
		}

		rep(student, m) {
			sort(ALL(time_of_students[student]));
			bool f = true;
			for (int i = 0; i < time_of_students[student].size(); i+= 2) {
				int a = time_of_students[student][i].second;
				int b = time_of_students[student][i+1].second;
				fill(timeforcalc[student]+a, timeforcalc[student]+b, 1);
			}
		}

		cin >> q;
		rep(t, q) {
			int ts, te, mm;
			cin >> ts >> te >> mm;
			int sum = 0;
			REP(i, ts, te)
				sum += timeforcalc[mm-1][i];
			cout << sum << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <functional>
#include <set>
#include <sstream>
#include <map>
#include <queue>
 
using namespace std;

long long gcd(long long x,long long y)
{
	return y==0 ? x : gcd(y,x%y);
}

int main()
{
	int N,M;
	while(cin>>N>>M,N){
		int r;
		cin>>r;

		//(PC,time)
		vector< vector<pair<int,int> > > in(M),out(M);

		for(int i=0;i<r;i++){
			int t,n,m,s;
			cin>>t>>n>>m>>s;

			if(s==0){
				out[m-1].push_back(make_pair(n,t));
			}else{
				in[m-1].push_back(make_pair(n,t));
			}
		}
		for(int i=0;i<M;i++){
			sort(in[i].begin(),in[i].end());
			sort(out[i].begin(),out[i].end());
		}

/*
		for(int i=0;i<N;i++){
			cout<<"student"<<i<<endl;
			for(int j=0;j<in[i].size();j++){
				cout<<"PC"<<in[i][j].first<<" in:"<<in[i][j].second<<endl;
				cout<<"PC"<<out[i][j].first<<" out:"<<out[i][j].second<<endl;
			}
		}
*/
		vector<vector<bool> > vb(M,vector<bool>(1261,false));


		for(int i=0;i<M;i++){
			for(int j=0;j<in[i].size();j++){
				int s=in[i][j].second,e=out[i][j].second;
				for(int k=s;k<e;k++) vb[i][k]=true;
			}
		}

//		for(int i=0;i<1261;i++) cout<<i<<" "<<vb[0][i]<<endl;

		int q;
		cin>>q;


		for(int i=0;i<q;i++){
			int s,e,m;
			cin>>s>>e>>m;
			int res=0;

			for(int j=s;j<e;j++){
				if(vb[m-1][j]){res++;}
			}

			cout<<res<<endl;
		}

	}

	return 0;
}

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

int main(){
	int N, n, M, m, r, q, s, t;
	while(cin >> N >> M){
		if(N == 0 && M == 0) break;
		vector<vector<int>> lgin(M+1,vector<int>(1262,0)); 
		cin >> r;
		for(int i = 0;i<r;i++){
			cin >> t >> n >> m >> s;
			if(s == 1){
				lgin[m][t+1] += 1;
			}
			else{
				lgin[m][t+1] -= 1;
			}
		}
		cin >> q;
		for(int i = 0;i<q;i++){
			cin >> n >> r >> m;
			if(lgin[m][0]==0) {
				for(int j = 540;j<1261;j++){
					lgin[m][j] = lgin[m][j-1]+lgin[m][j];
				}
				for(int j = 540;j<1261;j++){
					if(lgin[m][j]>0){
						lgin[m][j] = lgin[m][j-1] + 1;
					}
					else{
						lgin[m][j] = lgin[m][j-1];
					}
				}
				lgin[m][0] = 1;
			}
			cout << lgin[m][r] - lgin[m][n] << endl;
		}
	}
	return 0;
}