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


set<int>::iterator itr,it;
set<int> *v;
int n,m;

int main(){
	int r,q,time,pc,stu,s;
	int ts,te,mm,e;
	int cnt;
	while(cin>>n>>m){
		if(n==0 && m==0) break;
		v=new set<int>[m];
		cin>>r;
		for(int i=0;i<r;i++){
			cin>>time>>pc>>stu>>s;
			time=time*10+s;
			v[stu-1].insert(time);
		}
		for(int i=0;i<m;i++){
			cnt=0;
			for(itr=v[i].begin();itr!=v[i].end();){
				int j=*itr;
				it=itr++;
				if(j%10==1){
					if(cnt!=0){
						v[i].erase(it);
					}
					cnt++;
				}
				else{
					cnt--;
					if(cnt!=0){
						v[i].erase(it);
					}
				}
			}
		}
		cin>>q;
		for(int i=0;i<q;i++){
			cin>>ts>>te>>mm;
			mm--;
			cnt=0;
			for(itr=v[mm].begin();itr!=v[mm].end();itr++){
				s=(*itr)/10;
				++itr;
				e=(*itr)/10;
				if(te<s) break;
				if(s<=ts && ts<=e && s<=te && te<=e)  cnt+=te-ts;
				else if(ts<=s && s<=te){
					if(ts<=e && e<=te){
						cnt+=e-s;
					}
					else{
						cnt+=te-s;
					}
				}
				else if(ts<=e && e<=te){
					cnt+=e-ts;
				}
			}
			cout<<cnt<<endl;
		}
	}
	return 0;
}

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

int N, M;
int r, q;
class Record{
public:
  int t;
  int s;
  int n;
  int m;
  bool operator<(const Record &a) const {
    if(t != a.t) return t < a.t;
    if(s != a.s) return s < a.s;
    if(m != a.m) return m < a.m;
    return n < a.n;
  }
};

Record data[1000];


int solve(int &ts, int &te, int &m) {
  int i = 0;
  int state[1001];
  int ans = 0;
  Record tr[1000];
  int pr = 0;
  fill(state, state+1000, -1);
  int num = 0;
  for(i = 0; i < r; ++i) {
    if(data[i].m != m) continue;
    if(data[i].s) {
      ++num;
      if(num == 1) {
	tr[pr].s = 1;
	tr[pr].t = data[i].t;
	++pr;
      }
    } else {
      --num;
      if(num == 0) {
	tr[pr].s = 0;
	tr[pr].t = data[i].t;
	++pr;
      }
    }
  }

  for(i = 0; i < pr; ++i) {
    if(tr[i].s) {
      state[tr[i].n] = tr[i].t;
    } else {
      int tmp = state[tr[i].n];
      state[tr[i].n] = -1;

      if(tr[i].t < ts) continue;
      if(tmp > te) continue;

      if(tmp < ts && tr[i].t > te) ans += te - ts;
      else if(tmp < ts)              ans += tr[i].t - ts;
      else if(tr[i].t > te)        ans += te - tmp;
      else                           ans += tr[i].t - tmp;
    }
  }
  return ans;
}

main() {
  while((cin >> N >> M) && (N || M)) {
    cin >> r;
    for(int i = 0; i < r; ++i) {
      cin >> data[i].t >> data[i].n >> data[i].m >> data[i].s;
    }
    sort(data, data+r);
    cin >> q;
    for(int i = 0; i < q; ++i) {
      int ts, te, m;
      cin >> ts >> te >> m;
      cout << solve(ts, te, m) << endl;
    }
  }
}

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

int main()
{
	int N,M;
	while(cin>>N>>M && N!=0){
		vector<int> PC(N+1,-1);
		vector<vector<pair<int,int>> > Stu(M+1);
		int r;
		cin>>r;
		for(int i=0;i<r;i++){
			int t,n,m,s;
			cin>>t>>n>>m>>s;
			if(s==0)
				Stu[m].push_back(make_pair(PC[n],t));
			else{
				PC[n]=t;
			}
		}
		int q;
		cin>>q;
		for(int i=0;i<q;i++){
			int s,e,m;
			cin>>s>>e>>m;
			sort(Stu[m].begin(),Stu[m].end());
			int ans=0;
			int time=s;
			for(int k=0;k<Stu[m].size();k++){
				if(min(Stu[m][k].first,e)-max(time,Stu[m][k].second)<0) continue;
				ans+=min(Stu[m][k].first,e)-max(time,Stu[m][k].second);
				time=min(Stu[m][k].first,e);
			}
			cout<<ans<<endl;
		}
	}
}

C++: #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <numeric>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <functional>
#include <complex>

using namespace std;

const int INF = (1<<30) - 1;


int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    int N, M;
    while(cin>>N>>M, N|M){
        vector<pair<int, int>> S[10001];
        
        int r;
        cin >> r;
        while(r--){
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            S[m].push_back(make_pair(t, s));
        }
        for(int i=1; i<=M; i++)
            sort(S[i].begin(), S[i].end());
        
        int q;
        cin >> q;
        while(q--){
            int s, e, m;
            cin >> s >> e >> m;
            int sum = 0;
            int count = 0;
            int time = s;
            
            for(auto p: S[m]){
                if(time <= p.first && count > 0){
                    sum += min(p.first, e) - time;
                }
                time = max(time, min(p.first, e));
                if(time == e)
                    break;
                count += p.second ? 1 : -1;
            }
            
            cout << sum << endl;
        }
    }
    
    return 0;
}