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>
#define range(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;

int main() {
    int n, m;
    while (cin >> n >> m, n) {
        const int sz = 723,
                  base = 539;
        int sum[m][sz] = {};

        int r;
        cin >> r;
        rep (i, r) {
            int t, nn, mm, s;
            cin >> t >> nn >> mm >> s;
            t -= base;
            mm--;

            if (s) sum[mm][t] += 1;
            else   sum[mm][t] += -1;
        }

        rep (i, m) {
            rep (j, sz - 1) {
                sum[i][j + 1] += sum[i][j];
                sum[i][j] = abs(sum[i][j]) > 1 ? sum[i][j] > 0 ? 1 : -1 : sum[i][j];
            }
            sum[i][sz - 1] = abs(sum[i][sz - 1]) > 1 ? sum[i][sz - 1] > 0 ? 1 : -1 : sum[i][sz - 1];
        }

        rep (i, m) rep (j, sz - 1) {
            sum[i][j + 1] += sum[i][j];
        }

        int q;
        cin >> q;
        rep (i, q) {
            int a, b, c;
            cin >> a >> b >> c;
            a -= base;
            b -= base;
            c--;

            cout << sum[c][b - 1] - sum[c][a - 1] << endl;
        }
    }
    return 0;
}

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

using namespace std;

typedef pair<int, int> time_log;

struct pc_log{
	int m;
	time_log log;
};

typedef struct pc_log pc_log;

int main() {
	int N, M;
	while(cin >> N >> M && N && M) {
		int r; cin >> r;
		vector<vector<pc_log> > log(N);

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

			if(s) { // login
				pc_log tmp;
				tmp.m = m;
				tmp.log.first = t;
				tmp.log.second = -1;

				log[n-1].push_back(tmp);
			}
			else { // logout
				log[n-1][log[n-1].size()-1].log.second = t;
			}
		}

		int q; cin >> q;

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

			vector<time_log> st_log;
			for(int j = 0; j < N; j++) {
				for(int k = 0; k < log[j].size(); k++) {
					if(log[j][k].m == m) {
						st_log.push_back(log[j][k].log);
					}
				}
			}
			sort(st_log.begin(), st_log.end());

			// cout << ts << ":" << te << endl;
			// for(int j = 0; j < st_log.size(); j++) {
			// 	cout << st_log[j].first << "-" << st_log[j].second << endl;
			// }

			int res = 0;
			int cur = 0;

			for(int j = 0; j < st_log.size(); j++) {
				if(te <= st_log[j].first) break;
				while(st_log[j].second <= ts || st_log[j].second <= cur) j++;

				int add = min(te, st_log[j].second) - max(max(ts, cur), st_log[j].first);
				if(add > 0) {
					res += add;
				}

				cur = st_log[j].second;
			}
			cout << res << endl;
		}
	}

	return 0;
}

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

using namespace std;

struct R
{
    int s;
    int e;
};


struct S
{
    int s;
    int c;
    vector<R> r;
};


int main()
{
    int pcN, stN;
    while(cin >> pcN >> stN, stN){
        auto sts = vector<S>(stN);

        for(auto& a: sts){
            a.s = -1;
            a.c = 0;
        }

        int logN;
        cin >> logN;
        while(logN--){
            int t, _, m, s;
            cin >> t >> _ >> m >> s;
            // cout << t << m << s << endl;
            m -= 1;

            sts[m].c += s ? 1 : -1;
            if(sts[m].s == -1)
                sts[m].s = t;

            // cout << sts[m].s << ", " << sts[m].c << endl;

            if(sts[m].c == 0){
                R re = {sts[m].s, t};
                sts[m].r.push_back(re);
                sts[m].s = -1;
            }
        }

        int qN;
        cin >> qN;
        while(qN--){
            int ts, te, m;
            cin >> ts >> te >> m;

            m -= 1;

            int ans = 0;
            for(R& a: sts[m].r){
                // cout << a.s << ", " << a.e << endl;
                ans += max(0, (max(a.e, te) - min(a.s, ts)) - abs(a.s - ts) - abs(a.e - te));
            }

            cout << ans << endl;
        }
    }
}

C++: #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <map>


using namespace std;

#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) repi(i,0,n)
#define mp make_pair
#define pb push_back

struct term {
  int ts, te, n, m;
};
int main()
{
  int N, M;
  while(cin >> N >> M, N||M) {
    int r;
    cin >> r;
    map<pair<int, int>, int> table;
    vector<term> v;
    rep(i, r) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      m--; n--;
      if(s == 1) table[mp(n, m)] = t;
      else {
	v.pb((term){table[mp(n, m)], t, n, m});
      }
    }
    //    cout << "term" << endl;
    rep(i, v.size()) {
      //      cout << v[i].ts << " " << v[i].te << " " << v[i].n << " " << v[i].m << endl;
    }
    int q;
    cin >> q;
    //    cout << endl;
    rep(i, q) {
      int l, r, m;
      cin >> l >> r >> m;
      m--;
      int ans = 0;
      vector<int> s(1261);
      rep(j, v.size()) if(v[j].m == m){
	s[v[j].ts] += 1;
	s[v[j].te] -= 1;
      }
      rep(j, 1260) s[j+1] += s[j];
      repi(j, l, r) if(s[j]) ans++;
      cout << ans << endl;
    }

  }
  return 0;
}