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 <bits/stdc++.h>

using namespace std;
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os;}
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {os << "(" << p.first << ", " << p.second << ")"; return os;}

void solve() {
    int N, M;
    while(cin >> N >> M, N) {
        int R;
        cin >> R;
        vector<vector<int>> T(M, vector<int>(1261));
        for (int i = 0; i < R; i++) {
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            m--;
            if (s == 1) {
                T[m][t]++;
            } else {
                T[m][t]--;
            }
        }
        for(int i = 0; i < M; i++) {
            ll sum = 0;
            for(int j = 0; j < 1261; j++) {
                sum += T[i][j];
                T[i][j] = sum;
            }
        }
        int Q;
        cin >> Q;
        for (int q = 0; q < Q; q++) {
            int ts, te, m;
            cin >> ts >> te >> m;
            m--;
            int ans = 0;
            for(int j = ts; j < te; j++) {
                if (T[m][j] > 0) {
                    ans++;
                }
            }
            cout << ans << endl;
        }

    }
}

int main() {
    #ifdef LOCAL_ENV
    cin.exceptions(ios::failbit);
    #endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(16);
    
    solve();

    #ifdef LOCAL_ENV
    assert(cin.rdstate() & ios_base::eofbit);
    #endif
}

C++: //
//  main.cpp
//  Logout Records
//
//  Created by x15071xx on 2017/06/21.
//  Copyright ?? 2017??´ AIT. All rights reserved.
//

#include <iostream>

using namespace std;

int main(int argc, const char * argv[]) {
    
    
    while (1) {
        
        int N,M,r,t,n,m,s,q,ts,te,i,j,k;
        int PC[1000][720]={0};
        
        cin >> N >> M;
        
        if (N == 0 && M == 0) {
            break;
        }
        
        //?????°???????????°??¢???????¨????
        cin >> r;
        
        for (i = 0; i < r ; i++) {
            cin >> t >> n >> m >> s;
            
            t -= 540;
            
            //????????????PCn?????????m????????°??????????????°??¢?????????
            if (s == 1) {
                //login
                PC[n-1][t] = m;
            } else {
                //logout
                for (j = t-1; PC[n-1][j] != m; j--) {
                    PC[n-1][j] = m;
                }
            }
            
        }
        
        //?§£??? ????????£??????????????????????????????
        cin >> q;
        
        for (i = 0; i < q ; i++) {
            int count = 0;
            cin >> ts >> te >> m;
            ts -= 540;
            te -= 540;
            for (k = ts; k < te; k++) {
                for (j = 0; j < N; j++) {
                    if (PC[j][k] == m) {
                        count++;
                        break;
                    }
                }
            }
            
            cout << count << endl;
        }
        
    }
    return 0;
}

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

using namespace std;

int main(){

  while(1){
    int N,M,r;
    cin >> N >> M;
    if(!N and !M) break;
    cin >> r;
  
    int students[M+1][2000] = {};
    int numlogin[M+1] = {};
  
    // ?¨????
    for(int i=0; i<r; i++){ // 1000
      int t,n,m,s;
      cin >> t >> n >> m >> s;
      if(s == 0) { // ?????°??¢??????
	numlogin[m]--;
	if(numlogin[m] == 0){
	  for(int i=t-1; students[m][i] != 1; i--){ // 1000
	    students[m][i] = 1;
	  }
	}
      }
      else{ // ?????°??????
	numlogin[m]++;
	if(numlogin[m] == 1)
	  students[m][t] = 1;
      }
    }

    // ?´???????
    int sum[M+1][2000]={};
    for(int i=1; i<=M; i++){ // 100000
      for(int j=1; j<1500; j++){ // 1000
		sum[i][j] = sum[i][j-1] + students[i][j]; 
	//	students[i][j] += students[i][j-1];
      }
    }

    // ??????
    int  q;
    cin >> q;
    for(int i=0; i<q; i++){
      int ts,te,id;
      cin >> ts >> te >> id;
            cout << sum[id][te-1] - sum[id][ts] + students[id][ts] << endl;
      //      cout << students[id][te] - students[id][ts] << endl;
      //cout << students[id][te] - students[id][ts] << endl;
    }
  }
}