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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;
static const int INF = (int)1e8;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) for (int i=0; i<(int)(n); ++i)
#define FOREQ(i,k,n) for (int i=(k); i<=(int)(n); ++i)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define FIND(m,w) ((m).find(w)!=(m).end())

int c[10001][1261]={};
int main(void){
  int N,M;
  while(cin>>N>>M,N&&M){
    REP(i,10001)REP(j,1261)c[i][j]=0;
    int r;
    cin>>r;
    while(r--){
      int t,n,m,s;
      cin>>t>>n>>m>>s;
      REP(time,1260){
        if(time>=t){
          if(s){
            c[m][time]++;
          }else{
            c[m][time]--;
          }
        }
      }
    }
    
    int q; cin>>q;
    while(q--){
      int ans = 0;
      int t1,t2,m;
      cin>>t1>>t2>>m;
      for(int time = t1; time < t2; time++){
        if(c[m][time]) ans ++;
      }
      cout<<ans<<endl;
    }
  }

  return 0;
}

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

int S[10000][721]={};

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

  while(1){
    cin>>N>>M;
    if(N==0 && M==0)break;
    for(int i=0;i<M;i++){
      for(int j=0;j<=720;j++)S[i][j]=0;
    }
    cin>>r;
    for(int i=0;i<r;i++){
      cin>>t>>n>>m>>s;
      if(s==1)S[m-1][t-540]=1;
      else S[m-1][t-540]=-1;
    }
    for(int i=0;i<M;i++){
      for(int j=1;j<=720;j++){
        S[i][j]+=S[i][j-1];
      }
    }
    cin>>q;
    for(int i=0;i<q;i++){
      cin>>ts>>te>>m;
      time=0;
      for(int j=ts-540;j<te-540;j++){
	if(S[m-1][j]>0)time++;
      }
      cout<<time<<endl;
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
int r,q,a,b,c;
while(cin>>r,r){
cin>>r>>r;
vector<int>t(r),m(r),s(r);
for(int i=0;i<r;++i){
cin>>t[i]>>m[i]>>m[i]>>s[i];
}cin>>q;
for(int i=0;i<q;++i){
cin>>a>>b>>c;
int ans=0,cnt=0,p=a;
for(int i=0;i<t.size();++i){
int u=min(t[i],b);
if(cnt)ans+=max(u-p,0);
if(m[i] == c){
if(s[i])cnt++;
else cnt--;
}
p=max(p,u);
}
printf("%d\n", ans);
}
}
}

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

struct data{
    int id;
    int time;
    bool log;
    data(){}
    data(int a,int t,int b):id(a),time(t),log(b){}
    bool operator<(const data& r) const{
        return id == r.id ? time < r.time : id < r.id;
    }
};

typedef vector<data>::iterator V;

int main(){
    int N,M,n,m,r,i,j,q,b,e,c,s,t;
    while( cin>>N>>M, N||M ){
        cin >> r;
        vector< data > v(r);
        for(i=0;i<r;i++){
            cin>>t>>n>>m>>s;
            m--;
            v[i] = data(m,t,s);
        }
        sort(v.begin(), v.end());
        cin >> q;
        for(i=0;i<q;i++){
            cin>>b>>e>>m;
            b-=540; e-=540;
            m--;
            V low = lower_bound(v.begin(), v.end(), data(m,0,0));
            V up = lower_bound(v.begin(), v.end(), data(m+1,0,0));
            vector<int> in(720,0);
            for(V it = low; it != up; it++){
                if(it->log) in[it->time-540]++;
                else in[it->time-540]--;
            }
            j = 0,c = 0;
            for(t=0;t<e;t++){
                j += in[t];
                if( b <= t && t < e && j ){
                    c++;
                }
            }
            cout << c << endl;
        }
    }
    return 0;
}

C++: #include<iostream>

using namespace std;

#define MMAX 10000
#define tMAX 1260
int main(){

	int N, M;
	int r;
	int t, n, m1, s;
	int q;
	int ts, te, m2;
	int num,cnt;
	int data[MMAX*2][tMAX*2];

	while (1){
		cin >> N >> M;
		if (N == 0 && M == 0) break;
		for (int i = 1; i <= M; i++){
			for (int j = 0; j <= tMAX; j++){
				data[i][j] = 0;
			}
		}
		cin >> r;
		for (int i = 0; i < r; i++){
			cin >> t >> n >> m1 >> s;
			if (s == 0) data[m1][t]--;
			else data[m1][t]++;
		}
		cin >> q;
		for (int i = 1; i <= M; i++){
			num = 0;
			for (int j = 0; j <= tMAX; j++){
				num += data[i][j];
				data[i][j] = num;
			}
		}
		for (int i = 0; i < q; i++){
			cin >> ts >> te >> m2;
			cnt = 0;
			for (int j = ts; j < te; j++){
				if (data[m2][j] >= 1) cnt++;
			}
			cout << cnt << endl;
		}
	}
	return 0;
}