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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;

typedef struct
{
	int t, n, m, s;
} Log;

class sortLog
{
public:
	bool operator() (const Log& lhs, const Log& rhs)
	{
		if (lhs.n == rhs.n) {
			if (lhs.m == rhs.m) {
				return lhs.t < rhs.t;
			}
			return lhs.m < rhs.m;
		}
		return lhs.n < rhs.n;
	}
};

int main()
{
	int N, M;
	while (scanf("%d %d", &N, &M) != EOF) {
		if (N == 0 && M == 0)
			break;

		vector<Log> log;

		int r;
		scanf("%d", &r);
		for (int i = 0; i < r; ++i) {
			Log l;
			scanf("%d %d %d %d", &l.t, &l.n, &l.m, &l.s);
			log.push_back(l);
		}
		sort(log.begin(), log.end(), sortLog());

		vector<vector<int> > used(M+1, vector<int>(721, 0));
		for (int i = 0; i < log.size(); i += 2) {
			for (int s = log[i].t-540; s <= log[i+1].t-540; ++s)
				used[log[i].m][s] = 1;
		}

		int q;
		scanf("%d", &q);
		for (int i = 0; i < q; ++i) {
			int ts, te, m;
			scanf("%d %d %d", &ts, &te, &m);
			int ans = 0;
			for (int t = ts-540; t <= te-541; ++t) {
				if (used[m][t] && used[m][t+1])
					++ans;
			}
			printf("%d\n", ans);
		}
	}
	return 0;
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#pragma warning(disable:4996)
using namespace std;

int aa[10000][800];

int main() {
	while (1) {
		memset(aa, 0, sizeof(aa));
		int N, M; cin >> N >> M;
		if (!N)break;
		int R; cin >> R;
		for (int i = 0; i < R; ++i) {

			int t, n, m, s; cin >> t >> n >> m >> s;
			t -= 500;
			m-=1;
			if (s)aa[m][t]++;
			else aa[m][t]--;
		}
		
		for (int i = 0; i < 10000; ++i) {
			for (int j = 0; j < 799; ++j) {
				aa[i][j + 1] += aa[i][j];
			}
		}
		for (int i = 0; i < 10000; ++i) {
			for (int j = 0; j <= 799; ++j) {
				aa[i][j] =min(1,aa[i][j]);
			}
		}
		int Q; cin >> Q;
		for (int i = 0; i < Q; ++i) {
			int s, e, m; cin >> s >> e >> m;
			s -= 500; e -= 500;
			m--;
			int ans = 0;
			for (int j = s; j < e; ++j) {
				ans += aa[m][j];
			}
			cout << ans << endl;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>

#define rep(i, n) for(int i = 0; i < n; i++)

typedef long long ll;
using namespace std;
int mod = 1000000007;

int main(void){
    int N, M;
    int R;
    int Q;
    while(true){
        cin >> N >> M;
        if(N == 0) break;
        cin >> R;
        vector<int> tm(R);
        vector<int> no(R);
        vector<int> sid(R);
        vector<int> sw(R);
        rep(i, R) cin >> tm[i] >> no[i] >> sid[i] >> sw[i];
        cin >> Q;
        rep(i, Q){
            int ans = 0;
            int start, fin, m; cin >> start >> fin >> m;
            int record = 0;
            set<int> in_use;
            for(int j = tm[record]; j < fin; j++){
                if(record < R){
                    if(j == tm[record]){
                        if(sid[record] == m){
                            if(sw[record]){
                                in_use.insert(no[record]);
                            }
                            else in_use.erase(no[record]);
                        }
                        j--;
                        record++;
                        continue;
                    }
                }
                if(in_use.size() > 0){
                    if(start <= j) ans++;
                } 
            }
            cout << ans << endl;
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int main(){
	int n,p;
	while(cin>>n>>p,n){
		int q,a[p][1261-540],ch[p][1261-540],b,c,d,e;
		r(i,0,p)r(j,0,1261-540)ch[i][j]=a[i][j]=0;
		cin>>q;
		r(i,0,q){
			cin>>b>>c>>d>>e;
			/*b-=540;
			if(e==1){a[d-1][b]++;ch[d-1][b]=c;}
			else{
				for(int j=b-1;j>=0;j--){
					if(ch[d-1][j]==c)break;
					a[d-1][j]++;
				}
			}*/
		}
		cin>>q;
		while(q--){
			int s=0;
			cin>>b>>c>>d;
			b-=540;
			c-=540;
			r(i,b,c)if(a[d-1][i])s++;
			cout<<s<<endl;
		}
	}
}

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

int main()
{
  int N,M,r,q,t,n,m,s,S[10000][721],ts,te,time;

  while(1){
    cin>>N>>M;
    if(N==0 && M==0)break;
    for(int i=0;i<M;i++){
      for(int j=0;j<721;j++)S[i][j]=0;
    }
    cin>>r;
    for(int i=0;i<r;i++){
      cin>>t>>n>>m>>s;
      if(s==1)S[m-1][t-540]=1;
      else S[m-1][t-540]=-1;
    }
    for(int i=0;i<M;i++){
      for(int j=1;j<721;j++){
        S[i][j]=S[i][j]+S[i][j-1];
      }
    }
    cin>>q;
    for(int i=0;i<q;i++){
      cin>>ts>>te>>m;
      time=0;
      for(int j=ts-540;j<te-540;j++){
	if(S[m-1][j]>0)time++;
      }
      cout<<time<<endl;
    }
  }
  return 0;
}