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>
#include <stack>
using namespace std;
int main(){
	int N,M;
	while(cin>>N>>M){
		if(N==0 && M==0) break;
		int r,q;
		vector<vector<int> >pc(N+1,vector<int>(M+1,0));
		vector<vector<int> >time(M+1,vector<int>(1261,0));
		cin>>r;
		for(int i=0;i<r;i++){
			int t,n,m,s;
			cin>>t>>n>>m>>s;
			if(s==1){
				time[m][t]=1;
				pc[n][m]=t;
			}else if(s==0){
				for(int j=pc[n][m];j<t;j++){
					time[m][j]=1;
				}
				pc[n][m]=0;
			}
		}
		cin>>q;
		for(int i=0;i<q;i++){
			int ts,te,mn,count=0;
			cin>>ts>>te>>mn;
			for(int j=ts;j<te;j++){
				if(time[mn][j]==1) count++;
			}
			cout<<count<<endl;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
typedef long long i64;

void calcTime(vector<pair<int, int> > times){
    rep(i,times.size()-1){
        for(int j=i+1;j<times.size();j++){
            if(times[i].second>times[j].first && times[j].second>times[i].first){
                times[i].first=min(times[i].first,times[j].first);
                times[i].second=max(times[i].second,times[j].second);
                times[j].first=0;
                times[j].second=0;
            }
        }
    }
    int ans=0;
    for(pair<int, int> p : times){
        ans+=p.second-p.first;
    }
    cout << ans << endl;
}

int main(){
    map<pair<int, int>, vector<pair<int, int> > > history;
    while (true) {
        int N,M;
        cin >> N >> M;
        if(N==0 && M==0) break;
        int r;
        cin >> r;
        rep(i,r){
            int t,n,m,s;
            cin >> t >> n >> m >> s;
            pair<int, int> pcanduser = make_pair(m,n);
            if(s==1){
                if(history.find(pcanduser)==history.end()){
                    vector<pair<int, int> > vec;
                    vec.push_back(make_pair(t,t));
                    history[pcanduser]=vec;
                }else{
                    history[pcanduser].push_back(make_pair(t,t));
                }
            }else{
                history[make_pair(m,n)].back().second=t;
            }
        }
        // for(auto itr=history.begin();itr!=history.end();itr++){
        //     cout << "user : " << itr->first.first << " ";
        //     cout << "pc : " << itr->first.second << " ";
        //     for(pair<int, int> times : itr->second){
        //         cout << "start time : " << times.first << " ";
        //         cout << "end time : " << times.second << " ";
        //     }
        //     cout << endl;
        // }
        int q;
        cin >> q;
        rep(i,q){
            int ts,te,m;
            cin >> ts >> te >> m;
            vector<pair<int, int> > timeHistory;
            rep(i,N){
                // cout << "pc number : " << i+1 << endl;
                pair<int, int> userandpc = make_pair(m,i+1);
                vector<pair<int, int> > times=history[userandpc];
                for(pair<int, int> timepair : times){
                    if(timepair.second>ts&&te>timepair.first){
                        timeHistory.push_back(make_pair(max(timepair.first,ts),min(timepair.second,te)));
                        // cout << "cut to " << timeHistory.back().first << "~" << timeHistory.back().second << endl;
                    }
                }
            }
            calcTime(timeHistory);
        }
    }
    return 0;
}

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,flg2=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++){
			sum=0;
			cin>>ts>>te>>m;
			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(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 main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int m, n;
  while (cin >> m >> n, m) {
    vector<vector<int>> a(n, vector<int>(721));
    int r;
    cin >> r;
    while (r--) {
      int t, j, i, s;
      cin >> t >> j >> i >> s;
      t -= 540;
      --j, --i;
      if (s) {
        a[i][t] += 1;
      } else {
        a[i][t] -= 1;
      }
    }
    for (int i = 0; i < n; ++i) {
      for (int t = 0; t < 720; ++t) {
        a[i][t + 1] += a[i][t];
      }
    }
    int q;
    cin >> q;
    while (q--) {
      int l, r, i;
      cin >> l >> r >> i;
      l -= 540, r -= 540;
      --i;
      int res = 0;
      for (int t = l; t < r; ++t) {
        res += !!a[i][t];
      }
      cout << res << '\n';
    }
  }
}