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 loginTime[10000][1000];
 int logoutTime[10000][1000];
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 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<1000;j++){
	loginTime[i][j]=0;
 	logoutTime[i][j]=0;
      }
    }
    
  }
  return 0;
  
}

C++: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "iomanip"
#include "cmath"

using namespace std;

const long long int MOD = 1000000007;

long long int N, M, K, H, W, L, R;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	cin >> N >> M;
	while (N) {
		cin >> K;
		vector<vector<bool>>flag(M, vector<bool>(721));
		vector<queue<int>>Q(N);
		for (int i = 0; i < K; i++) {
			int a, b, c, d;
			cin >> a >> b >> c >> d;
			a -= 540;
			b--;
			c--;
			if (d) {
				Q[b].push(a);
			}
			else {
				for (int j = Q[b].front(); j < a; j++) {
					flag[c][j] = true;
				}
				Q[b].pop();
			}
		}
		cin >> K;
		for (int i = 0; i < K; i++) {
			int a, b, c;
			cin >> a >> b >> c;
			a -= 540;
			b -= 540;
			c--;
			int ans = 0;
			for (int j = a; j < b; j++) {
				ans += flag[c][j];
			}
			cout << ans << endl;
		}
		cin >> N >> M;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>

using namespace std;
 
#define NMAX 1000
#define tMAX 1260
int main(void){
	
	int N,M;
	int r;
	int use[NMAX+1][tMAX+1];
	int t,n,m,s;
	int q;
	int ts,te;
	int cnt;
	bool f;

	while(1){
		cin>>N>>M;
		if(N==0&&M==0) break;

		for(int i=1;i<=N;i++){
			for(int j=0;j<=tMAX;j++){
				use[i][j]=0;
			}
		}

		cin>>r;
		for(int i=0;i<r;i++){
			cin>>t>>n>>m>>s;
			if(s==1) use[n][t]=m;
			else use[n][t]=-1*m;
		}

		for(int i=1;i<=N;i++){
			for(int j=1;j<=tMAX;j++){
				use[i][j]+=use[i][j-1];
			}
		}

		cin>>q;

		for(int i=0;i<q;i++){
			cin>>ts>>te>>m;
			cnt=0;
			for(int j=ts;j<te;j++){
				f=false;
				for(int i=1;i<=N;i++){
					if(use[i][j]==m) f=true; 
				}
				if(f) cnt++;
			}
			cout<<cnt<<endl;
		}

	}

	return 0;
}

C++: #include <iostream>

using namespace std;
int C[1000]; // コンピュータのログイン時間を覚えておく

char T[10000][1260+1];

int main(){
  int N; // PC数(1000)
  int M; // 学生数(10000)
  int r; // 利用記録数
  int q; // 質問数
  int  k,m,t;
  int n, s, ts, te;

  while (1) {
    for (int i = 0; i < 10000; i++) {
      for (int j = 0; j <= 1260; j++) {
	       T[i][j] = 0;
      }
    }
    cin >> N >> M;
    if (N == 0 && M == 0)
      return 0;

    cin >> r; // 利用記録数
    //cout << r << endl;
    for (int i = 0; i < r; i++) {
      cin >> t >> n >> m >> s;
      //cout << t << ","<< n << ","<< m << ","<< s ;
      // t 時刻 (540～1260)
      // n PC番号
      // m 学生番号
      // s (1:ログイン、0:ログアウト)
      if (s == 1) {
	       C[n] = t; // PC番号
      } else if (s == 0) {
	       for ( int j = C[n]; j < t; j++){
	          T[m][j] = 1;
          }
      }
    }
      cin >> q; // 質問数
    for (int i = 0; i < q; i++) {
      cin >> ts >> te  >> m;
      k = 0;

      for (int j = ts; j < te; j++) {
        if (T[m][j] == 1){
          k++;
         }
       }
        cout << k  << endl;

     }
   }
    return 0;
}