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

#define MOD7 1000000007
#define MOD9 1000000009

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

using namespace std;

int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, -1, 0, 1 };

int nextInt() {int a; cin >> a; return a;}
char nextChar() {char a; cin >> a; return a;}
double nextDouble() {double a; cin >> a; return a;}
string nextString() {string a; cin >> a; return a;}

void inputVector(vector<int> &v, int &n) {rep(i,n){v.push_back(nextInt());}}
void inputVector(vector<double> &v, int &n) {rep(i,n){v.push_back(nextDouble());}}
void inputVector(vector<string> &v, int &n) {rep(i,n){v.push_back(nextString());}}

int memo[1270][10010];

signed main() {
    int N, M;
    while (cin >> N >> M) {
	if (!N) break;

	memset(memo, 0, sizeof(memo));

	int r;
	cin >> r;
	rep(i, r) {
	    int t, n, m, s;
	    cin >> t >> n >> m >> s;
	    m--;

	    if (s == 0) memo[t][m]--;
	    else memo[t][m]++;
	}
	REP(i, 500, 1265) {
	    rep(j, M) {
		memo[i][j] += memo[i - 1][j];
	    }
	}
	REP(i, 500, 1265) {
	    rep(j, M) {
		memo[i][j] = memo[i][j] > 0;
	    }
	}
	REP(i, 500, 1265) {
	    rep(j, M) {
		memo[i][j] += memo[i - 1][j];
	    }
	}

	int q;
	cin >> q;
	rep(i, q) {
	    int ts, te, m;
	    cin >> ts >> te >> m;
	    m--;

	    cout << memo[te - 1][m] - memo[ts - 1][m] << endl;
	}
    }

    return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

int main()
{
	int number = 0;
	for(;;){
		int N, M, r;
		cin >> N >> M;
		if(N == 0 && M == 0)
			break;
		cin >> r;

		vector<vector<int> > student(M+1);
		vector<int> num(M+1);
		for(int i=0; i<r; ++i){
			int t, n, m, s;
			cin >> t >> n >> m >> s;
			if(s == 1){
				if(num[m] == 0)
					student[m].push_back(t);
				++ num[m];
			}else{
				-- num[m];
				if(num[m] == 0)
					student[m].push_back(t);
			}
		}

		int q;
		cin >> q;
		for(int i=0; i<q; ++i){
			int ts, te, m;
			cin >> ts >> te >> m;
			int answer = 0;
			for(unsigned j=0; j<student[m].size(); j+=2){
				if(student[m][j] <= te && student[m][j+1] >= ts)
					answer += min(student[m][j+1],te) - max(student[m][j],ts);
			}
			cout << answer << endl;
		}

	}

	return 0;
}

C++: #include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

vector<pair<int,int> > ary[10001];

int main(){ _;
  int N,M,t,n,m,s,r,q,ts,te;
  while(cin>>N>>M,(N|M)!=0){
    cin>>r;
    REP(i,r){
      cin>>t>>n>>m>>s;
      ary[m].push_back(make_pair(t,s==1?1:-1));
    }
    cin>>q;
    REP(i,q){
      cin>>ts>>te>>m;
      map<int,int> a;
      for(auto p:ary[m]){
	if(p.first<ts)
	  a[ts]+=p.second;
	else if(p.first>te)
	  a[te]+=p.second;
	else
	  a[p.first]=p.second;
      }
      int sum=0,start=0,login=0;
      for(auto aa:a){
	if(login==0&&aa.second>0){
	  start=aa.first;
	}
	login+=aa.second;
	if(login==0&&aa.second<0){
	  sum+=aa.first-start;
	}
      }
      cout<<sum<<endl;
    }
    REP(i,M+1){
      ary[i].clear();
    }
  }
}

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

int main(){
  int N,M;
  while(cin>>N>>M){
    if(N==0)break;
    int r,q;
    int student[2*M][7210];
    memset(student,0,sizeof(student));  
    cin>>r;
    for(int i=0;i<r;i++){
      int t,n,m,s;
      cin>>t>>n>>m>>s;
      t-=540;m--;
      if(s==1){
	student[m][t]=n;
      }else{
	while(t>0 && student[m][--t]!=n)
	  if(student[m][t]==0) student[m][t]=n;
      }
    }
    cin>>q;
    for(int i=0;i<q;i++){
      int ts,te,m,ans=0;
      cin>>ts>>te>>m;
      ts-=540;te-=540;m--;
      for(int i=ts;i<te;i++){
	if(student[m][i]>0)ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
	int n,m;
	while(cin>>n>>m,n+m)
	{
		int b[n+2];
		bool a[m+2][740]={};
		int r;
		cin>>r;
		for (int i = 0; i < r; ++i)
		{
			int w,x,y;
			bool z;
			cin>>w>>x>>y>>z;
			if(z) b[x]=w-540;
			else
			{
				for (int j = b[x]; j < w-540; ++j)
				{
					a[y][j]=1;
				}
			}
		}
		int q; 
		cin>>q;
		for (int i = 0; i < q; ++i)
		{
			int t1,t2,t3,ans=0;
			cin>>t1>>t2>>t3;
			for (int j = t1-540; j < t2-540; ++j) ans+=a[t3][j];
			cout<<ans<<endl;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i,n,N) for(int i=n;i<N;i++)
#define p(S) cout<<S<<endl
using namespace std;


int main(){
	int N,M,r,q;
	while(cin>>N>>M,N){
		int t[1001],n[1001],m[1001],s[1001];
		int ts,te,mq;
		cin>>r;
		REP(i,0,r){
			cin>>t[i]>>n[i]>>m[i]>>s[i];
		}
		cin>>q;
		REP(i,0,q){
			cin>>ts>>te>>mq;
			int ans=0,cnt=0,k=0;
			REP(j,ts,te){
				for(;k<r&&t[k]<=j;k++){
					if(m[k]==mq){
						if(s[k]==0)cnt--;
						else cnt++;
					}
				}
				if(cnt) ans++;
			}
			p(ans);
		}
	}
	return 0;
}