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>
using namespace std;

typedef vector<int> V;
typedef pair<int,int> P;

typedef struct{
  int t,n,m,s;
} Record;

int main(){
  int n,m,r,q;
  
  while(cin >> n >> m ,n + m){
    const int MAX = 1261;

    cin >> r;
    Record d[r];
    vector<V> v(m,V(MAX,2)),cnt(m,V(MAX,0));

    for(int i = 0 ; i < r ; i++){
      cin >> d[i].t >> d[i].n >> d[i].m >> d[i].s;
      v[--d[i].m][d[i].t] = d[i].s;
    }

    for(int i = 0 ; i < m ; i++){
      cnt[i][540] = (v[i][540] == 1) ? 1 : 0;
      for(int j = 541 ; j < MAX ; j++){
	cnt[i][j] = cnt[i][j-1] + ((v[i][j] == 1) ? 1 : 0);
	if(v[i][j] == 0) cnt[i][j]--;
      }
    }

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

      sn--;
      int ans = 0;
      for(int i = ts ; i < tt ; i++){
	if(cnt[sn][i]) ans++;
      } 
      cout << ans << endl;
    }
  }

  return 0;
}

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

using namespace std;



int main(){

	int N,M,r;
	while(cin >> N >> M && !(N == 0 && M == 0)){
		// Ô
		vector<pair<int,int> > Gakusei[1001];
		cin >> r;
		pair<int,int> pc[10001];
		for(int i = 0; i < r; i++){
			int t;
			int n;
			int m;
			int s;
			cin >> t >> n >> m >> s;
			if(s == 1){
				pc[n].first = t;
			}
			else{
				pc[n].second = t;
				Gakusei[m].push_back(pc[n]);
			}
		}

		int q;
		cin >> q;
		bool term[1261];
		for(int i = 0; i < q; i++){
			fill(term,term+1261,false);
			int cnt = 0;
			int ts,te,m;
			cin >> ts >> te >> m;

			sort(Gakusei[m].begin(),Gakusei[m].end());
			int cTime = ts;
			for(int j = 0; j < Gakusei[m].size(); j++){
				// æÃ~ÉJnÔª¢©ç©Ä¢­
				
				if(cTime > Gakusei[m][j].second)
					continue;
				else{
					cnt += (min(te,Gakusei[m][j].second) - max(cTime,Gakusei[m][j].first));
					cTime = Gakusei[m][j].second;
				}
			}
			cout << cnt << endl;
		}

	}
	return 0;
}

C++: /*
 * Analyzing Login/Logout Records
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1148
 */

#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))
#define SQ(n) (n) * (n)

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 N, M;
  while(cin >> N >> M, N | M) {
    vector<vector<pii>> use(M);
    vi tim(M, 2000);
    vi num(M, 0);
    int r;
    cin >> r;
    REP(i, r){
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      m--;
      tim[m] = min(tim[m], t);
      num[m]+= s * 2 - 1;
      if(num[m] == 0) {
        use[m].push_back({tim[m], t});
        tim[m] = 2000;
      }
    }

    int q;
    cin >> q;
    REP(i, q){
      int a, b, m;
      cin >> a >> b >> m;
      m--;
      int ans = 0;
      for(pii t : use[m]) {
        ans += min(t.second, b) - max(t.first, a);
      }
      cout << ans << endl;
    }
  }
}

C++: //14:17
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

#define all(v) v.begin(),v.end()
#define INF 1<<30
#define mp 
#define pb push_back

#define fi first
#define se second

#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
typedef vector<pair<int, int> > vp;

typedef long long ll;
int per[10100][1010];
int stu[10100][1010][3];
int ln[10100];//???????????¨????????°?????°
int main(){
	
	//stu[m] [0]?????? t[1]PC n[2]?????°?????? s
	while(1){
		int n,m;
		int r;
		cin >> n >> m;
		if(n==0&&m==0) break;
		cin >> r;
		rep(i,m){
			ln[i]=0;
			rep(j,1010){
				per[i][j]=0;
				rep(k,3){
					stu[i][j][k]=0;
				}
			}
		}
		rep(i,r){
			int t,n,m,s;
			cin >> t >> n >> m >> s;
			m-=1;
			stu[m][ln[m]][0] = t;
			stu[m][ln[m]][1] = n;
			stu[m][ln[m]][2] = s;
			ln[m]++;
 		}
 		rep(i,m){
 			int ms = 0;
 			int p = 0;
 			rep(j,ln[i]){
 				if(stu[i][j][2]==1){

 					if(ms==0){
 						per[i][p++] = stu[i][j][0];
 					}
 					ms++;
 				}else if(stu[i][j][2]==0){
 					if(ms==1){
 						per[i][p++] = stu[i][j][0];
 					}
 					ms--;
 				}
 			}
 			ln[i]=p;

 		}
 	// 	cout << "debug" << endl;
 	// 	rep(i,m){
		// 	rep(j,ln[i]){
		// 		cout << "j:"<<j<<""<< per[i][j] << " ";
		// 	}
		// 	cout << endl;
		// }
		// cout << endl;


		int q;
		cin >> q;
		rep(i,q){
			int ans =0;
			int ts,te,m;
			cin >> ts >> te >> m;
			m--;
			for(int j=0;j<ln[m]/2;j++){
				ans += max(0,min(te,per[m][j*2+1])-max(ts,per[m][j*2]));
			}
			// cout << "ans" << endl;
			cout << ans << endl;
		}

		

	}
	return 0;
}