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;

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

int N, M;

int main() {
  int count[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++) { // init
      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;
      int ans1, ans2; // 4debug ans1は誤りらしい; エラーデータを弾けないから?
      for(int j=0; j<logg[m].size(); j++) {
	
	if(logg[m][j].first == logg[m][j].second) {
	  logg[m][j].second = te;
	}
	
	if(logg[m][j].first < te) {
	  ans1 = min(te, logg[m][j].second)-max(ts, logg[m][j].first);
	}
	else {
	  ans1 = 0;
	}
	ans2 = max(0, min(te,logg[m][j].second)-max(ts,logg[m][j].first));
	
	/*
        if(ans1!=ans2) {
	  cout<<"Q: "<<ts<<" "<<te<<endl;
	  cout<<"why: "<<min(te, logg[m][j].second)-max(ts, logg[m][j].first)<<endl;
	  cout<<"val: "<<logg[m][j].first<<" "<<logg[m][j].second<<endl;
	  cout<<"ans: "<<ans1<<" "<<ans2<<endl;
	}
	*/
	
        ans += ans1;
      }
      cout<<ans<<endl;
    }
  }
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>


using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define clear(arr, val) memset(arr, val, sizeof(arr))

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

template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };


int main()
{
	int n, m, r, q;
	while (cin >> n >> m, n)
	{
		cin >> r;
		static int stu[10010][2048];
		clear(stu, 0);
		while (r--)
		{
			int t, n, m, s;
			cin >> t >> n >> m >> s;
			if (s == 0)
				s = -1;
			stu[m][t] += s;
		}

		cin >> q;
		while (q--)
		{
			int s, e, m, res = 0;
			cin >> s >> e >> m;
			for (int t = 540, in = 0; t < e; ++t)
			{
				in += stu[m][t];
				if (t >= s && in > 0)
					++res;
			}
			cout << res << endl;
		}
	}
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
const int INF=1<<29,MOD=1e9+7;

int main(){
	int n,m;
	while(cin>>n>>m&&n){
		int t[m][1270]; rep(i,m)rep(j,1270) t[i][j]=0;
		int r; cin>>r;
		rep(i,r){
			int time,pc,mem,log;
			cin>>time>>pc>>mem>>log;
			mem--;
			if(log) t[mem][time]++;
			else t[mem][time]--;
		}
		int q; cin>>q;
		rep(i,m)rep(j,1269) t[i][j+1]+=t[i][j];
		rep(i,q){
			int ans=0;
			int in,out,mem;
			cin>>in>>out>>mem;
			mem--;
			for(int j=in;j<out;j++){
				if(t[mem][j]) ans++;
			}
			cout<<ans<<endl;
		}
	}
	return 0;
}

// 600=1
// 700=-1

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

int s[10000][750];
signed main() {
	int a, b, c; 
	while (cin >> a >> b, a | b) {
		memset(s, 0, sizeof(s));
		cin >> c;
		for (int d = 0; d < c; d++) {
			int e, f, g, h; scanf("%d%d%d%d", &e, &f, &g, &h); g--;
			e -= 540;
			if (h) {
				s[g][e]++;
			}
			else {
				s[g][e]--;
			}
		}
		for (int d = 0; d < b; d++) {
			int k = 0;
			for (int e = 0; e < 750; e++) {
				k += s[d][e];
				s[d][e] = k;
			}
		}
		int d; cin >> d;
		for (int e = 0; e < d; e++) {
			int f, g, h; scanf("%d%d%d", &f, &g, &h); f -= 540; g -= 540; h--;
			int sum = 0;
			for (; f < g; f++) {
				if (s[h][f])sum++;
			}
			cout << sum << endl;
		}
	}
}