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<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) ans++, flag = true;
				if (flag && t[tm - 1][j]==0) ans--,flag = false;
			}
			v.push_back(ans);
		}

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

	return 0;
}

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

int main(){
	while(true){
		vector<int> curlog;
		vector< list< pair<int,int> > > log;
		int N,M,r,q;
		cin>>N>>M;
		if(N==0&&M==0)break;
		cin>>r;
		curlog.resize(N);
		log.resize(M);
		for(int i=0;i<r;i++){
			int t,n,m,s;
			cin>>t>>n>>m>>s;
			if(s){
				curlog[n-1]=t;
			}else{
				log[m-1].push_back(pair<int,int>(curlog[n-1],t));
			}
		}
		cin>>q;
		for(int i=0;i<q;i++){
			int s,e,m,a=0;
			list< pair<int,int> > logcop;
			cin>>s>>e>>m;
			logcop=log[m-1];
			logcop.sort();
			for(list< pair<int,int> >::iterator ita=logcop.begin();ita!=logcop.end();++ita){
				int start=(*ita).first,last=(*ita).second;
				list< pair<int,int> >::iterator itb=ita;
				++itb;
				for(;itb!=logcop.end();){
					if((*itb).first>start&&(*itb).first<last){
						if((*itb).second>last)last=(*itb).second;
						itb=logcop.erase(itb);
					}else{
						++itb;
					}
				}
				if(start<s)start=s;
				if(last>e)last=e;
				a+=last-start;
			}
			cout<<a<<endl;
		}
	}
	return 0;
}

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

class history {
public:
    int pc, start, end;
    history(int p, int s, int e) {
        pc = p; start = s; end = e;
    }
};

int main(void){
    vector<history> hist[10010];
    int N, M;
    int r, t, n, m, s, q, ts, te;
    int imos[1265];
    while(cin >> N >> M && N) {
        rep(i, 10010) hist[i].clear();
        cin >> r;
        rep(i, r) {
            cin >> t >> n >> m >> s;
            if(s==1) {
                hist[m].push_back(history(n, t, -1));
            } else {
                rep(j, hist[m].size()) {
                    if(hist[m][j].pc == n && hist[m][j].end == -1) {
                        hist[m][j].end = t;
                        break;
                    }
                }
            }
        }
        cin >> q;
        rep(i, q) {
            cin >> ts >> te >> m;
            rep(j, 1265) imos[j] = 0;
            rep(j, hist[m].size()) {
                if(hist[m][j].start < te && hist[m][j].end > ts) {
                    imos[max(hist[m][j].start, ts)] += 1;
                    imos[min(hist[m][j].end, te)] -= 1;
                }
            }
            int ans = 0;
            int cnt = 0;
            int start;
            rep(j, 1265) {
                if(cnt == 0 && imos[j] > 0) {
                    start = j;
                }
                if(cnt > 0 && cnt+imos[j] == 0) {
                    ans += (j - start);
                }
                cnt += imos[j];
            }
            cout << ans << endl;
        }
    }
}

C++: #include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <queue>

using namespace std;

struct Use {
  int t;
  int n;
  int m;
  int s;
  Use() {;}
  Use(int t, int n, int m, int s) : t(t), n(n), m(m), s(s) {;}
};

int n, m, r, q;
int pc[1001][1400];

int main() {
  while (scanf("%d %d", &n, &m), n | m) {
    memset(pc, -1, sizeof(pc));
    scanf("%d", &r);
    queue<Use> que;
    for (int i = 0; i < r; i++) {
      int a, b, c, d;
      scanf("%d %d %d %d", &a, &b, &c, &d);
      b--;
      que.push(Use(a, b, c, d));
    }
    for (int i = 540; i <= 1260; i++) {
      if (que.empty()) { break; }
      for (int j = 0; j < n; j++) {
        pc[j][i] = pc[j][i - 1];
      }
      while (!que.empty()) {
        Use use = que.front();
        if (use.t != i) { break; }
        que.pop();
        if (use.s == 1) {
          pc[use.n][i] = use.m;
        } else {
          pc[use.n][i] = -1;
        }
      }
    }
    scanf("%d", &q);
    for (int iter = 0; iter < q; iter++) {
      int ts, te, a;
      scanf("%d %d %d", &ts, &te, &a);
      int ans = 0;
      for (int t = ts; t < te; t++) {
        for (int i = 0; i < n; i++) {
          if (pc[i][t] == a) { ans++; break; }
        }
      }
      printf("%d\n", ans);
    }
  }
}