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<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>

using namespace std;

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

typedef long long ll;

#define MAX_T 1260
#define MAX_N 1000

int N,M;
int tb[MAX_N+1][MAX_T+1];

void solve(){
}

int main(){
  while(true){
    cin >> N >> M;
    if(N == 0 && M == 0) break;
    
    memset(tb, false, sizeof(tb));
    
    int r, q;
    
    cin >> r;
    rep(i,r){
      int t,n,m,s;
      cin >> t >> n >> m >> s;
      for(int j=t;j<=MAX_T;j++) {
        tb[n][j] = (s == 1 ? m : 0);
      }
    }

    cin >> q;
    rep(i,q){
      int ts, te, m;
      cin >> ts >> te >> m;
      //      cout << "q: " << ts << " " << te << " " << m << endl;
      int cnt = 0;
      for(int i=ts;i<te;i++){
        for(int j=0;j<=N;j++){
          if(tb[j][i] == m){ cnt++; break; }
        }
      }
      cout << cnt << endl;
    }
  }
}

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<functional>
#include<map>
#include<set>
#include<math.h>

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

using namespace std;

int n,m;

//時刻,学生
int data[1300][10003];

void solve() {
	rep(i,1300) rep(j,10003) data[i][j]=0;
	int r;
	cin>>r;
	rep(i,r) {
		int a,b,c,d;
		cin>>a>>b>>c>>d;
		if(d==1) {
			data[a][c]=1;
		}
		else {
			data[a][c]=-1;
		}
	}

	rep(i,m+1) {
		for(int j=1;j<1300;j++) {
			data[j][i]+=data[j-1][i];
		}
	}

	int q;
	cin>>q;
	rep(i,q) {
		int a,b,c;
		cin>>a>>b>>c;
		int ans=0;
		for(int j=a;j<b;j++) {
			//printf("[debug] %d,%d = %d\n",j,c,data[j][c]);
			if(data[j][c]!=0) ans++;
		}
		//cout<<"ans="<<ans<<endl;
		cout<<ans<<endl;
	}

}

int main() {

	while (cin>>n>>m)
	{
		if(n+m==0) {
			return 0;
		}
		solve();
	}

	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#include<vector>
#include<map>

#define REP(i, n, N) for(ll i=(n); i<(N); i++)
#define RREP(i, n, N) for(ll i=(N-1); i>=n; i--)
#define SPACEREP(lst,itr) for(auto itr=lst.begin(); itr!=lst.end();++itr){cout<<*itr<<(++itr!=anslist.end()?" ":"\n");itr--;}
#define CK(n, a, b) ((a)<=(n)&&(n)<(b))
#define ALL(v) (v).begin(),(v).end()
#define MCP(a, b) memcpy(b,a,sizeof(b))
#define p(s) cout<<(s)<<endl
#define p2(a, b) cout<<(a)<<" "<<(b)<<endl
#define v2(T) vector<vector<T>>
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const ll inf = 1e18;


int main(){
    while(1){
		int N,M;
		cin >> N >> M;
		if(N==0&&M==0)break;

		int r,q,t,n,m,s,ts,te;
		int pc_min[1001][720]={0};
		cin >> r;
		//いもす法っぽいやーつ
		REP(i,0,r){
			cin >> t >> n >> m >> s;
			t=t-540;
			if(s){
				pc_min[n][t]=m;
			}else{
				pc_min[n][t]=-m;
			}
		}

		//累積和とってログイン履歴完成
		REP(i,1,N+1){
			REP(j,1,720){
				pc_min[i][j]+=pc_min[i][j-1];
			}
		}
		/**
		REP(i,1,N+1){
			REP(j,1,720){
				cout << pc_min[i][j];
			}
			cout << endl;
		}
		**/

		cin >> q;
		REP(i,0,q){
			int ans=0;
			cin >> ts >> te >> m;
			ts=ts-540;
			te=te-540;
			REP(j,ts,te){
				bool flag=false;
				REP(k,1,N+1){
					if(pc_min[k][j]==m)flag=true;
				}
				if(flag)ans++;
			}
			p(ans);
		}
	}
}

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