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 <map>
#include <stack>
#include <list>
#include <queue>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <math.h>

#define PREP(i, m, n) for(int i = m; i < n; i++)
#define MREP(i, m, n) for(int i = m - 1; i >= n; i--)

#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define all(v) v.begin(), v.end()
#define rev(v) v.rbegin(), v.rend()
#define prime first
#define weight second
#define MP make_pair
#define PB push_back

using namespace std;

int main(){
	
	int student = 0, PC = 0;
	while(cin >> PC >> student && (PC != 0 || student != 0)){
		vector<vector<int> > table(student, vector<int>(720, 0));
		vector<vector<int> > logintime(PC, vector<int>(student));

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

			n--;
			m--;
			t -= 540;

			if(s == 1){
				logintime[n][m] = t;
			}
			else{
				for(int i = logintime[n][m]; i < t; i++){
					if(table[m][i] == 0) table[m][i] = 1;
				}		
			}
		}

		int q = 0;
		cin >> q;
		rep(i, q){
			int t1, t2, m, count = 0;
			cin >> t1 >> t2 >> m;

			t1 -= 540;
			t2 -= 540;
			m--;

			for(int i = t1; i < t2; i++){
				if(table[m][i]) count++;
			}
			cout << count << endl;
		}
	}
	return 0;
}

C++: /*
 * Analyzing Login/Logout Records
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1148
 */

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
//#define LOG(...)
#define FOR(i, a, b) for(int i=(int)(a); i<(int)(b); ++i)
#define REP(i, n) for(int i=0; i<(int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main() {
  int N, M;
  while(cin >> N >> M, N | M) {
    vector<vector<pii>> use(M);
    vi tim(M, 2000);
    vi num(M, 0);
    int r;
    cin >> r;
    REP(i, r){
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      m--;
      tim[m] = min(tim[m], t);
      num[m]+= s * 2 - 1;
      if(num[m] == 0) {
        use[m].push_back({tim[m], t});
        tim[m] = 2000;
      }
    }

    int q;
    cin >> q;
    REP(i, q){
      int a, b, m;
      cin >> a >> b >> m;
      m--;
      int ans = 0;
      for(pii t : use[m]) {
        ans += max(min(t.second, b) - max(t.first, a), 0);
      }
      cout << ans << endl;
    }
  }
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)
#define all(n) n.begin(),n.end()

typedef pair<int, int> p;
const int W = 0, H = 1;


int main()
{
    int n, w, d;
    while(cin >> n >> w >> d && w )
    {
        vector<p> cakes;
        cakes.push_back(p(w, d));
        REP(n)
        {
            int pn, s; cin >> pn >> s; pn--;
            int side[2]; side[W] = cakes[pn].first; side[H] = cakes[pn].second;
            vector<p>::iterator it = cakes.begin();
            rep(j,pn)++it;
            
            cakes.erase(it);
            int sn = W;
            while(side[sn] < s)
            {
                s -= side[sn];
                sn = (sn + 1) % 2;
            }
            int neww[2];
            neww[0] = min(s, side[sn] - s); neww[1] = max(s, side[sn] - s);
            if(sn == W)
            {
                cakes.push_back(p(neww[0], side[H]));
                cakes.push_back(p(neww[1], side[H]));
            }
            else
            {
                cakes.push_back(p(side[W], neww[0]));
                cakes.push_back(p(side[W], neww[1]));
            }
        }
        vector<int> ans;
        REP(n + 1) ans.push_back(cakes[i].first * cakes[i].second);
        sort(all(ans));
        REP(n) cout << ans[i] << " ";
        cout << ans[n] << endl;
    }
    return 0;
}