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;

int time[10001][1261];

int main(){

	while(1){
		int N,M,r,q;
		int t,n,m,s;
		int ts,te;

		cin>>N>>M;

		if(N==0&&M==0) break;

		cin>>r;
		for(int i=0;i<=M;i++){
			for(int j=0;j<=1260;j++){
				time[i][j]=0;
			}
		}
		for(int i=0;i<r;i++){
			cin>>t>>n>>m>>s;
			if(s==0) time[m][t]--;
			if(s==1) time[m][t]++;
		}
		for(int i=0;i<=M;i++){
			for(int j=1;j<=1260;j++){
				time[i][j]+=time[i][j-1];
			}
		}
		cin>>q;
		for(int i=0;i<q;i++){
			cin>>ts>>te>>m;
			int ans=0;
			for(int j=ts;j<te;j++){
				if(time[m][j]!=0) ans++;
			}
			cout<<ans<<endl;
		}
	}
}

C++: //#include <bits/stdc++.h>
#include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, x, n) for (int i = (x); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e10;
template<typename T> void cmax(T &a, T b) { a = max(a, b); }
template<typename T> void cmin(T &a, T b) { a = min(a, b); }

signed main() {
  int N, M;
  while (cin >> N >> M) {
    if (N == 0 && M == 0) break;
    int pc[1004][800] = {};
    // memset(pc, 0, sizeof(pc));
    int r;
    cin >> r;
    int t, n, m, s;
    while (r--) {
      cin >> t >> n >> m >> s;
      //  m--;
      n--;
      t -= 540;

      if (s == 1) { // login
        pc[n][t] = m;
      } else { // logout
        pc[n][t] = -m;
      }
    }
    rep(i, N) rep(j, 799) pc[i][j + 1] += pc[i][j];

    int q;
    cin >> q;
    int ts, te;
    while (q--) {
      cin >> ts >> te >> m;
      // m--;
      ts -= 540;
      te -= 540;
      int cnt = 0;
      loop(i, ts, te) {
        bool flag = false;
        rep(j, N) flag |= pc[j][i] == m;
        cnt += flag;
      }
      cout << cnt << endl;
    }
  }

  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main(){
	int n;
	while(cin>>n>>n,n){
		vector<vector<bool> >in(n,vector<bool>(2000));
		int m;
		cin>>m;
		vvi tmp(m,vi(4));
		rep(i,m)rep(j,4)cin>>tmp[i][j];
		rep(i,m)if(!tmp[i][3]){
			for(int j=i-1;j>=0;j--){
				if(tmp[i][1]==tmp[j][1]&&tmp[i][2]==tmp[j][2]){
					loop(k,tmp[j][0]+1,tmp[i][0]+1)in[tmp[i][2]-1][k]=true;	
					break;
				}
			}
		}
		//while(cin>>m,m)cout<<in[0][m]<<endl;
		cin>>m;
		while(m--){
			int a,b,c;
			cin>>a>>b>>c;
			int out=0;
			loop(i,a+1,b+1)if(in[c-1][i])out++;
			cout<<out<<endl;
		}
	}
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
 
int N, M;
int r, q;
class Record{
public:
  int t;
  int s;
  int n;
  int m;
  bool operator<(const Record &a) const {
    if(t != a.t) return t < a.t;
    if(s != a.s) return s < a.s;
    if(m != a.m) return m < a.m;
    return n < a.n;
  }
};
 
Record data[1000];
 
 
int solve(int &ts, int &te, int &m) {
  int i = 0;
  int state[1001];
  int ans = 0;
  Record tr[1000];
  int pr = 0;
  fill(state, state+1000, -1);
  int num = 0;
  for(i = 0; i < r; ++i) {
    if(data[i].m != m) continue;
    if(data[i].s) {
      ++num;
      if(num == 1) {
        tr[pr].s = 1;
        tr[pr].t = data[i].t;
        ++pr;
      }
    } else {
      --num;
      if(num == 0) {
        tr[pr].s = 0;
        tr[pr].t = data[i].t;
        ++pr;
      }
    }
  }
 
  for(i = 0; i < pr; ++i) {
    if(tr[i].s) {
      state[tr[i].n] = tr[i].t;
    } else {
      int tmp = state[tr[i].n];
      state[tr[i].n] = -1;
 
      if(tr[i].t < ts) continue;
      if(tmp > te) continue;
 
      if(tmp < ts && tr[i].t > te) ans += te - ts;
      else if(tmp < ts)              ans += tr[i].t - ts;
      else if(tr[i].t > te)        ans += te - tmp;
      else                           ans += tr[i].t - tmp;
    }
  }
  return ans;
}
 
main() {
  while((cin >> N >> M) && (N || M)) {
    cin >> r;
    for(int i = 0; i < r; ++i) {
      cin >> data[i].t >> data[i].n >> data[i].m >> data[i].s;
    }
    sort(data, data+r);
    cin >> q;
    for(int i = 0; i < q; ++i) {
      int ts, te, m;
      cin >> ts >> te >> m;
      cout << solve(ts, te, m) << endl;
    }
  }
}

C++: #include <iostream>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int main(){
	int n,p;
	while(cin>>n>>p,n){
		int q,a[p][1261-540],ch[p][1261-540],b,c,d,e;
		r(i,0,p)r(j,0,1261-540)ch[i][j]=a[i][j]=0;
		cin>>q;
		r(i,0,q){
			cin>>b>>c>>d>>e;
			b-=540;
			if(e==1){a[d-1][b]++;ch[d-1][b]=c;}
			else{
				for(int j=b-1;j>=0;j--){
					if(ch[d-1][j]==c)break;
					a[d-1][j]++;
				}
			}
		}
		cin>>q;
		while(q--){
			int s=0;
			cin>>b>>c>>d;
			b-=540;
			c-=540;
			r(i,b,c)if(a[d-1][i])s++;
			cout<<s<<endl;
		}
	}
}