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 <algorithm>
using namespace std;

const int MINT = 540, MAXT = 1260;
const int T = MAXT - MINT;
const int N = 1000, M = 10000;

short records[M + 1][T + 1];

int main(){
	int n, m, r, q;
	while(cin >> n >> m, n){
		cin >> r;
		int s, e, t, a, b, ans = 0;
		for(int i = 0; i < m; ++i){
			fill(records[i], records[i] + T + 1, 0);
		}
		for(int i = 0; i < r; ++i){
			cin >> t >> a >> b >> s;
			if(s){
				++records[b][t - MINT];
			}
			else{
				--records[b][t - MINT];
			}
		}
		for(int i = 1; i <= m; ++i){
			for(int j = 0;j <= T;++j){
				records[i][j] += records[i][j - 1];
			}
		}
		cin >> q;
		for(int i = 0; i < q; ++i){
			cin >> a >> b >> m;
			ans = 0;
			a -= MINT;
			b -= MINT;
			for(int j = a;j < b; ++j){
				if(records[m][j]) ++ans;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

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

int student[10001][721];
int now[10001];

int main(){
  int N,M,r,   i,j,   t,n,m,s;
  while(1){
    cin>>N>>M;
    if(N==0)break;
    for(i=1;i<=M;i++){
      for(j=0;j<=720;j++){
	student[i][j]=0;
      }
    }

    cin>>r;
    for(i=0;i<r;i++){
      cin>>t>>n>>m>>s;
      if(s==1){
	student[m][t-540]++;
      }else{
	student[m][t-540]--;
      }
    }
    for(i=1;i<=M;++i){
      for(j=1;j<=720;++j){
	student[i][j]+=student[i][j-1];
      }
    }

      int q,ts,te, ans;
      cin>>q;
      for(i=0;i<q;i++){
	ans=0;
	cin>>ts>>te>>m;
	for(j=ts-540; j<te-540; j++){
	  if(student[m][j])ans++;
	}
	cout<<ans<<endl;
      }
    }
    return 0;
  }

C++: #include<iostream>

using namespace std;

#define MMAX 100
#define tMAX 126
int main(){

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

	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]=-1;
			else data[m1][t]=1;
		}
		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;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <tuple>

int main()
{
	while(true){
		int N, M;

		std::cin >> N >> M;
		if ( N == 0 && M == 0 ) break;

		int r;
		std::cin >> r;

		std::vector<std::tuple<int, int, int>> data(r);
		for ( int i = 0; i < r; ++i ) {
			auto &v = data[i];

			int n;
			std::cin >> std::get<0>(data[i]);
			std::cin >> n;
			std::cin >> std::get<1>(data[i]);
			std::cin >> std::get<2>(data[i]);
		}

		std::sort(data.begin(), data.end());
		int q;
		std::cin >> q;
		for ( int i = 0; i < q; ++i ) {
			int s, e, m;
			std::cin >> s >> e >> m;

			int be = s;
			int sum = 0;
			int pc = 0;
			for ( int j = 0; j < r; ++j ) {
				if ( std::get<1>(data[j]) == m ) {
					if ( std::get<2>(data[j]) == 1 ) {
						pc++;
					}else {
						pc--;
					}

					if ( be < std::get<0>(data[j]) && std::get<2>(data[j]) == 1 ) {
						if ( pc == 1 )
							be = std::get<0>(data[j]);
					}
					if ( std::get<2>(data[j]) == 0 ) {
						if ( pc == 0 ) {
							if ( e > std::get<0>(data[j]) ) {
								sum += std::get<0>(data[j]) - be;
							} else {
								sum += e - be;
								break;
							}
						}
					}
				}
			}
			std::cout << sum << std::endl;
		}
	}
	
	return 0;
}

C++: #define _USE_MATH_DEFINES
#define INF 100000000

#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair <int,int> P;


static const double EPS = 1e-8;

int tx[] = {-1,0,1,0};
int ty[] = {0,1,0,-1};

class Data{
public:
	int time;
	bool login;
	Data() : time(0),login(false){}
	Data(int t,int l){
		this->time = t;
		this->login = l;
	}
};

int main()
{
	int N,M;//N:pc M:student
	while(~scanf("%d %d",&N,&M)){
		if(N==M && M==0) break;
		map<P,vector<Data> > log;
		int r;
		scanf("%d",&r);
		for(int i=0;i<r;i++){
			//s:1 login s:0 logout
			//Pc n
			//student m
			//time t
			int t,n,m,s;
			scanf("%d %d %d %d",&t,&n,&m,&s);
			log[P(n,m)].push_back(Data(t,s));
		}
		int q;
		scanf("%d",&q);

		for(int i=0;i<q;i++){
			int ts,te,m;
			//student m
			scanf("%d %d %d",&ts,&te,&m);

			bool stay[1270];
			fill(stay,stay+1270,false);
			//memset(stay,0,sizeof(stay));

			for(int j=1;j<=N;j++){
				if(log.find(P(j,m)) == log.end()) continue;
				for(int k=0;k<log[P(j,m)].size();k+=2){
					int s = max(log[P(j,m)][k].time,ts);
					int e = min(log[P(j,m)][k+1].time,te);
					if(e-s<=0) continue;
					for(int l=s;l<=e;l++) stay[l] = true;
				}
			}

			int res = 0;
			for(int j=0;j<1270;j++){
				if(stay[j]){
					int k=j;
					for(;k<1270;k++){
						if(!stay[k]) break;
					}				
					res += k-j-1;
					j=k-1;
				}
			}
			printf("%d\n",res);
		}


	}
}