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 <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS; typedef istringstream ISS;
typedef long long LL;
typedef pair<int, int> PII;  typedef pair<LL, LL> PLL;
typedef vector<int>    VI;   typedef vector<VI>   VVI;   typedef vector<VVI>   VVVI;
typedef vector<LL>     VLL;  typedef vector<VLL>  VVLL;  typedef vector<VVLL>  VVVLL;
typedef vector<double> VD;   typedef vector<VD>   VVD;   typedef vector<VVD>   VVVD;
typedef vector<string> VS;   typedef vector<VS>   VVS;   typedef vector<VVS>   VVVS;
typedef vector<bool>   VB;   typedef vector<VB>   VVB;   typedef vector<VVB>   VVVB;
typedef vector<PII>    VPII; typedef vector<VPII> VVPII; typedef vector<VVPII> VVVPII;
typedef vector<PLL>    VPLL; typedef vector<VPLL> VVPLL; typedef vector<VVPLL> VVVPLL;

typedef unsigned int  UI;
typedef vector<UI>    VUI;
typedef vector<VUI>   VVUI;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back 
#define ALL(x)  (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define RANGEBOX(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define RANGE(x, l, r) ((l) <= (x) && (x) <= (r))
#define rep(i, N)  for (int i = 0; i < (int)(N); i++)
#define rrep(i, N) for (int i = N - 1; i >= 0; i--)
#define REP(i, init, N)  for (int i = (init); i < (int)(N); i++)
#define RREP(i, N, last) for (int i = (init - 1); i >= last; i--)
#define MAXUD(orig, target) orig = max(orig, target)
#define MINUD(orig, target) orig = min(orig, target)
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

template < typename T > inline T fromString(const string &s) { T res; ISS iss(s); iss >> res; return res; };
template < typename T > inline string toString(const T &a)   { OSS oss; oss << a; return oss.str(); };

template<typename T=int> inline void dump(vector<T> vs, bool ent=false) { rep(i, vs.size()) cout << vs[i] << (i+1==vs.size() ? '\n' : ' '); if (ent) cout << endl; }
template<typename T = int> inline void dump(vector<vector<T>> vs, bool ent = false) { rep(i, vs.size()) dump<T>(vs[i]); if (ent) cout << endl; }

const int    INF  = 0x3f3f3f3f;
const LL     INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int    DX[] = {1,  0, -1, 0};
const int    DY[] = {0, -1,  0, 1};
const double EPS  = 1e-12;
// const double PI   = acos(-1.0);
// lambda: [](T1 x)->T2{return y;}
// simple lambda: [](T x){return x;}

int N, M, r, q;

const int MAX_T = 1270;

void solve() {
    cin >> r;

    VVI ts(M, VI(MAX_T));

    rep(i, r) {
        int t, n, m, s;
        cin >> t >> n >> m >> s;
        --m;
        ts[m][t] += s ? 1 : -1;
    }

    rep(i, M) {
        partial_sum(ALL(ts[i]), ts[i].begin());
    }

    cin >> q;
    rep(i, q) {
        int s, e, m;
        cin >> s >> e >> m;
        --m;
        int cnt = count_if(ts[m].begin() + s, ts[m].begin() + e, [](int x){return x>0;});
        cout << cnt << endl;
    }
}

int main(void) {
    while (cin >> N >> M, N) {
        solve();
    }

    return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <map>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
int main(){
  int N, M;
  while (cin >> N >> M, N){
    int r, t, n, m, s;
    cin >> r;
    vector< vector<pii> > V(M);
    map<pii,int> L;
    for (int i = 0; i < r; ++i){
      cin >> t >> n >> m >> s;
      --m;
      if(s) L[pii(n,m)] = t;
      else V[m].push_back(pii(L[pii(n,m)],t));
    }
    int q, ts, te;
    cin >> q;
    for(int i = 0; i < q; ++i){
      cin >> ts >> te >> m;
      --m;
      sort(V[m].begin(),V[m].end());
      int ans = te-ts;
      for(int j = 0; j < V[m].size(); ++j){
	if(ts >= V[m][j].first) ts = max(ts,V[m][j].second);
	else if(V[m][j].first > te) break;
	else{
	  ans += ts-V[m][j].first;
	  ts = V[m][j].second;
	}
	if(ts >= te) break;
      }
      if(ts < te) ans += ts-te;
      cout << ans << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <stack>
#include <vector>

#define pb push_back
#define mp make_pair

using namespace std;

int main(void)
{
	int n,m;
	int r,q;
	int i1,i2,i3;
	int t,PCn,STm,s;
	int ts,te;
	int time_;
	int c;

	while(1){
		cin >> n >> m;
		if(n+m==0) break;

		vector<pair<int,int> > v[m+1];

		cin >> r;
		for(i1=0;i1<r;i1++){
			cin >> t >> PCn >> STm >> s;
			v[STm].pb(mp(t,s));
		}
		cin >> q;
		for(i1=0;i1<q;i1++){
			cin >> ts >> te >> STm;
			stack<int> in;
			time_=0;
			c=0;

			for(i2=540;i2<=te;i2++){
				if(c<v[STm].size()){
					if(v[STm][c].first<i2 && v[STm][c].second==1){
						c++;
						in.push(1);
					}
					if(v[STm][c].first<i2 && v[STm][c].second==0){
						c++;
						in.pop();
					}
				}
				if(!in.empty() && i2>ts) time_++;	
			}
			cout << time_ << endl;
		}

	}
	return 0;
}