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 rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define MOD 1000000007
#define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD)
typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec; typedef pair<int, int> pi;
int dd[] = { 0, 1, 0, -1, 0 };
#define all(u) begin(u),end(u)
#define mp make_pair
#define pb push_back


int main()
{
  while(true)
    {
      int N, M;
      scanf("%d%d", &N,&M);
      if(N == 0 && M == 0)break;
      //int A[10010][1500]={0};
      int A[10010][1500];
      memset(A,0,sizeof(A));
      int r;
      scanf("%d", &r);
      rep(i, r)
	{
	  int t, n, m, s;
	  scanf("%d%d%d%d", &t, &n, &m, &s);
	  if(s == 1)
	    {
	      A[m][t]++;
	    }
	  else
	    {
	      A[m][t+1]--;
	    }
	}
      repi(i, 1, M + 1)
	{
	  rep(t, 1300)
	    {
	      if(t == 0)continue;
	      A[i][t] += A[i][t-1];
	    }
	}

      int q;
      scanf("%d", &q);
      rep(i, q)
	{
	  int ts, te, m;
	  scanf("%d%d%d", &ts,&te,&m);
	  int start = -1;
	  int ans = 0;
	  repi(t, ts, te+1)
	    {
	      if(start == -1)
		{
		  if(A[m][t] > 0)
		    {
		      start = t;
		    }
		}
	      else
		{
		  if(A[m][t] <= 0)
		    {
		      ans += t - start - 1;
		      start = -1;
		    }
		}
	    }
	  if(start != -1)ans += te - start;
	  printf("%d\n", ans);
	}
    }
  return 0;
}

C++: #include<cstdio>

int cnt[10000];
bool used[1260-540][10000];

int main(){
	for(int m;scanf("%*d%d",&m),m;){
		for(int i=0;i<m;i++)	cnt[i]=0;

		int r;	scanf("%d",&r);
		int t=0;
		for(int i=0;i<r;i++){
			int tt,st,flg;	scanf("%d%*d%d%d",&tt,&st,&flg);
			st--;
			for(;t<tt-540;t++){
				for(int j=0;j<m;j++)	used[t][j]=(cnt[j]>0);
			}
			if(flg)	cnt[st]++;
			else	cnt[st]--;
		}
		for(;t<1260-540;t++){
			for(int j=0;j<m;j++)	used[t][j]=(cnt[j]>0);
		}

		int q;	scanf("%d",&q);
		for(int i=0;i<q;i++){
			int ts,te,st;	scanf("%d%d%d",&ts,&te,&st);
			st--;
			int ans=0;
			for(int t=ts-540;t<te-540;t++)	ans+=used[t][st];
			printf("%d\n",ans);
		}
	}

	return 0;
}

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

typedef vector<int> V;
typedef pair<int,int> P;

typedef struct{
  int t,n,m,s;
} Record;

int main(){
  int n,m,r,q;
  
  while(cin >> n >> m ,n + m){
    const int MAX = 1260-540;

    cin >> r;
    Record d[r];
    vector<V> v(m,V(MAX,2)),cnt(m,V(MAX,0));

    for(int i = 0 ; i < r ; i++){
      cin >> d[i].t >> d[i].n >> d[i].m >> d[i].s;
      v[--d[i].m][d[i].t-540] = d[i].s;
    }

    for(int i = 0 ; i < m ; i++){
      cnt[i][0] = (v[i][0] == 1) ? 1 : 0;
      for(int j = 1 ; j <= MAX ; j++){
	cnt[i][j] = cnt[i][j-1] + ((v[i][j] == 1) ? 1 : 0);
	if(v[i][j] == 0) cnt[i][j]--;
      }
    }

    int ts,tt,sn;
    cin >> q;
    for(int i = 0 ; i < q ; i++){
      cin >> ts >> tt >> sn;

      ts -= 540; tt -= 540; sn--;
      int ans = 0;
      for(int i = ts ; i < tt ; i++){
	if(cnt[sn][i]) ans++;
      } 
      cout << ans << endl;
    }
  }

  return 0;
}

C++: #include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int main(){
	int n,p,a[100][1261-539],ch[100][1261-539];
	while(cin>>n>>p,n){
		int q,b,c,d,e;
		r(i,0,p)r(j,0,1261-539)ch[i][j]=a[i][j]=0;
		cin>>q;
		r(i,0,q){
			cin>>b>>c>>d>>e;
			b-=539;
			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-=539;
			c-=539;
			r(i,b,c)if(a[d-1][i])s++;
			cout<<s<<endl;
		}
	}
}

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

#define rep(i,N) for(int i=0;i<(int)(N);i++)
#define REP(i,k,N) for(int i=k;i<(int)(N);i++)

using namespace std;
int main(){
    int N,M;
    while(cin >> N >> M,N) {
        int t[1000], n[1000], m[1000], s[1000];
        int r, q, begin, end, id;
        cin >> r;
        rep(i, r) cin >> t[i] >> n[i] >> m[i] >> s[i];
        cin >> q;
        rep(i, q) {
            cin >> begin >> end >> id;
            //ここまで入力

            int ans = 0,  pc= 0, tmp;
            rep(j, r) {
                if (m[j] != id)continue;
                if (s[j] == 1) {
                    //ログイン
                    pc++;
                    if (pc == 1) tmp = t[j];
                }else{
                    //ログアウト
                    pc--;
                    if (pc == 0 && begin <= t[j] && tmp <= end) {
                        ans += (min(end, t[j]) - max(begin, tmp));
                    }
                }
            }
            cout << ans << endl;
        }
    }
    return 0;
}