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][1000];
  int logoutTime[10000][1000];
  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<cstring>
using namespace std;
int x[10000][1440],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) {
				for (j = a; j < 1440; j++) {
					x[c][j] = 1;
				}
			}
			else {
				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 vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair

#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)

const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

int main(){
    int n, m;
    while(cin >> n >> m, n+m){
        int r;
        cin >> r;
        vector<vector<int>> li, lo;
        rep(i, r){
            vector<int> v(4);
            cin >> v[2] >> v[0] >> v[1] >> v[3];
            if(v[3] == 1) li.push_back(v);
            else  lo.push_back(v);
        }
        sort(li.begin(), li.end());
        sort(lo.begin(), lo.end());
        int o;
        cin >> o;
        rep(i, o){
            int s, e, m;
            cin >> s >> e >> m;
            bool b[2000];
            fill(b, b+2000, false);
            for(int i=0; i< li.size(); i++){
                if(li[i][1] == m){
                    for(int j=li[i][2]; j <= lo[i][2]; j++) b[j] = true;
                }
            }
            int ret = 0;
            for(int i=s; i <= e; i++){
                if(b[i]) ret++;
                if(b[i]&&(!b[i+1] || i == e))ret--;
            }
            cout << ret << endl;
        }
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#define M 10001
using namespace std;

typedef struct {
  int n;  // PC ÌÔ
  int in,out; // OCEOAEgÌ
} U;


int main(){
  int n,m, r,q;
  int tt,nn,mm,ss;
  int ts,te, ans;
  vector<U> students[M];
  U u;

  while( cin>>n>>m && (n||m) ){

    for( int i=0;i<M;i++ )
      students[i].clear();

    cin >> r;
    for( int i=0;i<r;i++ ){
      cin >> tt >> nn >> mm >> ss;
      if( ss==1 ){  // login
	u.n=nn; u.in=tt; u.out=-1;
	students[mm].push_back( u );
      }else{ // logout
	for( int j=students[mm].size()-1;j>=0;--j ){
	  if( students[mm][j].n==nn ){
	    students[mm][j].out = tt; break;
	  }
	}
      }
    }

    // pÔÌvZ
    for( int i=1;i<=m;i++ ){
      bool f;
      do{
	f=false;
	for( int j=0;j<students[i].size();j++ ){
	  for( int k=0;k<students[i].size();k++ ){
	    if( j==k ) continue;

	    if( students[i][j].in <= students[i][k].in &&
		students[i][k].in <= students[i][j].out ){

	      if( students[i][j].out < students[i][k].out )
		students[i][j].out = students[i][k].out;
	      students[i].erase( students[i].begin()+k );
	      f=true; continue;
	    }
	  }
	}
      }while( f );
    }

    cin >> q;
    for( int loop=0;loop<q;loop++ ){
      cin >> ts >> te >> mm;
      ans=0;

      if( false && q==5 )
      cout << ts << " "<< te << " " << mm << endl;
      for( int i=0;i<students[mm].size();i++ ){
	u = students[mm][i];
	
	if( false && q==5 )
	  cout << "  PC" << u.n << " login:" << u.in
	       << " logout:" << u.out << endl;

	if( u.in<=ts && u.out>ts )
	  ans += ( min(u.out,te)-ts );
	else if( u.in>ts && u.in<te )
	  ans += ( min(te,u.out)-u.in );
      }
      cout << ans << endl;
    }
  }

  return 0;
}