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 <map>
#include <algorithm>
#include <vector>
#include <stack>
#include <math.h>

using namespace std;

#define T 800

int main(int argc, char const *argv[])
{
	int N,M;
	int r;
	int t,n,m,s;
	int q;
	int ts,te;
	int ans;

	while(1){
		cin>>N>>M;
		if(N+M==0) break;

		map<int,vector<int> > lio;

		for(int i1=0;i1<M;i1++){
			vector<int> v(T);
			fill(v.begin(), v.end(),0);
			lio[i1]=v;
		}

		cin>>r;
		for(int i1=0;i1<r;i1++){
			cin>>t>>n>>m>>s;
			vector<int> v=lio[m-1];
			if(s==1) {
				v[t-540]+=1;
			}
			else if(s==0) v[t-540]+=-1;
			lio[m-1]=v;
			
		}


		for(int i1=0;i1<M;i1++){
			vector<int> v=lio[i1];
			for(int i2=1;i2<T;i2++){
				v[i2]=v[i2-1]+v[i2];
			}
			lio[i1]=v;
		}

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

			vector<int> v=lio[m-1];
			ans=0;
			for(int i2=ts-540;i2<te-540;i2++){
				if(v[i2]>0) {
					ans++;
					//cout<<i2<<" "<<v[i2]<<" ";
				}
			}
			cout<<ans<<endl;
		}



	}
	return 0;
}

C++: #include <iostream>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stdlib.h>
#include <string.h>

using namespace std;

int N,M,r,q;
typedef struct{
	int t,n,m,s;
} LOG;

bool time_table[10000+1][1260 - 540 + 1];

void solve()
{
	memset(time_table,0,sizeof(time_table));

	cin>>r;
	vector<LOG> logs(r);
	for(int i = 0;i < r; i++){
		cin >> logs[i].t >> logs[i].n >> logs[i].m >> logs[i].s;
	}

	int count[10001]={0},t[10001] = {0};
	for(int j = 0; j < r; j++){
		int m = logs[j].m;
		if(count[m] == 1 && logs[j].s == 0)
			for(int k = t[m]; k < logs[j].t - 540; k++)
				time_table[m][k] = true;

		if(count[m] == 0 && logs[j].s == 1)
			t[m] = logs[j].t - 540;
		count[m] += (logs[j].s == 1) ? 1 : -1; //ツカツウツδ督トツづーツ堕敖個クツつウツつケツづゥ
	}

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


}

int main()
{
	while(cin >>N >>M,(N||M)){
		solve();
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <map>
#include <vector>
#include <utility>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>

#define rep(i,j,k) for(int i=(int)j;i<(int)k;i++)
#define itrep(i,x) for(auto i=(x).begin(); i!=(x).end();i++)
#define Sort(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define vec vector
#define INF INT_MAX
#define INFL 1e18
#define MOD 1000000007
#define pb push_back
#define MP make_pair
#define PI 3.1415926535
typedef long long int ll;
typedef std::pair<int,int> P;
int D=1;
int dx[4]={0,1,0,-1},dy[4]={-1,0,1,0};
const int limit=60;
using namespace std;

int main(){
    int N,M;
    while(cin>>N>>M&&N&&M){
        vector<vector<int> > v(M,vector<int>(1260-540+2,0));
        int r; cin>>r;
        rep(i,0,r){
            int t,n,m,s;
            cin>>t>>n>>m>>s;
            m--;
            t-=540;
            if(s==1) v[m][t]=1;
            else v[m][t]=-1;
        }
        rep(i,0,M)rep(j,0,1260-539)v[i][j+1]+=v[i][j];
        rep(i,0,M)rep(j,0,1260-539){
            if(v[i][j+1])v[i][j+1]=1;
            v[i][j+1]+=v[i][j];
        }

        int q; cin>>q;
        rep(i,0,q){
            int ts,te,m;
            cin>>ts>>te>>m;
            m--;
            ts-=540; te-=540;
            cout<<v[m][te]-v[m][ts]<<endl;
        }
    }
    return 0;
}