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>
#define REP(i,n) for (int i=0;i<(n);i++)
#define ALL(a) (a).begin(),(a).end()
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using namespace std;
const int MAX_STUDENT = 10001;
const int MAX_TIME = 1261;
int main(){
  int N,M;
  while(true){
    cin >> N >> M;
    if(!N)break;
    int r;
    cin >> r;
    vector<vector<int>> timeline(MAX_STUDENT,vector<int>(MAX_TIMW,0));
    REP(i,r){
      int t,n,m,s;
      cin >> t >> n >> m >> s;
      if(s==0){
	timeline[m][t] -= 1;
      }else{
	timeline[m][t] += 1;
      }
    }
    REP(i,MAX_STUDENT)
      for(int j = 540;j<MAX_TIME;j++){
      timeline[i][j+1] += timeline[i][j];
    }
    int q;
    cin >> q;

    REP(i,q){
      int ct = 0;
      int t1,t2,m;
      cin >> t1 >> t2 >> m;
      for(int j = t1;j<t2;j++){
	if(timeline[m][j]>0)ct++;
      }
      cout << ct << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int main()
{
  for(;;){
    int N,M;
    cin >> N >> M;
    if(N == 0 || M == 0) break;
    vector<pair<int,int> > v(M,make_pair<int,int>(0,0));
    vector<vector<pair<int,int> > > w(M);
    int r;
    cin >> r;
    for(int i=0; i<r; i++){
      int t,n,m,s;
      cin >> t >> n >> m >> s;
      if(s == 1){
        if(v[m-1].first++ == 0) v[m-1].second = t;
      }else if(s == 0){
        if(--v[m-1].first == 0)
          w[m-1].push_back(make_pair<int,int>(v[m-1].second,t));
      }
    }
    int q;
    cin >> q;
    for(int i=0; i<q; i++){
      int ts,te,m;
      cin >> ts >> te >> m;
      vector<pair<int,int> > x = w[m-1];
      int ret=0;
      for(vector<pair<int,int> >::iterator p = x.begin(); p != x.end(); p++){
        if(ts <= (*p).first && (*p).second <= te)
          ret += (*p).second - (*p).first;
        else if((*p).first <= ts && te <= (*p).second)
          ret += te - ts;
        else if((*p).first <= ts) ret += (*p).second - ts;
        else if(te <= (*p).second) ret += te - (*p).first;
      }
      cout << ret << endl;
    }
  }
  return 0;
}

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

int solve(vector<multimap<int,bool> > &record,int ts,int te,int a){
  int d=0,sum=0,start=0;
  for(multimap<int,bool>::iterator it=record[a-1].begin();it!=record[a-1].end();++it){
    if(!d&&it->second)start=it->first;
    if(it->second)++d;
    else --d;
    if(!d){
      int st,en;
      st=(ts>start)?ts:start;
      en=(te<it->first)?te:it->first;
      sum+=en-st;
    }
  }
  return sum;
}

int main(void){
  int n,m;
  while(cin>>n>>m,n||m){
    int r,q;
    cin>>r;
    vector<multimap<int,bool> > record(m);
    for(int i=0,t,a,b,s;i<r;++i){
      cin>>t>>a>>b>>s;
      record[b-1].insert(make_pair(t,((s)?true:false)));
    }
    cin>>q;
    for(int i=0,ts,te,a;i<q;++i){
      cin>>ts>>te>>a;
      cout<<solve(record,ts,te,a)<<endl;
    }
  }
}

C++: #include <iostream>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stdlib.h>

using namespace std;

int N,M,r,q;
typedef struct{
	int t,n,m,s;
} LOG;

bool time_table[10000+1][1260 - 540 + 1];

void solve()
{
	memset(time_table,0,sizeof(time_table));

	cin>>r;
	vector<LOG> logs(r);
	for(int i = 0;i < r; i++){
		cin >> logs[i].t >> logs[i].n >> logs[i].m >> logs[i].s;
	}

	int count[10001]={0},t[10001] = {0};
	for(int j = 0; j < r; j++){
		int m = logs[j].m;
		if(count[m] == 1 && logs[j].s == 0)
			for(int k = t[m]; k < logs[j].t - 540; k++)
				time_table[m][k] = true;

		if(count[m] == 0 && logs[j].s == 1)
			t[m] = logs[j].t - 540;
		count[m] += (logs[j].s == 1) ? 1 : -1; //ツカツウツδ督トツづーツ堕敖個クツつウツつケツづゥ
	}

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


}

int main()
{
	while(cin >>N >>M,(N||M)){
		solve();
	}
	return 0;
}

C++: #include<stdio.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <string>

using namespace std;

int n, m, r, q, t[10000][1300];

int main() {

	
	while (1) {

		for (int i = 0; i < 10000; i++) {
			for (int j = 0; j < 1300; j++) {
				t[i][j] = 0;
			}
		}

		cin >> n >> m;
		if (n==0 && m==0) break;

		cin >> r;
		for (int i = 0; i < r; i++) {
				int tt, tn, tm, ts;
				cin >> tt >> tn >> tm >> ts;
				if (ts == 1) t[tm-1][tt]++;
				else t[tm - 1][tt+1]--;
		}

		for (int i = 0; i < m; i++) {
			for (int j = 1; j <1300; j++) {
				t[i][j] += t[i][j-1];
			}
		}

		vector<int> v;
		cin >> q;
		for (int i = 0; i < q; i++) {
			int ts, te, tm;
			cin >> ts >> te >> tm;
			int ans=0;
			bool flag = false;
			for (int j = ts; j < te; j++) {
				if (t[tm - 1][j]>0 && t[tm - 1][j+1]>0) ans++, flag = true;
				//if (flag && (t[tm - 1][j]==0 || (j==te&&t[tm-1][te+1]<1))) ans--, flag = false;
			}
			v.push_back(ans);
		}

		for (int h = 0; h < v.size(); h++) {
			cout << v[h] << endl;
		}
	}

	return 0;
}