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 <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());

			int res = 0;
			int cur = ts;

			for(int j = 0; j < st_log.size(); j++) {
				if(!j) {
					res += (st_log[j].second - max(cur, st_log[j].first));
					cur = st_log[j].second;
				}
				else {
					if(cur < st_log[j].first) {
						res += (st_log[j].second - st_log[j].first);
						cur = st_log[j].second;
					}
					else { // cur >= st_log[j].first
						if(cur < st_log[j].second) {
							res += (st_log[j].second - cur);
							cur = st_log[j].second;
						}
					}
				}
			}
			if (cur > te) {
				res -= cur - te;
			}
			cout << res << endl;
		}


		// for(int i = 0; i < N; i++) {
		// 	for(int j = 0; j < log[i].size(); j++) {
		// 		cout << log[i][j].m << ":" << log[i][j].log.first << "-" << log[i][j].log.second << " ";
		// 	}
		// 	cout << endl;
		// }
	}

	return 0;
}

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

int N, M, r, q;
int t[1024], n[1024], m[1024], s[1024];

vector<pair<pair<int, int>, int> > query;
bool pc_status[10010];

bool input() {
	cin >> N >> M;
	if(N == 0 && M == 0)
		return false;
	cin >> r;
	for(int i = 0; i < r; i++) {
		cin >> t[i] >> n[i] >> m[i] >> s[i];
	}
	cin >> q;
	for(int i = 0; i < q; i++) {
		int tmp_ts, tmp_te, tmp_m;
		cin >> tmp_ts >> tmp_te >> tmp_m;
		query.push_back( pair<pair<int,int> ,int>(pair<int,int>(tmp_ts, tmp_te), tmp_m) );
	}

	return true;
}

void init() {
	query.clear();
	for(int i = 0; i < 10010; i++)
		pc_status[i] = false;
}

int solve(pair<pair<int,int>,int> que) {
	int use = 0;
	int ret = 0;
	int start = -1, end = -1;
	for(int i = 0; i < r; i++) {
		if(m[i] == que.second) {
			if(s[i] == 1) {
				if(use == 0)
					start = t[i];
				use++;
			}

			else {
				use--;
				if(use == 0)
					end = t[i];
			}

			if(use == 0) {
				if(start < que.first.second && end > que.first.first) {
					ret += min(end, que.first.second) - max(start, que.first.first);
				}
			}
		}
	}

	return ret;
}

int main(void) {
	while(init(), input()) {
		for(int i = 0; i < q; i++) {
			cout << solve(query[i]) << endl;
		}
	}
	return 0;
}

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

class hist{
public:
	int s,e;
	hist(int s,int e):s(s),e(e){};
};

bool operator < (hist a,hist b){
	if(a.s!=b.s) return a.s<b.s;
	return a.e<b.e;
}

int main()
{
	int N,M;
	while(cin>>N>>M && N!=0){
		vector<int> PC(N+1,-1);
		vector<vector<hist> > Stu(M+1);
		int r;
		cin>>r;
		for(int i=0;i<r;i++){
			int t,n,m,s;
			cin>>t>>n>>m>>s;
			if(s==0)
				Stu[m].push_back(hist(PC[n],t));
			else{
				PC[n]=t;
			}
		}
		int q;
		cin>>q;
		for(int i=0;i<q;i++){
			int s,e,m;
			cin>>s>>e>>m;
			sort(Stu[m].begin(),Stu[m].end());
			int ans=0;
			int time=s;
			for(int k=0;k<Stu[m].size();k++){
				if(min(Stu[m][k].e,e)-max(time,Stu[m][k].s)<0) continue;
				ans+=min(Stu[m][k].e,e)-max(time,Stu[m][k].s);
				time=min(Stu[m][k].e,e);
			}
			cout<<ans<<endl;
		}
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define SQ(x) ((x)*(x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1<<29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;



int re[10001][1300];
int N,M;


int main(){
  while(1){
    memset(re,0,sizeof(re));
    int r,q,t,n,m,s,ts,te;
    scanf("%d%d",&N,&M);
    if(N+M==0) break;
    scanf("%d",&r);
    rep(i,r){
      scanf("%d%d%d%d",&t,&n,&m,&s); m--;
      if(s) re[m][t]++;
      else re[m][t]--;
    }

    rep(i,10000){
      rep(j,1270) re[i][j+1] += re[i][j];
    }

    scanf("%d",&q);
    rep(i,q){
      scanf("%d%d%d",&ts,&te,&m); m--;
      int res = 0;
      for(int j=ts;j<te;j++) if(re[m][j]>0) res++;
      //      for(int j=ts;j<te;j++) cout << re[m][j] << " "; cout << endl;
      printf("%d\n", res);
    }
  }
  return 0;
}