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 unsigned long long ull;
#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)
#define all(a) (a).begin(), (a).end()

const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

int main(){
	int n, m;
	while(cin >> n >> m, n+m){
		int idx = 0;

		int r;
		cin >> r;
		vector<vector<int> > v1, v2;
		rep(i, r){
			vector<int> vv(4);
			cin >> vv[2] >> vv[0] >> vv[1] >> vv[3];
			if(vv[3] == 1) v1.push_back(vv);
			else v2.push_back(vv);
		}
		sort(all(v1));
		sort(all(v2));

		int q; cin >> q;
		rep(qq, q){
			bool student[1300]; rep(j, 1300) student[j] = false;
			int a, b, c;
			cin >> a >> b >> c;
			int ret = 0;

			for(int i=0; i< v1.size(); i++){
				if(v1[i][1] == c){
					for(int j = v1[i][2]; j < v2[i][2]; j++) student[j] = true;
				}
			}
			for(int i = a; i < b; i++) if(student[i]) ret++;
			cout << ret << endl;
		}
	}
}

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

using namespace std;

class student {
public:
    int st[100], ed[100];
    int num, cnt;
    student() { num = 0; cnt = 0; }
};

int main()
{
    int N; // 学生数
    int M; // PC数

    while (cin >> N >> M, N || M) {
        int r;  // 利用記録数
        int q;  // 質問数
        vector<student> std(N);

        // 登録
        cin >> r;
        for (int i = 0; i < r; i++) {
            int t, n, m, s;  // s(1: ログイン, 0: ログアウト), 時刻tにPCnで学生mが
            cin >> t >> n >> m >> s;

            if (s == 1) { // ログイン
                if (std[m].cnt == 0) {  // まだログインしていない
                    std[m].st[std[m].num] = t;
                }
                std[m].cnt++;
            }
            else { // ログアウト
                if (std[m].cnt == 1) 
                    std[m].ed[std[m].num++] = t;
                std[m].cnt--;
            }
        }

        // 質問
        cin >> q;
        for (int i = 0; i < q; i++) {
            int ts, te, m;
            int ans = 0;

            cin >> ts >> te >> m;

            for (int j = 0; j < std[m].num; j++) {
                if (std[m].ed[j] <= ts)
                    continue;
                if (std[m].st[j] >= te)
                    break;
                
                if (std[m].st[j] <= ts && std[m].ed[j] >= te) {
                    ans += te - ts;
                    break;
                }
                if (std[m].st[j] <= ts && std[m].ed[j] < te)
                    ans += std[m].ed[j] - ts;
                else if (std[m].st[j] >= ts && std[m].ed[j] >= te) {
                    ans += te - std[m].st[j];
                    break;
                }
                else if (std[m].st[j] >= ts && std[m].ed[j] < te) 
                    ans += std[m].ed[j] - std[m].st[j];
            }
            cout << ans << endl;
        }
    }

    return 0;
}

C++: #include<vector>
#include<cmath>
#include<map>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<string>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<stack>
#include<bitset>
#include<functional>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<deque>
using namespace std;
#define pb push_back
typedef long long lint;
#define mp make_pair
#define fi first
#define se second
typedef pair<int,int> pint;
int main(){
	int n,m,r,q,i,j;vector <int> out;
	while(1){
		cin>>n>>m;
		if(n==0) break;
		vector<vector<int> > sei(m+1,vector<int>(1300,0));
		int r,q;
		cin>>r;
		for(i=0;i<r;i++){
			int t1,n1,m1,s;cin>>t1>>n1>>m1>>s;
			if(s==1) sei[m1][t1]++;
			else sei[m1][t1]-=1;
		}
		for(i=0;i<=m;i++){
			for(j=500;j<=1295;j++){
				sei[i][j]+=sei[i][j-1];
			}
		}
		cin>>q;
		for(i=0;i<q;i++){
			int s2,e,m2;cin>>s2>>e>>m2;
			int ret=0;
			for(j=s2;j<e;j++) if(sei[m2][j]>0) ret++;
			out.pb(ret);
		}
	}
	for(i=0;i<out.size();i++) cout<<out[i]<<endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "../dump.hpp"
#else
#define dump(...)
#endif

#define rep(i,n) for(ll i=0,i##_cond=(n);i<i##_cond;++i)
#define FOR(i,a,b) for(ll i=(a),i##_cond=(b);i<i##_cond;++i)
#define ROF(i,a,b) for(ll i=(a)-1,i##_cond=(b);i>=i##_cond;--i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend() //sortで大きい順
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
#define SUM(a) accumulate(all(a),0)
#define sz(x) ((ll)(x).size())
#define pb push_back
#define fst first
#define snd second
#define mp make_pair

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<ll,ll> pii;

const ll inf = 1ll<<62;
const ll mod = 1e9+7;

int main(){
#define int ll
  while(1){
    int n,m; cin >> n >> m; if(n == 0 and m == 0) break;
    vvi stu(m,vi(1270));
    int r; cin >> r;
    rep(i,r){
      int t,a,b,s; cin >> t >> a >> b >> s;
      b--;
      if(s) stu[b][t]++;
      else stu[b][t]--;
    }
    rep(i,m) rep(j,1269)
      stu[i][j+1] += stu[i][j];
    //dump(stu);
    int q; cin >> q;
    rep(i,q){
      int ts,te,a; cin >> ts >> te >> a; a--;
      int ans = 0;
      FOR(j,ts,te)
	if(stu[a][j]) ans++;
      cout << ans << endl;
    }
  }
}