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>
#define S 540
#define T 720 //1260 - 540
using namespace std;

int main()
{
	int n, m, r;
	while (cin >> n >> m, n) {
		vector<vector<int> > v(m, vector<int>(T, 0));
		cin >> r;
		while (r--) {
			int t, pc, st, s;
			cin >> t >> pc >> st >> s;
			v[st-1][t-S] += s?1:-1;
		}

		for (int i = 0; i < m; i++) {
			int sum = 0, state = 0;
			for (int j = 0; j < T; j++) {
				sum += !!state;
				state += v[i][j];
				v[i][j] = sum;
			}
		}
		int q, ts, te, st;
		cin >> q;
		while (q--) {
			cin >> ts >> te >> st;
			cout << v[st-1][te-S] - v[st-1][ts-S] << endl;
		}
	}
	
	return 0;
}

C++: #include<iostream>

using namespace std;

int cnt[10001][1261], sum[10001][1261], N, M, R, Q;
int main()
{
 loop:;
  fill((int*)cnt, (int*)cnt + 10001 * 1261, 0);
  fill((int*)sum, (int*)sum + 10001 * 1261, 0);
  cin >> N >> M;
  if(!N)
    return 0;
  cin >> R;
  for(int i = 0; i < R; i++)
  {
    int t, n, m, s;
    cin >> t >> n >> m >> s;
    if(s)
      cnt[m][t]++;
    else
      cnt[m][t]--;
  }
  for(int m = 1; m <= M; m++)
  { 
    sum[m][0] = cnt[m][0] ? 1 : 0;
    for(int i = 1; i <= 1260; i++)
    {
      cnt[m][i] = cnt[m][i - 1] + cnt[m][i];
      sum[m][i] = sum[m][i - 1] + (cnt[m][i] ? 1 : 0);
    }
  }
  cin >> Q;
  for(int i = 0; i < Q; i++)
  {
    int ts, te, m;
    cin >> ts >> te >> m;
    cout << sum[m][te - 1] - sum[m][ts - 1] << endl;
  }
  goto loop;
}

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

typedef long long ll;

using namespace std;

namespace Solver {

    int N, M, r, q;
    vector<int> t, n, m, s, qts, qte, qm;
    inline bool Read() {
        cin >> N >> M;
        if (N == 0) return false;

        cin >> r;
        t.resize(r);
        n.resize(r);
        m.resize(r);
        s.resize(r);
        for (int i = 0; i < r; i++) cin >> t[i] >> n[i] >> m[i] >> s[i];

        cin >> q;
        qts.resize(q);
        qte.resize(q);
        qm.resize(q);
        for (int i = 0; i < q; i++) cin >> qts[i] >> qte[i] >> qm[i];

        return true;
    }

    inline void Work() {
        for (int i = 0; i < q; i++) {
            int ans = 0, pc = 0, st = 0;
            for (int j = 0; j < r; j++) {
                if (qm[i] == m[j]) {
                    if (s[j] == 1) {
                        if (pc == 0) st = t[j];
                        pc++;
                    } else {
                        if (pc == 1) ans += max(0, min(qte[i], t[j]) - max(qts[i], st));
                        pc--;
                    }
                }
            }

            cout << ans << endl;
        }
    }

    inline void Solve() {
        while (Read()) Work();
    }
}

int main() {
    Solver::Solve();
}

C++: #include<iostream>
#include <list>
#include<stack>
#include<queue>
#include <vector>
#include <set>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
#include<string>
#include <functional>
#include<fstream>

#define FOR(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define REP(i,n) FOR((i),0,(n))
#define LL long long
#define CLR(a) memset((a),0,sizeof(a))
#define SZ(x) (int((x).size()))
#define WAITING(str) int str;std::cin>>str;
#define DEBUGING(str) cout<<str<<endl
using namespace std;

const LL MOD = 1000000007;// 10^9+7
const int INF = (1 << 30);


//変数
struct Database {
	int N, M, r, q;
	vector<vector<int>> use;

	Database() 
	{
		cin >> N >> M;
		if (N == 0)return;
		use.resize(M+1, vector<int>(60 * 24));

		cin >> r;
		REP(i, r) {
			int t, n, m, s;
			cin >> t >> n >> m >> s;
			use[m][t] += (s == 1 ? 1 : -1);
		}
		REP(i, M+1)REP(t, 60 * 24-1)
			use[i][t + 1] += use[i][t];
	}
	
	void query()
	{

		int ts, te, m;
		cin >> ts >> te >> m;
		
		int res = 0;
		FOR(t, ts, te) {
			res += (use[m][t] > 0 ? 1 : 0);
		}
		cerr << "==========";
		cout << res << endl;
	}
};


//メイン関数
int main()
{
	while (true) {
		Database base;
		if (base.N == 0)break;

		int q;
		cin >> q;
		REP(i, q)base.query();
	}

	return 0;
}

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

int main()
{
  int N,M,r,q,t,n,m,s,S[10001][721],ts,te,time;

  while(1){
    cin>>N>>M;
    if(N==0 && M==0)break;
    for(int i=1;i<=M;i++){
      for(int j=0;j<721;j++)S[i][j]=0;
    }
    cin>>r;
    for(int i=0;i<r;i++){
      cin>>t>>n>>m>>s;
      if(s==1)S[m][t-540]=1;
      else S[m][t-540]=-1;
    }
    for(int i=1;i<=M;i++){
      for(int j=1;j<721;j++){
        S[i][j]+=S[i][j-1];
      }
    }
    cin>>q;
    for(int i=0;i<q;i++){
      cin>>ts>>te>>m;
      time=0;
      for(int j=ts-540;j<te-540;j++){
	if(S[m][j]>0)time++;
      }
      cout<<time<<endl;
    }
  }
  return 0;
}