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;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(),(a).end()
#define ms(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10) 
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) { return cost < e.cost; }
	bool operator > (const edge& e) { return cost > e.cost; }
};
///************************************************************///
///************************************************************///
///************************************************************///

int login[10005][1300];
struct data{ int t, n, m, s; };

int main()
{
	int n, m;
	while (cin >> n >> m, n)
	{
		int r, q;
		ms(login, 0);
		queue<data> que;
		cin >> r;
		REP(i, r)
		{
			int a, b, c, d;
			cin >> a >> b >> c >> d;
			c--;
			que.push(data{a,b,c,d});
		}

		FOR(i, 1, 1261)
		{
			REP(j, m)
			{
				login[j][i] = login[j][i - 1];
			}
			while (!que.empty()&&que.front().t == i)
			{
				login[que.front().m][i] += (que.front().s == 1 ? 1 : -1);
				que.pop();
			}
		}
		cin >> q;
		REP(i, q)
		{
			int ans = 0;
			int a, b, c;
			cin >> a >> b >> c;
			c--;
			FOR(j, a, b)
			{
				if (login[c][j] > 0)
				{
					ans++;
					//cout <<"j="<< j << endl;
				}
			}
			cout << ans << endl;
		}

	}
	return 0;
}

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);
        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++: #include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#define pb push_back
using namespace std;

struct rec{
  int t,n,m,s;
  rec(int a,int b,int c,int d){t=a;n=b;m=c;s=d;}
};
struct query{
  int ts,te,m;
  query(int a,int b,int c){ts=a;te=b;m=c;}
};
int main(void){
  int N,M;
  int r,q;
  int t,n,m,s,ts,te;
  vector<rec> R;
  vector<query> Q;
  int t1,t2,rogin;
  int i,j,k;
  int res;
  while(scanf("%d %d",&N,&M),N|M){
    R.clear();
    scanf("%d",&r);
    for(i=0;i<r;i++){
      scanf("%d %d %d %d",&t,&n,&m,&s);
      R.pb(rec(t,n,m,s));
    }
    scanf("%d",&q);
    for(i=0;i<q;i++){
      scanf("%d %d %d",&ts,&te,&m);
      //Q.pb(query(ts,te,m));
      res=rogin=t1=0;
      for(j=0;j<r;j++){
        if(R[j].t>te) break;
        if(R[j].m==m){
          //printf("%d ??????\n",j);
          if(rogin==0) t1=max(R[j].t,ts),rogin++;
          else if(R[j].s==1) rogin++;
          else if(R[j].s==0) rogin--;
          if(rogin==0&&R[j].s==0) res+=max(0,R[j].t-t1);
        }
      }
      if(rogin>0){
        res+=te-t1;
      }
      printf("%d\n",res);
    }
    
  }
  return 0;
}