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<cstdio>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
typedef pair<int, int> P;

bool isCross(P p1, P p2) {
  if (p1.second >= p2.first) return true;
  return false;
}

P marge(P p1, P p2) {
  return P(min(p1.first, p2.first), p2.second);
}

int main() {
  int N, M, r, t, n, m, s, q, ts, te, mq;
  while (1) {
    scanf("%d%d", &N, &M);
    if (N == 0) break;
    scanf("%d", &r);
    vector<P> stu[10000]; // login time, logout time
    int pc[1000]; // login time
    for (int i = 0; i < r; i++) {
      scanf("%d%d%d%d", &t, &n, &m, &s);
      n--; m--;
      if (s) pc[n] = t;
      else {
        P p = P(pc[n], t);
        int num = stu[m].size() - 1;
        if (num >= 0 && isCross(stu[m][num], p)) {
          stu[m][num] = marge(stu[m][num], p);
          p = stu[m][num];
          num--;
          while (num >= 0 && isCross(stu[m][num], p)) {
            stu[m][num] = marge(stu[m][num], p);
            stu[m].erase(stu[m].end() - 1);
            num--;
          }
        } else stu[m].push_back(p);
      }
    }
    scanf("%d", &q);
    for (int i = 0; i < q; i++) {
      scanf("%d%d%d", &ts, &te, &mq);
      mq--;
      vector<P>::iterator it1 = lower_bound(stu[mq].begin(), stu[mq].end(), P(ts, ts));
      vector<P>::iterator it2 = upper_bound(stu[mq].begin(), stu[mq].end(), P(te, te));
      if (it2 == stu[mq].begin()) printf("0\n");
      else {
        int sum = 0;
        if (it1 != stu[mq].begin() && (*(it1 - 1)).second > ts) {
          if ((*(it1 - 1)).second > te) sum += te - ts;
          else sum += (*(it1 - 1)).second - ts;
        }
        if (it1 == it2) { printf("%d\n", sum); continue; }
        it2--;
        for (; it1 != it2; it1++) sum += (*it1).second - (*it1).first;
        if ((*it2).second > te) sum += te - (*it2).first;
        else sum += (*it2).second - (*it2).first;
        printf("%d\n", sum);
      }
    }
  }
}

C++: #define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
	for (;;) {
		int N, M; cin >> N >> M;
		if (N == 0) break;
		vector< vector<int> > a(M, vector<int>(1300));
		int r; cin >> r;
		while (r--) {
			int t, n, m, s; cin >> t >> n >> m >> s;
			m--;
			a[m][t] += (s ? 1 : -1);
		}
		vector< vector<int> > s(M, vector<int>(1300));
		for (int m = 0; m < M; m++)
			for (int t = 1; t < 1300; t++)
				s[m][t] = s[m][t - 1] + a[m][t - 1];
		int q; cin >> q;
		while (q--) {
			int ts, te, m; cin >> ts >> te >> m;
			m--;
			int cnt = 0;
			for (int t = ts + 1; t <= te; t++)
				if (s[m][t]) cnt++;
			cout << cnt << endl;
		}
	}
}

C++: #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))

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 pc_n, student_n;
    while (cin >> pc_n >> student_n, pc_n|student_n) {
        vvi log(pc_n+1, vi(721, -1));
        int r;
        cin >> r;
        REP(i, r) {
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            if (s) {
                log[n][t-540] = m;
            } else {
                for (int j = t-540-1; log[n][j] != m; j--) {
                    log[n][j] = m;
                }
            }
        }

//        FOR(i, 1, pc_n+1) {
//            REP(j, 720) {
//                if (log[i][j] != -1) {
//                    LOG("%d", log[i][j]);
//                } else {
//                    LOG(" ");
//                }
//            }
//            LOG("\n");
//        }

        int q;
        cin >> q;
        REP(i, q) {
            int ans = 0;
            int ts, te, m;
            cin >> ts >> te >> m;
            FOR(t, ts, te) {
                FOR(j, 1, pc_n+1) {
                    if (log[j][t-540] == m) {
                        ans++;
                        break;
                    }
                }
            }
            cout << ans << endl;
        }
    }
}