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;

int table[10001][800];

int main()
{
  int N, M;
  while(cin >> N >> M, N || M){
    memset(table, 0, sizeof(table));
    int r, q;
    cin >> r;
    while(r--){
      int t, n, m, s;
      cin >> t >> n >> m >> s; 
      if(s == 1) table[m][t-540] += 1;
      else table[m][t-540] -= 1;
    }
    for(int i = 1; i <= M; i++){
      for(int j = 1; j < 800; j++) table[i][j] += table[i][j-1];
    }
    cin >> q;
    while(q--){
      int ts, te, m;
      int cnt = 0;
      cin >> ts >> te >> m;
      for(int j = ts - 540; j < te - 540; j++){
	if(table[m][j] > 0) cnt++; 
      }
      cout << cnt << endl;
    }
  }
  return 0;
}

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

int main() {

    int n, m, r, q;
    while (cin >> n >> m, (n or m)) {

        cin >> r;

        vector< pair<int, int> > nt[m+1];

        vector<int> tt(r), nn(r), mm(r), ss(r);
        rep(i, r) {
            cin >> tt[i] >> nn[i] >> mm[i] >> ss[i];
            nn[i]--;
            mm[i]--;
        }

        rep(i, r) {
            if (ss[i] == 1) {
                for (int j=i+1; j<r; ++j) {
                    if (nn[i] == nn[j] and mm[i] == mm[j] and ss[j] == 0 and tt[j] != -1) {
                        nt[mm[i]].push_back({tt[i], tt[j]});
                        tt[j] = -1;
                        break;
                    }
                }
            }
        }

        cin >> q;

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

            int ans=0, s=-1, e=-1;
            qm--;

            for (auto se : nt[qm]) {
                int ss = se.first;
                int ee = se.second;

                if (ts < ee) continue;
                if (ss > te) break;

                if (s != -1 and e != -1) {
                    if (e >= ss) {
                        e = max(e, min(ee, te));
                    }
                    else {
                        ans += (e - s);
                        s = ss;
                        e = min(ee, te);
                    }
                }

                if (s == -1) {
                    s = max(ts, ss);
                }

                if (e == -1) {
                    e = min(te, ee);
                }

            }

            cout << ans + (e - s) << endl;

        }

    }
}

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

typedef pair <int, int> PII;
typedef vector<PII> VPII;
typedef vector<VPII> VVPII;
bool U[700];
int L[1001];

int main( void )
{
    int N, M;
    while ( cin >> N >> M && N && M ) {
        VVPII T( M+1 );
        memset( L, -1, sizeof(L) );
        int r, q;
        int t, n, m, s, ts, te;
        cin >> r;
        for ( int i = 0; i < r; i++ ) {
            cin >> t >> n >> m >> s;
            t -= 540;
            if ( s ) {
                L[n] = t;
            } else {
                T[m].push_back( PII( L[n], t ) );
                L[n] = -1;
            }
        }
        cin >> q;
        for ( int i = 0; i < q; i++ ) {
            memset( U, false, sizeof(U) );
            int answer = 0;
            cin >> ts >> te >> m;
            ts -= 540;
            te -= 540;
            for ( VPII ::iterator it = T[m].begin(); it != T[m].end(); it++ ) {
                PII r = *it;
                if ( r.second < ts || r.first > te ) continue;
                for ( int j = r.first; j < r.second; j++ ) U[j] = true;
            }
            for ( int j = ts; j < te; j++ ) if ( U[j] ) answer++;
            cout << answer << 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;
}