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 <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  while(cin>>N>>M,N||M) {
    int use[M+1][1300]; CLR(use);
    int r; cin >> r;
    FOR(i,0,r) {
      int t, n , m , s;
      cin >> t >> n >> m >> s;
      if(s == 1) {
        use[m][t]++;
      } else {
        use[m][t]--;
      }
    }
    FOR(i,1,M+1) {
      FOR(j,0,1299) {
        use[i][j+1] += use[i][j];
      }
    }
    int q; cin >> q;
    FOR(i,0,q) {
      int ts, te, m;
      cin >> ts >> te >> m;
      int ans = 0;
      FOR(j,ts,te) {
        if(use[m][j] > 0) ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>
#include <iterator>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define FOR( v, c ) for ( auto &v : c )

#define EACH( it, c ) for ( auto it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()
#define DRANGE( c, p ) (c).begin(), (c).begin() + p, (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

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

	while ( true )
	{
		int n, m;
		cin >> n >> m;
		if ( !( n | m ) )
		{
			break;
		}

		int r;
		cin >> r;
		VVI used_by( n + 1, VI( 1260 + 1, 0 ) ); // used_by[ PC ][ 時刻 ] := 誰
		REP( i, 0, r )
		{
			int t, n, m, s;
			cin >> t >> n >> m >> s;
			used_by[n][t] = m * ( s ? 1 : -1 );
		}
		REP( i, 0, used_by.size() )
		{
			REP( j, 0, used_by[i].size() - 1 )
			{
				used_by[i][ j + 1 ] += used_by[i][j];
			}
		}

		int q;
		cin >> q;
		REP( query_num, 0, q )
		{
			int s, e, m;
			cin >> s >> e >> m;

			int res = 0;
			REP( t, s, e )
			{
				bool used = false;
				REP( i, 0, used_by.size() )
				{
					used |= used_by[i][t] == m;
				}
				res += used;
			}

			cout << res << endl;
		}
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int n, m, r, q;
int memo[10005][1270] = {0};
int sum[10005][1270] = {0};

void order();

int main() {
  while(1) {
    cin >> n >> m;
    if(n + m == 0) break;
    cin >> r;
    for(int i = 0; i < r; ++i) {
      int t, x, y, s;
      cin >> t >> x >> y >> s;
      --y;
      if(s)
        ++memo[y][t];
      else
        --memo[y][t];
    }
    order();
    cin >> q;
    for(int i = 0; i < q; ++i) {
      int from, to, num;
      cin >> from >> to >> num;
      --num;
      cout << sum[num][to - 1] - sum[num][from - 1] << endl;
    }
    for(int i = 0; i < m; ++i)
      for(int j = 0; j <= 1261; ++j)
        memo[i][j] = sum[i][j] = 0;
  }
  return 0;
}

void order() {
  for(int i = 0; i < m; ++i)
    for(int j = 1; j <= 1261; ++j) {
      memo[i][j] += memo[i][j - 1];
      sum[i][j] = sum[i][j - 1];
      if(memo[i][j] > 0) ++sum[i][j];
    }
}

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

typedef pair<int, int> P;

int N, M;

const int MAX_M = 10000;

int use_pc_num[MAX_M];          // 学生がログインしているPCの数
int use_start_time[MAX_M];      // 学生が現在使用しているPCの中で一番最初にログインした時刻

void solve(){
    int r, q;
    cin >> r;
    // initialize
    for(int i = 0; i < M; i++) use_pc_num[i] = use_start_time[i] = 0;
    vector<P> login_data[MAX_M];
    for(int i = 0; i < r; i++){
        int t, n, m, s;
        cin >> t >> n >> m >> s;
        if(s == 1){
            if(use_pc_num[m - 1]++ == 0) use_start_time[m - 1] = t;
        }else{
            if(use_pc_num[m - 1]-- == 1) login_data[m - 1].push_back(P(use_start_time[m-1], t));
        }
    }

    for(int i = 0; i < M; i++) sort(login_data[i].begin(), login_data[i].end());

    cin >> q;
    for(int i = 0; i < q; i++){
        int ts, te, m, ans = 0;
        cin >> ts >> te >> m;
        for(int i = 0; i < login_data[m - 1].size(); i++){
            P data = login_data[m - 1][i];
            // 重なっていないなら無視
            if(data.second < ts || data.first > te) continue;
            ans += min(te, data.second) - max(ts, data.first);
        }
        cout << ans << endl;
    }
}

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

    while(cin >> N >> M, N | M){
        solve();
    }
    return 0;
}