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 MAX 1100

int main(){

  while(1){
    int N,M, r, q, time[MAX][2000] = {};
    int t, n, m, s, ts, te;
    cin>>N>>M;
    if(!N&&!M)break;
    cin>>r;
    for(int i = 1; i <= r; i++){
      cin>>t>>n>>m>>s;
      if(!s){
	time[n][t+1] -= m;
      }else {
	time[n][t] += m;
      }
    }

    for(int j = 540; j <= 1260; j++){
      for(int k = 1; k <= N; k++){//cout<<time[k][j]<<" ";cout<<endl;
	time[k][j] += time[k][j-1];
      }
    }
   /* for(int j = 780; j <= 870; j++){cout<<j<<" ";
      for(int k = 1; k <= N; k++)cout<<time[k][j]<<" ";cout<<endl;
    }*/
 
    cin>>q;
    for(int i = 1; i <= q; i++){
      int ans = 0, temp = 0;
      cin>>ts>>te>>m;
      for(int j = ts; j <= te; j++){
	for(int k = 1; k <= N; k++){
	  int l = j;
	  if(time[k][j] == m){//cout<<k<<" "<<j<<" "<<m<<endl;
	    while(time[k][l] && l <= te)l++;
	    ans += l-j-1; //cout<<ans<<" "<<l<<" "<<j<<endl;
	    if(m == time[k][j-1] && j-1 >= ts)ans++; 
	    j = l-1;
	   // temp = j;
	    break;
	  }
	}
      }
      cout<<ans<<endl;
    }
  }

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <utility>
#include <stdlib.h>

using namespace std;

int main(void)
{
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) break;
    int r;
    cin >> r;
    int time[r], pc[r], stu[r], log[r];
    

    for (size_t i = 0; i < r; i++) {
      cin >> time[i] >> pc[i] >> stu[i] >> log[i];   
    }
    int que;
    cin >> que;
    int ts[que], te[que], qm[que];
    for (size_t i = 0; i < que; i++) {
      cin >> ts[i] >> te[i] >> qm[i];
    }
    //??\??????

    for (size_t k = 0; k < que; k++) {

      int start = ts[k];
      int end = te[k];
      int diff = end - start;
      int student = qm[k];
      vector<int> timecount;
      timecount.resize(diff);
      for (size_t i = 0; i < diff; i++)
	timecount[i] = 0;

      for (size_t i = 0; i < r; i++) {
	int intime, outtime;

	//question????????????recode ??????????????°????????????
	if (student == stu[i] && log[i]) {
	  intime = time[i];
	  //pc????????§???
	  int pcnum = pc[i]; 
	  int j = i + 1;
	  //?????°??¢???????¨?????????¢???
	  while (true) {
	    if(student == stu[j] && pcnum == pc[j]) {
	      outtime = time[j];

	      if (intime < start) intime = start;
	      if (outtime > end) outtime = end;
	      intime -= start;
	      outtime -= start;

	         
	      for (int l = intime; l < outtime; l++)
		timecount[l] = 1;
	      break;
	    }
	    j++;
	    if (j > r)
	      break;
	  }
	}
      }
      int cnt = 0;
      for (size_t i = 0; i < diff; i++)
      if (timecount[i])
	cnt++;
      cout << cnt << endl;
     

    }
  }
  
  
  return true;
  
}

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 << 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<iostream>

using namespace std;

#define MMAX 10000
#define tMAX 1260
int main(){

	int N, M;
	int r;
	int t, n, m1, s;
	int q;
	int ts, te, m2;
	int num,cnt;
	int data[MMAX+1][tMAX+1];

	while (1){
		cin >> N >> M;
		if (N == 0 && M == 0) break;
		for (int i = 1; i <= M; i++){
			for (int j = 0; j <= tMAX; j++){
				data[i][j] = 0;
			}
		}
		cin >> r;
		for (int i = 0; i < r; i++){
			cin >> t >> n >> m1 >> s;
			if (s == 0) data[m1][t]--;
			else data[m1][t]++;
		}
		cin >> q;
		for (int i = 1; i <= M; i++){
			num = 0;
			for (int j = 0; j <= tMAX; j++){
				num += data[i][j];
				/*data[i][j] = num;*/
			}
		}
		for (int i = 0; i < q; i++){
			cin >> ts >> te >> m2;
			cnt = 0;
			for (int j = ts; j < te; j++){
				if (data[m2][j] >= 1) cnt++;
			}
			cout << cnt << endl;
		}
	}
	return 0;
}