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>
#include<vector>
using namespace std;

typedef pair<int, int> P;

vector<P> p[10001];
int c[10001][2];
int N, M, R, Q;

int main()
{
 loop:
  for(int i = 0; i < 10001; i++)
  {
    c[i][0] = -1;
    c[i][1] = 0;
    p[i].clear();
  }
  cin >> N >> M;
  if(!N)
    return 0;
  cin >> R;
  for(int i = 0; i < R; i++)
  {
    int t, n, m, s;
    cin >> t >> n >> m >> s;
    if(s)
    {
      if(c[m][0] == -1)
        c[m][0] = t;
      c[m][1]++;
    }
    else
    {
      if(!(--c[m][1]))
      {
        p[m].push_back(make_pair(c[m][0], t));
        c[m][0] = -1;
      }
    }
  }
  cin >> Q;
  for(int i = 0; i < Q; i++)
  {
    int ts, te, m, a = 0;
    cin >> ts >> te >> m;
    for(auto e: p[m])
    {
      if(e.second < ts || te < e.first)
        continue;
      if(ts <= e.first && e.second <= te)
        a += e.second - e.first;
      else if(ts <= e.first)
        a += te - e.first;
      else if(e.second <= te)
        a += e.second - ts;
      else
        a += te - ts;
    }
    cout << a << endl;
  }
  goto loop;
}

C++: #include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<cstring>
using namespace std;

int main(){
	while(1){
		int sum,N,M,r,i,j,k,q,ts,te,start,m,rec[1001][4],in=0,flg1=0;
		cin>>N>>M;
		if(N==0&&M==0)break;
		cin>>r;
		for(i=0;i<r;i++){
			cin>>rec[i][0]>>rec[i][1]>>rec[i][2]>>rec[i][3];
		}
		rec[r][0]=1261;
		cin>>q;
		for(i=0;i<q;i++){
			in=0;
			sum=0;
			cin>>ts>>te>>m;
			flg1=0;
			if(ts<rec[0][0])flg1=1;
			if(te>rec[r-1][0])te=rec[r-1][0];
			for(j=0;rec[j][0]<ts;j++){
				if(rec[j][2]!=m)continue;
				if(flg1==1){
					ts=rec[j][0];flg1=0;
				}
				if(rec[j][3]==1){
					in++;
				}else if(rec[j][3]==0){
					in--;
				}
			}
			if(in>0){
				start=ts;
			}else{
				start=te;
			}
			for(;rec[j][0]<te;j++){
				if(rec[j][2]!=m)continue;
				if(flg1==1){
					ts=rec[j][0];flg1=0;start=ts;
				}
				if(rec[j][3]==1&&in!=0){
					in++;
				}else if(rec[j][3]==1&&in==0){
					start=rec[j][0];
					in++;
				}else if(rec[j][3]==0&&in==1){
					in--;
					sum+=rec[j][0]-start;
					start=te;
				}else{
					in--;
				}
			}
			sum+=(te-start);
			cout<<sum<<endl;
		}
	}
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int x[10000][1440],y[10000],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){
				y[c]++;
				for(j=a;j<1440;j++){
					x[c][j]=1;
				}
			}
			else{
				y[c]--;
				if(y[c]==0){
					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;
using ll=long long;
using ld=long double;

#define rep(i,N) for(int i=0;i<(int)(N);++i)
#define rrep(i,N) for(int i=(int)(N)-1;i>=0;--i)
#define debug(x) cout<<#x<<"="<<x<<endl;
constexpr ll MOD=1000000007;
constexpr ll INF=1LL<<60;
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}
template<typename T> void fail(T v){cout << v << endl;exit(0);}
//template end

void solve(){
    int N,M;
    while((cin>>N>>M,N|M)){
        int r;cin>>r;
        vector<vector<int>> imos(N,vector<int>(750,0));
        rep(i,r){
            int t,n,m,s;cin>>t>>n>>m>>s;
            t-=540;n--;
            if(s==1){
                imos[n][t]+=m;
            }else imos[n][t]-=m;
        }
        rep(i,N)
            rep(j,730)imos[i][j+1]+=imos[i][j];
        int q;cin>>q;
        while(q--){
            int ts,te,m;cin>>ts>>te>>m;
            ts-=540;te-=540;
            int res=0;
            for(int i=ts;i<te;i++)rep(j,N)if(imos[j][i]==m){res++;break;}
            cout<<res<<endl;
        }
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    solve();
    return 0;
}

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

struct Record{

  int s_cnt,e_cnt;
  int s_time[500],e_time[500];
  
};

int main(){
  
  int N,M;
  int r,q,t,n,m,m2,s;
  int t_s,t_e;
  int table[750];
  
  while(1){
    
    cin >>N>>M;
    if(N==0 && M==0)break;
    
    Record student[M+1];
    
    for(int i=0 ; i < M+1 ; i++){
      student[i].s_cnt=0;
      student[i].e_cnt=0;
    }
    
    cin >>r;
    for(int i=0 ; i < r ; i++){
      cin >>t>>n>>m>>s;
      if(s == 1){
	student[m].s_time[student[m].s_cnt]=t;
	student[m].s_cnt++;
      }else{
	student[m].e_time[student[m].e_cnt]=t;
	student[m].e_cnt++;
      }
    }
    
    cin >>q;
    for(int i=0 ; i < q ; i++){
      cin >>t_s>>t_e>>m2;
      for(int j=0 ; j < 750 ; j++)table[j]=0;
      for(int j=0 ; j < student[m2].s_cnt ; j++){
	for(int k=student[m2].s_time[j]-540 ; k < student[m2].e_time[j]-540 ; k++){
	  table[k]=1;
	}
      }
      int sum=0;
      for(int j=t_s-540 ; j < t_e-540  ; j++)sum+=table[j];
      cout <<sum<<endl;
    }
    
  }
  
  return 0;
}