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>

using namespace std;
/*
typedef struct{
  int timer[720];

}Time;
*/
//Time timess[10000];

int main(){

  int n,m;
  int t;
  int ts,te;
  int n2,m2,s;
  int com;
  //PCへのログイン
  int login[1000]; 
  int sum;
  //学生がいくつログインしているか
  int s_ln[10000];
  int loginTime[10000][10];
  int logoutTime[10000][10];
  int count[10000];
  while(true){
 
    cin >>n>>m;

    if(n==0 && m==0)break;

    cin >>com;

    for(int i=0;i<com;i++){
      cin >>t>>n2>>m2>>s;

      if(s==1){
	if(login[n2]==0){
	  login[n2]=m2;
	  s_ln[m2]++;
	  if(s_ln[m2]==1){
	    loginTime[m2][count[m2]]=t;
	  }
	}
      }
      else if(s==0){
	login[n2]=0;
	s_ln[m2]--;
	if(s_ln[m2]==0){
	  logoutTime[m2][count[m2]]=t;

	  //	  cout<<"TEST"<<logoutTime[m2][count[m2]]<<" "<<loginTime[m2][count[m2]]<<endl;
	  count[m2]++;
	}
      }
    }
    cin >>com;
    
    for(int i=0;i<com;i++){
      sum = 0;
      cin >>ts>>te>>m2;

      for(int j=0;j<count[m2];j++){
	if(logoutTime[m2][j]>ts&&loginTime[m2][j]<te){
	  if(logoutTime[m2][j]>te)sum+=te-loginTime[m2][j];
	  else sum+=logoutTime[m2][j]-loginTime[m2][j];
	  if(loginTime[m2][j]<ts)sum+=loginTime[m2][j]-ts;
	}
      }
      cout <<sum<<endl;
    }

    for(int i=0;i<n;i++){
      count[i]=0;
      for(int j=0;j<10;j++){
	loginTime[i][j]=0;
 	logoutTime[i][j]=0;
      }
    }
    
  }
  return 0;
  
}

C++: #include<iostream>
using namespace std;

int main()
{
	int N, M;
	while (cin >> N >> M && N != 0 || M != 0)
	{
		int r;
		cin >> r;
		static int timeLine[10000 + 1][24 * 60 - 540];
		static int pcNumber[1000 + 1];
		for (int i = 0; i < M + 1; i++)
		{
			for (int j = 0; j < 24 * 60 -540;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)
			{
				pcNumber[n] = t;
			}
			else
			{
				for (int j = pcNumber[n] - 540; j < t- 540; 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 -540; j < end - 540; j++)
			{
				if (timeLine[m][j] == 1)
				{
					ans++;
				}
			}

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

C++: #include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
using namespace std;
#define INF 100000000

typedef long long ll;
typedef pair<int, int> P;

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

int main(void) {
    int N, M, r;
    while (1) {
        cin >> N >> M;
        if (N == 0 && M == 0) break;
        cin >> r;
        vector<P> students[M+1];
        for (int i = 0; i < r; i++) {
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            students[m].push_back(make_pair(t, s));
        }
        for (int i = 1; i <= M; i++) {
            sort(students[i].begin(), students[i].end());
        }
        int q;
        cin >> q;
        for (int i = 0; i < q; i++) {
            int s, e, m;
            cin >> s >> e >> m;
            int time = s, cnt = 0, ans = 0;
            for (int j = 0; j < students[m].size(); j++) {
                P p = students[m][j];
                if (cnt > 0) {
                    ans += min(p.first, e) - time;
                }
                time = max(s, min(p.first, e));
                cnt += p.second ? 1 : -1;
                if (time == e) break;
            }
            cout << ans << endl;
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define MOD 1000000007
#define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD)
typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec; typedef pair<int, int> pi;
int dd[] = { 0, 1, 0, -1, 0 };
#define all(u) begin(u),end(u)
#define mp make_pair
#define pb push_back


int main()
{
  while(true)
    {
      int N, M;
      scanf("%d%d", &N,&M);
      if(N == 0 && M == 0)break;
      //int A[M+1][1500]={0};
      int A[M+1][1500];
      memset(A,0,sizeof(A));
      int r;
      scanf("%d", &r);
      rep(i, r)
	{
	  int t, n, m, s;
	  scanf("%d%d%d%d", &t, &n, &m, &s);
	  if(s == 1)
	    {
	      A[m][t]++;
	    }
	  else
	    {
	      A[m][t+1]--;
	    }
	}
      repi(i, 1, M + 1)
	{
	  rep(t, 1300)
	    {
	      if(t == 0)continue;
	      A[i][t] += A[i][t-1];
	    }
	}

      int q;
      scanf("%d", &q);
      rep(i, q)
	{
	  int ts, te, m;
	  scanf("%d%d%d", &ts,&te,&m);
	  int start = -1;
	  int ans = 0;
	  repi(t, ts, te+1)
	    {
	      if(start == -1)
		{
		  if(A[m][t] > 0)
		    {
		      start = t;
		    }
		}
	      else
		{
		  if(A[m][t] <= 0)
		    {
		      ans += t - start - 1;
		      start = -1;
		    }
		}
	    }
	  if(start != -1)ans += te - start;
	  printf("%d\n", ans);
	}
    }
  return 0;
}