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++: l = (1260-540+1)
while True:
	N,M = map(int,input().split(" "))
	if N == 0 and M == 0:
		break
	T = [[0]*l for _ in range(M)]
	r = int(input())
	for i in range(r):
		t,n,m,s = map(int,input().split(" "))
		if s == 1:
			T[m-1][t-540] += 1
		else:
			T[m-1][t-540] -= 1

	q = int(input())
	for i in range(q):
		t1,t2,m = map(int,input().split(" "))
		T_range = []
		flg = True if T[m-1][0] >= 1 else False
		lb = 0
		for j in range(l-1):
			T[m-1][j+1] += T[m-1][j]
			if flg and T[m-1][j+1] == 0:
				T_range.append((lb+ 540,j+1+540))
				flg = False
			elif (not flg) and T[m-1][j+1] >= 1:
				flg = True
				lb = (j+1)
		if flg:
			T_range.append((lb+540,l+540))
		ans = 0
		for a,b in T_range:
			if not (b < t1 or t2 < a):
				ans += (min(t2,b) - max(t1,a))

		print(ans)

C++: #include<iostream>
using namespace std;
typedef struct TimeRange Range;
struct TimeRange
{
	int start;
	int end;
};

int main()
{
	int N, M;
	while (cin >> N >> M && N != 0 || M != 0)
	{
		int r;
		cin >> r;
		static int timeLine[10000 + 1][24 * 60];
		static int startTime[1000 + 1][10000 + 1];
		for (int i = 0; i < 10000 + 1; i++)
		{
			for (int j = 0; j < 24 * 60;j++)
			{
				timeLine[i][j] = 0;
			}
		}

		for (int i = 0; i < r; i++)
		{
			int t, n, m, s;
			cin >> t >> n >> m >> s;
			if (s == 1)
			{
				startTime[n][m] = t;
			}
			else
			{
				for (int j = startTime[n][m]; j < t; j++)
				{
					timeLine[m][j] = 1;
				}
			}
		}

		int q;
		cin >> q;

		for (int i = 0; i < q; i++)
		{
			int start, end, m;
			cin >> start >> end >> m;
			int ans = 0;
			for (int j = start; j < end; j++)
			{
				if (timeLine[m][j] == 1)
				{
					ans++;
				}
			}

			cout << ans << endl;
			
		}
	}
	return 0;
}

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

using namespace std;

typedef pair<int, int> time_log;

struct pc_log{
	int m;
	time_log log;
};

typedef struct pc_log pc_log;

int main() {
	int N, M;
	while(cin >> N >> M && N && M) {
		int r; cin >> r;
		vector<vector<pc_log> > log(N);

		for(int i = 0; i < r; i++) {
			int t, n, m, s; cin >> t >> n >> m >> s;

			if(s) { // login
				pc_log tmp;
				tmp.m = m;
				tmp.log.first = t;
				tmp.log.second = -1;

				log[n-1].push_back(tmp);
			}
			else { // logout
				log[n-1][log[n-1].size()-1].log.second = t;
			}
		}

		int q; cin >> q;

		for(int i = 0; i < q; i++) {
			int ts, te, m; cin >> ts >> te >> m;

			vector<time_log> st_log;
			for(int j = 0; j < N; j++) {
				for(int k = 0; k < log[j].size(); k++) {
					if(log[j][k].m == m) {
						st_log.push_back(log[j][k].log);
					}
				}
			}
			sort(st_log.begin(), st_log.end());

			int res = 0;
			int cur = ts;

			for(int j = 0; j < st_log.size(); j++) {
				if(cur > te) {
					break;
				}

				if(!j) {
					if(ts < st_log[j].first) {
						res += (st_log[j].second - st_log[j].first);
					}
					else if(st_log[j].second > ts){
						res += (st_log[j].second - ts);
					}
					cur = st_log[j].second;
				}
				else {
					if(cur < st_log[j].first) {
						res += (st_log[j].second - st_log[j].first);
						cur = st_log[j].second;
					}
					else { // cur >= st_log[j].first
						if(cur < st_log[j].second) {
							res += (st_log[j].second - cur);
							cur = st_log[j].second;
						}
					}
				}
			}

			if (cur > te) {
				res -= cur - te;
			}
			cout << res << endl;
		}
	}

	return 0;
}

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

using namespace std;

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

  while(true){
    int n,m;cin >>n>>m;
    if(n==0) break;
    int r,q;
    int time[m+1][1300];
    for(int i=0;i<m+1;i++)for(int j=0;j<1300;j++)time[i][j]=0;
    cin>>r;
    for(int i=0;i<r;i++){
      int t,pc,stu,s;cin>>t>>pc>>stu>>s;
      if(s) time[stu][t]++;
      else time[stu][t]--;
    }
    for(int i=1;i<=m;i++)for(int j=1;j<1265;j++)time[i][j]+=time[i][j-1];
    cin>>q;
    for(int i=0;i<q;i++){
      int ts,te,stu;cin>>ts>>te>>stu;
      int res=0;
      for(int j=ts;j<te;j++)if(time[stu][j])res++;
      cout <<res<<endl;
    }
  }
}