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>
#include<cstring>

using namespace std;

const int MAX = 1300;
struct Info{int t,n,m,s;};
typedef pair<int,int> P;
vector<Info> V;
bool used[MAX];
int N,M;

int solve(int ts, int te, int m){

  int ans = 0;

  vector<P> login;
  vector<P> logout;

  for(int i = 0; i < V.size(); i++)
    if(V[i].m == m && V[i].s == 1)
      login.push_back(P(V[i].n,V[i].t));

  for(int i = 0; i < V.size(); i++)
    if(V[i].m == m && V[i].s == 0)
      logout.push_back(P(V[i].n,V[i].t));

  sort(login.begin(),login.end());
  sort(logout.begin(),logout.end());
  
  for(int i = 0; i < login.size(); i++){
    int s = max(ts,login[i].second);
    int e = min(te,logout[i].second);
    fill(used+s,used+e,true);
  }

  for(int i = ts; i < te; i++)
    if(used[i]) ans++;

  return ans;
}

void init(){
  V.clear();
  memset(used,false,sizeof(used));
}

int main(){

  while(cin >> N >> M && N+M){
    init();
    int r;
    cin >> r;
    while(r--){
      Info f;
      cin >> f.t >> f.n >> f.m >> f.s;
      V.push_back(f);
    }
    int q;
    cin >> q;
    while(q--){
      int ts,te,m;
      cin >> ts >> te >> m;
      cout << solve(ts,te,m) << endl;
    }
  }
  return 0;
}

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

using namespace std;

int tm1[10001][722];

int main()
{
    while(1){
        int n,m,r,q;
        cin >> n >> m;
        if(n == 0 && m == 0){
            break;
        }
        rep(i,m){
            memset(tm1[i],0,sizeof(tm1[i]));
        }
        cin >> r;
        rep(i,r){
            int a,b,c,d;
            cin >> a >> b >> c >> d;
            c--;
            if(d){
                tm1[c][a-540]++;
            }else{
                tm1[c][a-540]--;
            }
        }
        rep(i,m){
            rep(j,720){
                tm1[i][j+1] += tm1[i][j];
            }
        }
        cin >> q;
        rep(i,q){
            int a,b,c;
            cin >> a >> b >> c;
            int res = 0;
            a -= 540, b -= 540;
            for(int j=a;j<b;j++){
                if(tm1[c-1][j]){
                    res++;
                }
            }
            cout << res << "\n";
        }
    }
}

C++: #include <iostream>
#include <vector>
using namespace std;
int main()
{
  for(;;){
    int N,M;
    cin >> N >> M;
    if(N == 0 || M == 0) break;
    vector<pair<int,int> > v(M,make_pair<int,int>(0,0));
    vector<vector<pair<int,int> > > w(M);
    int r;
    cin >> r;
    for(int i=0; i<r; i++){
      int t,n,m,s;
      cin >> t >> n >> m >> s;
      if(s == 1){
        if(v[m-1].first++ == 0) v[m-1].second = t;
      }else if(s == 0){
        if(--v[m-1].first == 0)
          w[m-1].push_back(make_pair<int,int>(v[m-1].second,t));
      }
    }
    int q;
    cin >> q;
    for(int i=0; i<q; i++){
      int ts,te,m;
      cin >> ts >> te >> m;
      vector<pair<int,int> > x = w[m-1];
      int ret=0;
      for(vector<pair<int,int> >::iterator p = x.begin(); p != x.end(); p++){
        int start = ts < (*p).first ? (*p).first : ts;
        int end = (*p).second < te ? (*p).second : te;
        if(end - start > 0) ret += end - start;
      }
      cout << ret << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>

#define rep2(i, b, n) for(int i=b; i < n; ++i)
#define rep(i, n) rep2(i, 0, n)
#define sz(x) x.size()
#define pb(x) push_back(x)
#define mk(x, y) make_pair(x, y)
#define B first
#define E second

using namespace std;

typedef pair<int, int> pii;

int cnt[10000];
vector<pii> v[10000];

bool used[1000];

int main( void )
{
	int n, m;
	while(cin >> n >> m, n | m) {
		fill(cnt, cnt+1000, 0);
		memset(used, false, sizeof(used));
		rep(i, m) v[i].clear();

		int r; cin >> r;
		rep(i, r) {
			int tt, nn, mm, ss;
			cin >> tt >> nn >> mm >> ss;
			--mm;
			if(ss) {
				if(used[nn]) continue;
				if(cnt[mm] == 0) {
					v[mm].pb(mk(tt, 0));
				}
				++cnt[mm];
				used[nn] = true;
			}
			else {
				if(!used[nn]) continue;
				--cnt[mm];
				if(cnt[mm] == 0) {
					v[mm][sz(v[mm])-1].E = tt;
					//cout << v[mm][sz(v[mm])-1].B << ' ' << v[mm][sz(v[mm])-1].E << endl;
				}
				used[nn] = false;
			}
		}
		
		int q; cin >> q;
		rep(i, q) {
			int ts, te, mm, time;
			cin >> ts >> te >> mm;
			--mm;
			time = 0;
			rep(j, sz(v[mm])) {
				if((ts <= v[mm][j].B && v[mm][j].B <= te) ||
					(ts <= v[mm][j].E && v[mm][j].E <= te) ||
					(v[mm][j].B <= ts && te <= v[mm][j].E)
				) {
					time += (min(te, v[mm][j].E) - max(ts, v[mm][j].B));
				}
			}
			cout << time << endl;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int main(){
	int n,p,wq[1000000000000000000];
	while(cin>>n>>p,n){
		int q,a[10000][1261],ch[10000][1261],b,c,d,e;
		r(i,0,p)r(j,0,1261)ch[i][j]=a[i][j]=0;
		cin>>q;
		r(i,0,q){
			cin>>b>>c>>d>>e;
			if(e==1){a[d-1][b]++;ch[d-1][b]=c;}
			else{
				for(int j=b-1;j>=0;j--){
					if(ch[d-1][j]==c)break;
					a[d-1][j]++;
				}
			}
		}
		cin>>q;
		while(q--){
			int s=0;
			cin>>b>>c>>d;
			r(i,b,c)if(a[d-1][i])s++;
			cout<<s<<endl;
		}
	}
}