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++: 4 2
10
775 1 1 1
780 4 2 1
790 2 1 1
800 2 1 0
810 3 1 1
820 1 1 0
825 3 1 0
860 1 1 1
870 4 2 0
880 1 1 0
1
780 870 1
13 15
12
540 12 13 1
600 12 13 0
650 13 15 1
660 12 15 1
665 11 13 1
670 13 15 0
675 11 13 0
680 12 15 0
1000 11 14 1
1060 12 14 1
1060 11 14 0
1080 12 14 0
3
540 700 13
600 1000 15
1000 1200 11
1 1
2
600 1 1 1
700 1 1 0
5
540 600 1
550 650 1
610 620 1
650 750 1
700 800 1
0 0

C++: #include<bits/stdc++.h>
#define LL long long
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPA(i,n) for(int i=1;i<(n);++i)
#define PII pair<int,int>
#define PLI pair<long long, int>
#define PLL pair<long long, long long>
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9)
#define INFLL ((LL)1e18)
#define ALL(x) (x).begin(),(x).end()
#define CTOI(x) (x - 'A') 
#define ctoi(x) (x - 'a')
#define BIT(x) (1 << (x))
using namespace std;

LL modinv(LL a){
    LL b = MOD, u = 1, v = 0;
    while(b){
        LL t = a / b;
        a -= t * b;swap(a, b);
        u -= t * v;swap(u, v);
    }
    u%=MOD;
    if(u < 0) u += MOD;
    return u;
}

LL exp(LL a, LL b){
    LL res = 1;
    LL sum = a;
    while(b){
        if(b&1)res = (res * sum) % MOD;
        sum = (sum * sum) % MOD;
        b>>=1;
    }
    return res;
}

int count(int x){
    int res = 0;
    while(x){
        res += x & 1;
        x>>=1;
    }
    return res;
}

vector<int> getsosu(int N){
    vector<int> res;
    vector<int> check(N+1, true);
    for(int i=2;i<=N;++i){
        if(check[i]){
            res.push_back(i);
            for(int j=i*2;j<=N;j+=i){
                check[j]=false;
            }
        }
    }
    return res;
}

int main(){
    int N, M;
    while(cin >> N >> M && N){
        int r;
        cin >> r;
        vector<vector<PII> > student(M+1);
        REP(i, r){
            int a, b, c, d;
            cin >> a >> b >> c >> d;
            student[c].push_back(PII(a, d * 2 - 1));
        }
        int q;
        cin >> q;
        REP(i, q){
            int s, e, m;
            cin >> s >> e >> m;
            vector<int> imos(1261, 0);
            REP(j, student[m].size()){
                imos[student[m][j].first]+=student[m][j].second;
            }
            int ans = 0;
            int sum = 0;
            REP(j, 1261){
                sum += imos[j];
                if(s <= j && j < e && sum > 0)++ans;
            }
            cout << ans << endl;
        }
    }
    return 0;
}

C++: // AOJ 1148

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef pair <int, int> P;
int data[1500][1500];

int main(void)
{
	int N, M;
	while (cin >> N >> M, N && M){
		for (int i = 0; i < 1500; i++){
			for (int j = 0; j < 1500; j++){
				data[i][j] = 0;
			}
		}

		int r;
		int t, n, m, s;
		cin >> r;
		for (int i = 0; i < r; i++){
			cin >> t >> n >> m >> s;
			for (int j = t; j < 1500; j++){
				data[n][j] = (s == 1) ? m : 0;
			}
		}

		int q;
		int ts, te;
		cin >> q;
		for (int i = 0; i < q; i++){
			cin >> ts >> te >> m;
			int ret = 0;
			for (int i = 0; i <= N; i++){
				for (int j = ts; j < te; j++){
					if (data[i][j] == m) ret++;
				}
			}
			cout << ret << endl;
		}
	}

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct log{
  int t,n,m,s;
  bool operator<(const log&a)const{
    return n==a.n ? (m == a.m? t < a.t : m < a.m) : n<a.n;
  }
};
int main(){
  int nu,me,i,j,k,o,f;
  cin >> nu >> me;
  while(nu!=0||me!=0){
    int r,q;
    cin >> r;
    int t,n,m,s,ts,te;
    vector <log> logs;
    log l;
    for(i=0;i<r;i++){
      cin >> l.t >> l.n >> l.m >> l.s ;
      logs.push_back(l);
    }
    cin >> q;
    sort(logs.begin(),logs.end());
    i=0;

    int a;
    for(a=0;a<q;a++){
      cin >> ts >> te >> m ;
      
      j=0;
      o=0;
      f=0;
      for(i=ts;i<te;i++){
	f=0;
	k=0;
	for(j=0;j<logs.size();j+=2){
	  if(logs[j].m==m&&logs[j].t<=i&&i<logs[j+1].t){
	    f=1;
	  }
	}
	
	o+=f;
      }
      cout << o << endl;
    }
    
    cin >> nu >> me;
  }
  return 0;
}

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

using namespace std;

typedef pair<int, int> time_log;

struct pc_log{
	int m;
	time_log log;
};

typedef struct pc_log pc_log;

int main() {
	int N, M;
	while(cin >> N >> M && N && M) {
		int r; cin >> r;
		vector<vector<pc_log> > log(N);

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

			if(s) { // login
				pc_log tmp;
				tmp.m = m;
				tmp.log.first = t;
				tmp.log.second = -1;

				log[n-1].push_back(tmp);
			}
			else { // logout
				log[n-1][log[n-1].size()-1].log.second = t;
			}
		}

		int q; cin >> q;

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

			vector<time_log> st_log;
			for(int j = 0; j < N; j++) {
				for(int k = 0; k < log[j].size(); k++) {
					if(log[j][k].m == m) {
						st_log.push_back(log[j][k].log);
					}
				}
			}
			sort(st_log.begin(), st_log.end());

			int res = 0;
			int cur = 0;

			for(int j = 0; j < st_log.size(); j++) {
				while(st_log[j].second < ts) j++;
				while(st_log[j].second < cur) j++;

				res += min(te, st_log[j].second) - max(max(ts, cur), st_log[j].first);

				cur = st_log[j].second;
			}
			cout << res << endl;
		}
	}

	return 0;
}