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 <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>

#define MAX_T 1260

using namespace std;

typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;

int main(void) {
    while (1) {
        int n; // pc
        int m; // person
        cin >> n >> m;
        if (n == 0 && m == 0) {
            break;
        }

        VI pc(m + 1, 0);
        VI start(m + 1, 0);

        VVI time(m + 1, VI(MAX_T + 1, 0));

        int r;
        cin >> r;
        for (int i = 0; i < r; i++) {
            int t;
            int n2; // pc
            int m2; // person
            int s;
            cin >> t >> n2 >> m2 >> s;
            if (s == 1) {
                if (pc[m2] == 0) {
                    start[m2] = t;
                }
                pc[m2]++;
            } else {
                pc[m2]--;
                if (pc[m2] == 0) {
                    int end = t;
                    for (int loop = start[m2]; loop < end; loop++) {
                        time[m2][loop] = 1;
                    }
                }
            }
        }
        /*
           for (int i = 1; i <= m; i++) {
           int count = 0;
           for (int j = 0; j < MAX_T + 1; j++) {
           count += time[i][j];
           }
           cout << i << " " << count << endl;
           }
         */
        int q;
        cin >> q;
        for (int i = 0; i < q; i++) {
            int s;
            int e;
            int m2;
            cin >> s >> e >> m2;
            int count = 0;
            for (int j = s; j < e; j++) {
                count += time[m2][j];
            }
            cout << count << endl;
        }

    }
    return 0;
}

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

int solve(multimap<int,bool> &record,int ts,int te){
  int d=0,sum=0,start=0;
  for(multimap<int,bool>::iterator it=record.begin();it!=record.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>0)?en-st:0;
    }
  }
  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[a-1],ts,te)<<endl;
    }
  }
  return 0;
}

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

int N,M,r,q;
int t,m,n,s,ts,te;
int main(void){
    while(true){
        cin>>N>>M;
        if(N==0&&M==0)break;
        cin>>r;
        vector<vector<int>> time(M+1,vector<int>(730,0));
        vector<vector<int>> eq(M+1,vector<int>(730,0));
        for(int i=0;i<r;i++){
            cin>>t>>n>>m>>s;
            t-=540;
            if(s==1) time[m][t+1]++;
            else time[m][t+1]--;
        }
        for(int i=1;i<=M;i++){
            for(int k=1;k<730;k++){
                time[i][k]+=time[i][k-1];
                eq[i][k]=eq[i][k-1]+(time[i][k]>0 ? 1 : 0);
            }
        }
        cin>>q;
        for(int i=0;i<q;i++){
            cin>>ts>>te>>m;
            ts-=540;te-=540;
            cout<<eq[m][te]-eq[m][ts]<<endl;
        }

    }
    return 0;
}

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

int main()
{
  int N,M,r,q,t[1000],n[1000],m[1000],s[1000],ts,te,stu,sum,log,res;

  for(;;){
    cin>>N>>M;
    if(!(N|M)) break;
    cin>>r;
    for(int i=0;i<r;i++)
      cin>>t[i]>>n[i]>>m[i]>>s[i];
    cin>>q;
    for(int i=0;i<q;i++){
      cin>>ts>>te>>stu;
      sum=0; log=0;
      for(int j=0;j<r;j++){
	if(m[j]!=stu) continue;
	if(s[j]){
	  log++;
	  if(log==1) res=t[j];
	} else {
	  log--;
	  if(!log&&ts<=t[j]&&res<=te) sum+=(min(te,t[j])-max(ts,res));
	}
      }
      cout<<sum<<endl;
    }
  }
}

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

using ll = long long;

int main() {
    int N, M;
    while(cin >> N >> M, N) {
        vector<vector<int>> imos(M, vector<int>(1262));
        int r;
        cin >> r;
        for(int i=0; i<r; ++i) {
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            m--;
            imos[m][t] = (s == 0 ? -1 : 1);
        }
        for(int i=0; i<M; ++i) {
            for(int j=1; j<imos[i].size(); ++j) {
                imos[i][j] += imos[i][j-1];
            }
        }
        int q;
        cin >> q;
        for(int i=0; i<q; ++i) {
            int ts, te, m;
            cin >> ts >> te >> m;
            m--;
            int res = 0;
            for(int t=ts; t<te; ++t) {
                res += (imos[m][t] > 0);
            }
            cout << res << endl;
        }
    }
}