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<bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) (v).begin(),(v).end()
#define int long long
typedef pair<int,int> P;

//-----------------------------------------------------------------------

int N,M,R,Q;
int A[1005][1005]; //生徒番号,時間

signed main()
{   
    cin.tie(0);
    ios::sync_with_stdio(false);

    while(cin>>N>>M,N){
        memset(A,0,sizeof(A));
        cin>>R;
        REP(i,R){
            int t,n,m,s;
            cin>>t>>n>>m>>s;
            t-=540; n--; m--;
            if(s) A[m][t]++;
            else A[m][t]--;
        }
        REP(i,M){
            REP(j,1000){
                A[i][j+1]+=A[i][j];
            }
        }
        cin>>Q;
        REP(i,Q){
            int s,e,m;
            cin>>s>>e>>m;
            s-=540; e-=540; m--;
            int res=0;
            for(int i=s;i<e;i++){
                res+=(A[m][i]>=1);
            }
            cout<<res<<endl;
        }
    }
}

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

using namespace std;

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n + m == 0) {
      break;
    }
    vector<vector<int>> pc(n);
    for (int i = 0; i < n; i++) {
      pc[i] = vector<int>(1261);
    }
    int r;
    cin >> r;
    for (int i = 0; i < r; i++) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      b--;
      if (d) {
        pc[b][a] = c;
      } else {
        pc[b][a] = -c;
      }
    }
    for (int i = 0; i < n; i++) {
      for (int t = 1; t < 1261; t++) {
        pc[i][t] += pc[i][t - 1];
      }
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
      int ts, te, tm;
      cin >> ts >> te >> tm;
     
      int ans = 0;
      for (int t = ts; t < te; t++) {
        for (int j = 0; j < n; j++) {
          if (pc[j][t] == tm) {
            ans++;
            break;
          }
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}

C++: 



//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1148&lang=jp




#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>
#include<numeric>




#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;




using namespace std;




int main()
{
	int N,M;




	while(cin >> N >> M && N)
	{
		int PC[N+1];
		int record[M+1][1261];
		fill(PC,PC+N+1,0);
		fill_n((int *)record, sizeof(record) / sizeof(int), 0);




		int R;
		cin >> R;
		rep(nth,R)
		{
			int t,n,m,s;
			cin >> t >> n >> m >> s;
			if(s == 1){
				PC[n] = t;
			}else{
				int ts = PC[n];
				REP(i,ts,t){
					record[m][i] = 1;
				}
				PC[n] = 0;
			}
		}




		int Q;
		cin >> Q;
		rep(nth,Q)
		{
			int ts, te, m;
			cin >> ts >> te >> m;
			int ans=0;
			REP(i,ts,te){
				if(record[m][i]){ ans+=1; }
			}
			cout << ans << endl;
		}
	}




	return 0;
}

C++: #include <cstdio>

#pragma warning(disable: 4996)

#define MAX_M 10000

int N, M, R, Q, t1, n1, m1, s1, t2, m2, n2, s2, s, e, m;

int record[MAX_M + 1][1260];

int main()
{
	while (true)
	{
		scanf("%d", &N);
		scanf("%d", &M);
		scanf("%d", &R);

		for (int i = 0; i < (R / 2); i++)
		{
			scanf("%d", &t1);
			scanf("%d", &n1);
			scanf("%d", &m1);
			scanf("%d", &s1);
			scanf("%d", &t2);
			scanf("%d", &n2);
			scanf("%d", &m2);
			scanf("%d", &s2);

			for (int j = t1; j < t2; j++)
			{
				record[m1][j] = 1;
			}
		}

		scanf("%d", &Q);

		for (int i = 0; i < Q; i++)
		{
			scanf("%d", &s);
			scanf("%d", &e);
			scanf("%d", &m);

			int count = 0;

			for (int j = s; j < e; j++)
			{
				count += record[m][j];
			}

			printf("%d\n", count);
		}

		for (int i = 1; i <= M; i++)
		{
			for (int j = 540; j < 1260; j++)
			{
				record[i][j] = 0;
			}
		}
	}

	return 0;
}

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

using namespace std;

int main(){

  while(1){
    int N,M,r;
    cin >> N >> M;
    if(!N and !M) break;
    cin >> r;
  
    int students[M+1][2000] = {};
    int numlogin[M+1] = {};
  
    // ?¨????
    for(int i=0; i<r; i++){ // 1000
      int t,n,m,s;
      cin >> t >> n >> m >> s;
      if(s == 0) { // ?????°??¢??????
	numlogin[m]--;
	if(numlogin[m] == 0){
	  for(int i=t-1; students[m][i] != 1; i--){ // 1000
	    students[m][i] = 1;
	  }
	}
      }
      else{ // ?????°??????
	numlogin[m]++;
	if(numlogin[m] == 1)
	  students[m][t] = 1;
      }
    }

    // ?´???????
    int sum[M+1][2000]={};
    for(int i=1; i<=M; i++){ // 100000
      for(int j=1; j<1500; j++){ // 1000
		sum[i][j] = sum[i][j-1] + students[i][j]; 
	//	students[i][j] += students[i][j-1];
      }
    }

    // ??????
    int  q;
    cin >> q;
    for(int i=0; i<q; i++){
      int ts,te,id;
      cin >> ts >> te >> id;
            cout << sum[id][te-1] - sum[id][ts] + students[id][ts] << endl;
      //      cout << students[id][te] - students[id][ts] << endl;
      //cout << students[id][te] - students[id][ts] << endl;
    }
  }
}