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 <vector>
#include <algorithm>
using namespace std;

int N, M;
const int MAX_N = 1000;
const int MAX_T = 1260 - 540;

void debug(int record[MAX_N+1][MAX_T+1], int max_t){
	cout << "debug : " << endl;
	for(int i = 1 ; i <= N ; i++ ){
		for(int j=0 ; j <= max_t ; j++ ){
			cout << record[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;
}


int main(){
	while( cin >> N >> M , N || M ){
		int r, q;
		int max_t = 0;
		
		// record[i][j] := i 番目のPCを時刻 j に使っている学生の番号
		// 使っていないときは 0 
		int record[MAX_N+1][MAX_T+1] = {0};
		int interval[MAX_N+1][MAX_T+1] = {0};
		
		cin >> r;
		for(int i=0 ; i < r ; i++ ){
			int t, n, m, s;
			cin >> t >> n >> m >> s;
			t -= 540;
			// ログインのとき -m , ログアウトのとき m
			if( s == 1 )
				record[n][t+1] = -m ;
			else
				record[n][t] = m ;
			max_t = max( max_t , t );
		}
		
		// record[][] を埋める
		for(int i=0 ; i <= N ; i++ ){
			bool flag = false;
			int m = 0;
			for(int j=0 ; j <= MAX_T ; j++ ){
				if( record[i][j] < 0 ){ // ログインしたとき
					flag = true;
					record[i][j] *= -1;
					m = record[i][j];
				}else if( record[i][j] > 0 ){ // ログアウトしたとき
					flag = false;
					interval[i][j] = m;
				}else if( flag ){ 
					record[i][j] = m;
					interval[i][j] = m;
				}
			}
		}
		//debug( record , max_t );
		
		// 質問数
		cin >> q;
		for(int i=0 ; i < q ; i++ ){
			int ts, te, m;
			cin >> ts >> te >> m;
			ts -= 540;
			te -= 540;
			// 時刻[ts,te] に学生 m が使っている合計時間を求める
			int cnt = 0;
			for(int t = ts ; t <= te ; t++ ){
				for(int n=0 ; n <= N ; n++ ){
					if( record[n][t] == m ){
						cnt++;
						if( t == ts && interval[n][t] == m )
							cnt--;
						//cout << "time : " << t + 540 << endl;
						break;
					}
				}
			}
			cout << cnt << endl;
		}
	}
}

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

using namespace std;

typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<pair<int, int> > VII;

int main() {
  
  int N, M;
  while(cin >> N >> M, N) {
    int r; cin >> r;
    VVI dat(M, VI(2014, 0));
    for(int i=0; i<r; i++) {
      int t, n, m, s; cin >> t >> n >> m >> s;
      m --;
      if(s) dat[m][t] ++;
      else dat[m][t] --;
    }
    
    for(int i=0; i<M; i++) {
      // imos PCs login: 1 logout: -1
      for(int j=0; j<2013; j++) {
	dat[i][j+1] += dat[i][j];
      }
    }
    
    VVI dat2(M, VI(2014, 0));
    
    for(int i=0; i<M; i++) {
      // 複数のPCをひとまとめにして、更に時間の累積和を取る
      for(int j=0; j<2013; j++) {
	dat2[i][j+1] = dat2[i][j] + (dat[i][j] > 0);
      }
    }
    
    int q;
    cin >> q;
    for(int i=0; i<q; i++) {
      int ts, te, m;
      cin >> ts >> te >> m; m --;
      cout << dat2[m][te] - dat2[m][ts] << endl;
    }
  }
  
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<tuple>

using namespace std;
typedef pair<int, int> pii;

int main(){

    while(1){
        int N, M;
        cin>> N>> M;
        if(!(N||M)) break;
        int r;
        cin>> r;

        int t[r], n[r], m[r], s[r];
        for(int i=0; i<r; i++){
            cin>> t[i]>> n[i]>> m[i]>> s[i];
            m[i]--;
        }
        int Q;
        cin>> Q;
        for(int i=0; i<Q; i++){
            int ss, ee, mm;
            cin>> ss>> ee>> mm;
            mm--;
            int a[1270]={};
            for(int i=0; i<r; i++){
                if(m[i]==mm){
                    if(s[i]) a[t[i]]+=1;
                    else a[t[i]]-=1;
                }
            }
            for(int i=1; i<1270; i++){
                a[i]+=a[i-1];
            }
            int k=0;
            for(int j=ss; j<ee; j++){
                k+=(a[j]>0);
            }
            cout<< k<< endl;
        }
    }

    return 0;
}

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

using namespace std;

const int MAX = 1300;
struct Info{int t,n,m,s;};
typedef pair<int,int> P;
vector<Info> V;
bool used[MAX];
int N,M;
int solve(int ts, int te, int m){

  int ans = 0;
  vector<Info> :: iterator ite = V.begin();

  while(1){
    if((*ite).t >= ts) break;
    if(ite != V.end()) ite++;
    else return 0;
  }

  int sta,end;

  vector<P> login;
  vector<P> logout;

  for(int i = 0; i < V.size(); i++)
    if(V[i].m == m && V[i].s == 1)
      login.push_back(P(V[i].n,V[i].t));

  for(int i = 0; i < V.size(); i++)
    if(V[i].m == m && V[i].s == 0)
      logout.push_back(P(V[i].n,V[i].t));

  sort(login.begin(),login.end());
  sort(logout.begin(),logout.end());

  for(int i = 0; i < login.size(); i++){
    int s = max(ts,login[i].second);
    int e = min(te,logout[i].second);
    fill(used+s,used+e,true);
  }

  for(int i = ts; i < te; i++)
    if(used[i]) ans++;
  return ans;
}

void init(){
  V.clear();
  memset(used,false,sizeof(used));
}

int main(){

  while(cin >> N >> M && N+M){
    init();
    int r;
    cin >> r;
    while(r--){
      Info f;
      cin >> f.t >> f.n >> f.m >> f.s;
      V.push_back(f);
    }
    int q;
    cin >> q;
    while(q--){
      int ts,te,m;
      cin >> ts >> te >> m;
      cout << solve(ts,te,m) << endl;
    }
  }
  return 0;
}