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 <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 0;
  
}

C++: #include<iostream>
#include<cstring>
using namespace std;
int x[10000][1440],y[10000],n,m,r,q,i,j,a,b,c,d,s;
int main() {
	while (true) {
		memset(x, 0, sizeof(x));
		cin >> n >> m >> r; if (n == 0) { break; }
		for (i = 0; i < r; i++) {
			cin >> a >> b >> c >> d;
			if (d == 1) {
				y[c]++;
				for (j = a; j < 1440; j++) {
					x[c][j] = 1;
				}
			}
			else {
				y[c]--;
				if (y[c] == 0) {
					for (j = a; j < 1440; j++) {
						x[c][j] = 0;
					}
				}
			}
		}
		cin >> q;
		for (i = 0; i < q; i++) {
			s = 0;
			cin >> a >> b >> c;
			for (j = a; j < b; j++) {
				if (x[c][j] == 1) { s++; }
			}
			cout << s << endl;
		}
	}
}

C++: #include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
 
#define rep(i, n) for(int i=0; i<(n); ++i)

class history {
public:
    int pc, start, end;
    history(int p, int s, int e) {
        pc = p; start = s; end = e;
    }
};
int main(void){
    int N, M;
    int r, t, n, m, s, ts, te, q;
    vector<history> hist[1001];
    while(cin >> N >> M && N) {
        rep(i, 1001) hist[i].clear();
        cin >> r;
        rep(i, r) {
            cin >> t >> n >> m >> s;
            n--; m--;
            if(s==1) {
                hist[m].push_back(history(n, t, -1));
            } else {
                rep(j, hist[m].size()) {
                    if(hist[m][j].pc == n && hist[m][j].end == -1) {
                        hist[m][j].end = t;
                        break;
                    }
                }
            }
        }
        cin >> q;
        rep(i, q) {
            int ans = 0;
            cin >> ts >> te >> m;
            m--;
            int tmp[1265];
            rep(j, 1265) tmp[j] = 0;
            rep(j, hist[m].size()) {
                if(hist[m][j].end >= ts && hist[m][j].start <= te) {
                    for(int t=max(ts, hist[m][j].start); 
                            t <= min(te, hist[m][j].end); ++t) tmp[t] = 1;
                }
            }
            for(int j=540; j<1265; j++) {
                int state = 0;
                if(tmp[j] == 1) {
                    int start = j;
                    while(tmp[j]==1) j++;
                    ans += j - start - 1;
                }
            }
            cout << ans << endl;
        }
    }
}