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++: // Analyzing Login/Logout Records
// http://mofupp.info/pukiwiki/?plugin=attach&pcmd=open&file=2007B_pes.cpp&refer=ICPC%2Fdomestic%2F2007B

#include <iostream>
#include <vector>

using namespace std;

const int MaxN = 1001;
const int MaxM = 10001;

int N, M;

int main() {
  int count[MaxM];
  //vector < vector< pair<int,int> > > logg(MaxM);
  while(cin>>N>>M && !(N==0 && M==0)) {
    vector < vector< pair<int,int> > > logg(MaxM);
    int r;
    cin>>r;
    int t,n,m,s;
    for(int i=0; i<MaxM; i++) {
      count[i] = 0;
    }
    for(int i=0; i<r; i++) {
      cin>>t>>n>>m>>s;
      if(s == 1) { // login
	count[m]++;
	if(count[m] == 1) {
	  logg[m].push_back(make_pair(t,t));
	}
      }
      else { // logout
	count[m]--;
	if(count[m] == 0) {
	  logg[m].back().second = t;
	}
      }
    }
    int q;
    cin>>q;
    for(int i=0; i<q; i++) {
      int ts, te, m, ans = 0;
      cin>>ts>>te>>m;
      for(int j=0; j<logg[m].size(); j++) {
	if(logg[m][j].first < te) {
	  ans += min(te, logg[m][j].second)-max(ts, logg[m][j].first);
	}
      }
      cout<<ans<<endl;
    }
    //logg.clear();
  }
}

C++: #include <bits/stdc++.h>
#define INF 1e18
#define int long long
#define Rep(i, a, n) for (int i = (a); i < (n); i++)
#define Repr(i, n, a) for (int i = (n); i >= (a); i--)
#define rep(i, n) Rep(i, 0, n)
#define repr(i, n) Repr(i, n, 0)
#define all(a) a.begin(), a.end()
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;

int a[10010][1310];

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, m;
    while (1)
    {
        cin >> n >> m;
        if (n == 0 && m == 0)
            break;
        rep(i, 10010) rep(j, 1310) a[i][j] = 0;
        int r;
        cin >> r;
        rep(i, r)
        {
            int T, N, M, S;
            cin >> T >> N >> M >> S;
            int x = (S == 1 ? 1 : -1);
            T++;
            a[M - 1][T] += x;
        }
        rep(i, m) rep(j, 1300) a[i][j + 1] += a[i][j];
        rep(i, m) rep(j, 1300) if (a[i][j] > 1) a[i][j] = 1;
        rep(i, m) rep(j, 1300) a[i][j + 1] += a[i][j];
        int q;
        cin >> q;
        while (q--)
        {
            int A, B, C;
            cin >> A >> B >> C;
            C--;
            cout << a[C][B] - a[C][A] << endl;
        }
    }
}

C++: #include <iostream>
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <math.h>
using namespace std;

struct rec{
	int t,m,s;
};

int main(){
	
	int N,M,r,t,n,m,s,ts,te,in,start,pd,q,sw,sw2;
	rec rec[1000];
	
	while(cin>>N>>M,N||M){
		cin>>r;
		for(int i=0;i<r;i++){
			cin>>t>>n>>m>>s;
			rec[i].t=t;rec[i].m=m;rec[i].s=s;
		}
		cin>>q;
		for(int h=0;h<q;h++){
			cin>>ts>>te>>m;
			in=0;pd=0;sw=0;sw2=0;
			for(int i=0;i<r;i++){
				if(rec[i].m!=m)continue;
				if(rec[i].t>=ts&&sw==0){
					if(in>0)start=ts;sw=1;}
				if(rec[i].t>=te){
					if(in>0)pd+=te-start;sw2=1;
					break;
				}
				if(rec[i].s==1){
					if(rec[i].t>=ts&&in==0)start=rec[i].t;
					in++;
				}
				else {
					if(rec[i].t>ts&&in==1)pd+=rec[i].t-start;
					in--;
				}
			}
		if(sw2==0&&in>0)pd+=te-start;
		cout<<pd<<endl;
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <istream>
#include <sstream>
#include <iomanip>
using namespace std;

typedef ostringstream oss;
typedef istringstream iss;

typedef vector<int> vi;
typedef vector< vi > vvi;

const int MIN_TIME = 540;
const int MAX_TIME = 1260;

bool isNotZero(int x) { return x; };

int main(void) {
	int pcNum, stuNum;
	while (cin >> pcNum >> stuNum, pcNum) {
		vvi recs(stuNum, vi(MAX_TIME - MIN_TIME + 1, 0));

		int recNum, queNum;
		cin >> recNum;
		for (int ri = 0; ri < recNum; ri++) {
			int time, pc, stu, type;
			cin >> time >> pc >> stu >> type;
			pc--; stu--;
			time -= MIN_TIME;
			if (type) {
				for (int i = time; i < recs[stu].size(); i++) {
					recs[stu][i] = recs[stu][i] + 1;
				}
			}else {
				for (int i = time; i < recs[stu].size(); i++) {
					recs[stu][i] = recs[stu][i] - 1;
				}

			}
		}

		cin >> queNum;
		for (int qi = 0; qi < queNum; qi++) {
			int start, end, stu;
			cin >> start >> end >> stu;
			stu--;
			start -= MIN_TIME; end -= MIN_TIME;

			int time = 0;
			time = count_if(recs[stu].begin() + start, recs[stu].begin() + end, isNotZero);

			cout << time << endl;
		}
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define MOD 1000000007
typedef long long ll;
using namespace std;

int N,M;

int R,Q;
int t[1001],n[1001],m[1001],s[1001];
int st[51],ed[51],tar[51];

void solve(){
  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++) cin>>st[i]>>ed[i]>>tar[i];

  vector<int> ans;
  for(int i=0;i<Q;i++){
    int res=0;
    int dep=0;
    int start=0;
    for(int j=0;j<R;j++){
      if(tar[i]!=m[j]) continue;
      if(s[j]==1){
        if(dep==0) start=t[j];
        dep++;
      }else{
        dep--;
        if(dep==0){
          int ma=max(st[i],start);
          int mi=min(ed[i],t[j]);
          if(mi-ma>0) res+=(mi-ma);
          start=0;
        }
      }
    }
    ans.push_back(res);
  }

  for(int i=0;i<ans.size();i++) cout<<ans[i]<<endl;
}

int main(){
  while(1){
    cin>>N>>M;
    if(N==0&&M==0) break;
    solve();
  }

  return 0;
}