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 <algorithm>

using namespace std;

#define rep(i, n) for(int i=0; i<(n); ++i)
#define all(c) (c).begin(), (c).end()

struct Log{
    int id, s, e;
    Log(int id, int s, int e):id(id), s(s), e(e){}
};

int main(){
    int N, M;
    while(cin >> N >> M, N|M){
        vector<vector<Log> > usr(M);
        int r;
        cin >> r;
        while(r--){
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            m--;
            if(s)usr[m].emplace_back(n, t, 0);
            else (*find_if(all(usr[m]), [&](Log& l){return l.id == n && l.e == 0;})).e = t;
        }
        int q;
        cin >> q;
        while(q--){
            int s, e, m, res = 0;
            cin >> s >> e >> m;
            m--;
            for(auto& x: usr[m]){
                if(e < x.s)break;
                if(x.s < s && x.e < s)continue;
                res += min(e, x.e) - max(s, x.s);
                s = max(max(s, x.s), min(e, x.e));
            }
            cout << res << '\n';
        }
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
int n,m;
int use[10001][1500];
int flag[10001];
main()
{
	while(cin>>n>>m,n)
	{
		int r;cin>>r;
		for(int i=0;i<m;i++)
		{
			flag[i]=0;
			for(int j=0;j<1500;j++)use[i][j]=0;
		}
		for(int i=0;i<r;i++)
		{
			int T,N,M,S;cin>>T>>N>>M>>S;
			use[M][T]+=S?1:-1;
		}
		int q;cin>>q;
		for(int i=0;i<q;i++)
		{
			int t1,t2,M;cin>>t1>>t2>>M;
			if(!flag[M]++)for(int j=1;j<1500;j++)use[M][j]+=use[M][j-1];
			int cnt=0;
			for(int j=t1;j<t2;j++)cnt+=!!use[M][j];
			cout<<cnt<<endl;
		}
	}
}

C++: #include<iostream>

using namespace std;

int time[10001][1261];

int main(){

	while(1){
		int N,M,r,q;
		int t,n,m,s;
		int ts,te;

		cin>>N>>M;

		if(N==0&&M==0) break;

		cin>>r;
		for(int i=0;i<=M;i++){
			for(int j=0;j<=1260;j++){
				time[i][j]=0;
			}
		}
		for(int i=0;i<r;i++){
			cin>>t>>n>>m>>s;
			if(s==0) time[m][t]--;
			if(s==1) time[m][t]++;
		}
		for(int i=0;i<=M;i++){
			for(int j=1;j<=1260;j++){
				time[i][j]+=time[i][j-1];
			}
		}
		cin>>q;
		for(int i=0;i<q;i++){
			cin>>ts>>te>>m;
			int ans=0;
			for(int j=ts;j<te;j++){
				if(time[m][j]!=0) ans++;
			}
			cout<<ans<<endl;
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>

#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

int main() {
  while(1){
    int N,M;
    cin>>N>>M;
    if(!N)break;
    int r;
    cin>>r;
    vector<vector<short>> v(M,vector<short>(1300));
    REP(i,r){
      int t,n,m,s;
      cin>>t>>n>>m>>s;
      --m;
      if(s) ++v[m][t];
      else --v[m][t];
    }
    vector<vector<short>> sum(M,vector<short>(1301));
    REP(i,M) partial_sum(begin(v[i]),end(v[i]),begin(sum[i]));
    int q;
    cin>>q;
    REP(i,q){
      int ts,te,m;
      cin>>ts>>te>>m;
      --m;
      int tu=0;
      for(int j=ts;j<te;++j){
        if(sum[m][j])
          ++tu;
      }
      cout<<tu<<endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <tuple>
#include <algorithm>
using namespace std;
typedef tuple<int, int, int> T;

int main() {
	int N, M;
	while(cin >> N >> M && (N || M)){
		int r;
		cin >> r;
		vector<T> vec[M + 1];
		int t, n, m, s;
		for(int i = 0; i < r; ++i){
			cin >> t >> n >> m >> s;
			vec[m].push_back(make_tuple(t, n, s));
		}
		int q;
		cin >> q;
		int ts, te, mt;
		for(int i = 0; i < q; ++i){
			cin >> ts >> te >> mt;
			
			int ls[1001] = {0};
			bool l[1270];
			fill(l, l + 1270, false);
			for(auto it = vec[mt].begin(); it != vec[mt].end(); ++it){
				int first = get<0>((*it));
				int second = get<1>((*it));
				int third = get<2>((*it));
				
				if(third){
					ls[second] = max(first, ts);
				}
				else if(ls[second] != 0){
					int e = min(first, te);
					if(e - ls[second] <= 0){
						ls[second] = 0;
							continue;
					}
					for(int j = ls[second]; j <= e; ++j)
						l[j] = true;
					ls[second] = 0;
				}
				
			}
			int ans = 0;
			for(int j = 0; j < 1270; ++j){
				if(l[j]){
					int k = j;
					for(; k < 1270; ++k){
						if(!l[k])
							break;
					}
					ans += (k - j - 1);	
					j = (k - 1);
				}
			}
				
			cout << ans << endl;
		}
		
		
	}
	return 0;
}