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 <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

int time_m[10001][1261]; // time[i][j] : i?????????????????????j????????°????????¶???

int main(){
	int N, M, r, q;
	while(true){
		cin >> N >> M;
		if(N == 0 || M == 0){
			break;
		}
		cin >> r;
		int t_, n_, m_, s_;
		for(int i = 0; i <= M; i++){
			for(int t = 540; t < 1261; t++){
				time_m[i][t] = 0;
			}
		}
		for(int loop = 0; loop < r; loop++){
			cin >> t_ >> n_ >> m_ >> s_;
			if(s_ == 1){
				time_m[m_][t_]++;
			}else{
				time_m[m_][t_]--;
			}
		}
		for(int i = 0; i <= M; i++){
			for(int t = 541; t < 1261; t++){
				time_m[i][t] += time_m[i][t - 1];
			}
		}
		cin >> q;
		int ts, te;
		for(int loop = 0; loop < q; loop++){
			cin >> ts >> te >> m_;
			int ans;
			ans = 0;
			for(int t = ts; t < te; t++){
				if(time_m[m_][t] > 0){
					ans++;
				}
			}
			printf("%d\n", ans);
		}
	}
	return 0;
}

C++: #include <iostream>
#define NUMBER 10000
#define TIME 1261
using namespace std;

int record[NUMBER][TIME];
int rec[1000][4];
int quest[50][3];

void answer_question(int q) {
  for (int i = 0; i < q; i++) {
    int student = quest[i][2];
    int count = 0;
    for (int j = quest[i][0]; j <= quest[i][1]; j++) {
      int t = -1;
      if (record[student][j]) {
	for (int k = j; k <= quest[i][1]; k++) {
	  if (record[student][k] == 0) {
	    t = k - 1;
	    break;
	  }
	  if (k == quest[i][1]) t = k;
	}
	count += t - j;
	j = t;
      }
    }
    cout << count << endl;
  }
}

void record_search(int r) {
  for (int i = 0; i < r; i++) {
    if (rec[i][3] == 0) continue;
    for (int j = i + 1; j < r; j++) {
      if (rec[j][1] == rec[i][1] && rec[j][2] == rec[i][2] && rec[j][3] == 0) {
	for (int k = rec[i][0]; k <= rec[j][0]; k++) {
	  record[rec[i][2]][k]++;
	}
	goto label;
      }
    }
  label: ;
  }
}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) return 0;

    int r, q;
    cin >> r;
    for (int i = 0; i < r; i++)
      cin >> rec[i][0] >> rec[i][1] >> rec[i][2] >> rec[i][3];

    cin >> q;
    for (int i = 0; i < q; i++)
      cin >> quest[i][0] >> quest[i][1] >> quest[i][2];

    for (int i = 0; i < r; i++)
      for (int j = 0; j < TIME; j++)
	record[i][j] = 0;
    record_search(r);
    answer_question(q);
  }
}

C++: #include<bits/stdc++.h>
using namespace std;

#define MAX 1100
#define TS 590
#define TE 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 = TS; j <= TE; 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, f = 0;
      cin>>ts>>te>>m;
      for(int j = ts; j <= te; j++){
	for(int k = 1; k <= N; k++){
	  if(time[k][j] == m){//cout<<k<<" "<<j<<" "<<m<<endl;
		int l = j;
	    while(time[k][l+1] && l + 1 <= te)l++;
	    ans += l-j; //cout<<ans<<" =ans  "<<l<<" "<<j<<endl;
	    if(m == time[k][j-1] && j-1 >= ts && l + 1 <= te)ans++;
	    //if(l != f)f = 0;
	    j = l+1;
	   // temp = j;
	    k = 1;
	  }
	}
      }
      cout<<ans<<endl;
    }
  }

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#define M 10000
using namespace std;

struct P{
  int ts,te,n;
};

int main(){
  int n,m, r,q, te,ts, t,nn,mm,s;
  vector<P> v[M+1];
  P p;

  while( cin >> n >>m && (n||m) ){
    cin >> r;
    for( int i=1;i<=m;i++ ) v[i].clear();

    for( int i=0;i<r;i++ ){
      cin >> t >> nn >> mm >> s;

      if( s==1 ){
	p.ts=t; p.te=-1; p.n=nn; v[mm].push_back( p );
      }else{
	for( int j=v[mm].size()-1;j>=0;j-- ){
	  if( v[mm][j].n==nn && v[mm][j].te==-1 ){
	    v[mm][j].te=t; break;
	  }
	}
      }
    }
    /*
    for( int i=1;i<=m;i++ ){
      for( unsigned int j=0;j<v[i].size();j++ )
	cout << "("<<v[i][j].ts << "-"<< v[i][j].te <<"),"
	     << v[i][j].n <<" " ;
      if( v[i].size()>0 )
	cout << endl;
    }
    cout << "CNV"<<endl;
    */
    // ツつゥツづ板づ?づ?つ「ツづゥツ篠楪甘板づーツ鳴ウツつュツつキ
    for( int i=1;i<=m;i++ ){
      for( unsigned int j=1;j<v[i].size();j++ ){
	int k=j-1;
	if( v[i][k].te >= v[i][j].ts ){
	  v[i][k].te = max( v[i][k].te, v[i][j].te );
	  v[i].erase( v[i].begin()+j );
	  j--;
	}
      }

    }
    /*
    for( int i=1;i<=m;i++ ){
      for( unsigned int j=0;j<v[i].size();j++ )
	cout << "("<<v[i][j].ts << "-"<< v[i][j].te <<"),"
	     << v[i][j].n <<" " ;
      if( v[i].size()>0 )
	cout << endl;
    }
    */
    cin >> q;
    for( int lp=0;lp<q;lp++ ){
      cin >> ts>>te >> m;
      int ans=0;
      //      cout << ts << "->" << te << " m="<<m<<endl;

      for( unsigned int i=0;i<v[m].size();i++ ){
	if( v[m][i].te<=ts ) continue;
	if( v[m][i].ts>=te ) continue;
	ans += ( min( te,v[m][i].te )- max(v[m][i].ts,ts) );
	//	cout << " "<< ans << endl;
      }

      cout << ans << endl;
    }
  }

  return 0;
}