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;

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;
		}
	}
}

C++: #include <iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef int long long ll;


int main() {
  int N,M;
  while(true){
      cin>>N>>M;
      if(N==0&&M==0)break;
      pair<bool,int> use[N+1][722];
      rep(i,N+1){rep(j,722){use[i][j]={false,0};}}

      int r;
      cin>>r;

      rep(i,r){
          int t,a,b,c;
          cin>>t>>a>>b>>c;
          t-=540;
          if(c==1){
              use[a][t]={true,b};
          }else {
              int k = t;
              k--;
              while (k >= 0 && (use[a][k].first != true && use[a][k].second != b)) {
                  use[a][k] = {true, b};
                  k--;
              }
          }
      }
      int q;
      cin>>q;

      rep(i,q){
          bool aaa[722];
          rep(i,722)aaa[i]=false;
          int str,las,cl;
          cin>>str>>las>>cl;
          int ans=0;
          str-=540;las-=540;
          for(int j=1;j<=N;j++){
              for(int kk=str;kk<las;kk++){
                  if(use[j][kk].first==true&&use[j][kk].second==cl)aaa[kk]=true;
              }
          }
          for(int l=str;l<las;l++){if(aaa[l]==true)ans++;}
      cout<<ans<<endl;
      }



  }
    return 0;

}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
    while(1){
        int n, m;
        cin >> n >> m;
        if (!n || !m) break;
        int r;
        cin >> r;
        vector<vector<int> > data(m, vector<int>(721, 0));
        for (int i = 0; i < r; i++){
            int t, pc, student, s;
            cin >> t >> pc >> student >> s;
            student--;
            t -= 540;
            if (s == 1) data[student][t] += 1;
            else data[student][t] -= 1;
        }
        for (int i = 0; i < m; i++){
            for (int j = 0; j < 720; j++) data[i][j+1] += data[i][j];
        }
        int q;
        cin >> q;
        for (int i = 0; i < q; i++){
            int ts, te, stm;
            cin >> ts >> te >> stm;
            ts -= 540;
            te -= 540;
            stm--;
            int ans = 0;
            for (int j = ts; j < te; j++){
                if (data[stm][j] > 0) ans++;
            }
            cout << ans << endl;
        }
    }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <cassert>
#include <complex>
#define repi(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define endl "\n"
#define ing long long
using namespace std;
const int INF = 1<<30;
const int MOD = (int)1e9 + 7;
const int MAX_N = (int)1e5 + 5;
#define debug(x) cout << #x << ": " << x << endl
signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    while(cin >> N >> M, N or M)
    {
        int times[M][1300] = {};
        int r; cin >> r;
        for(int i = 0; i < r; i++)
        {
            int t, n, m, s; cin >> t >> n >> m >> s;
            m--;
            if(s) times[m][t + 1]++;
            else times[m][t + 1]--;
        }
        for(int i = 0; i < M; i++)
        {
            for(int j = 540; j <= 1300; j++) times[i][j] += times[i][j - 1];
        }
        int q; cin >> q;
        while(q--)
        {
            int s, t, m; cin >> s >> t >> m;
            m--;
            int cnt = 0;
            for(int i = s + 1; i <= t; i++)
            {
                if(times[m][i]) cnt++;
            }
            cout << cnt << endl;
        }
    }
    return 0;
}

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

int S[10000][1261]={};

int main()
{
  int N,M,r,q,t,n,m,s,ts,te,time;

  while(1){
    cin>>N>>M;
    if(N==0 && M==0)break;
    for(int i=0;i<M;i++){
      for(int j=0;j<=1261;j++)S[i][j]=0;
    }
    cin>>r;
    for(int i=0;i<r;i++){
      cin>>t>>n>>m>>s;
      if(s==1)S[m-1][t]=1;
      else S[m-1][t]=-1;
    }
    for(int i=0;i<M;i++){
      for(int j=1;j<=1261;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;j<te;j++){
	if(S[m-1][j]>0)time++;
      }
      cout<<time<<endl;
    }
  }
  return 0;
}