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;
#define rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define MOD 1000000007
#define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD)
typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec; typedef pair<int, int> pi;
int dd[] = { 0, 1, 0, -1, 0 };
#define all(u) begin(u),end(u)
#define mp make_pair
#define pb push_back


int main()
{
  while(true)
    {
      int N, M;
      scanf("%d%d", &N,&M);
      if(N == 0 && M == 0)break;
      int A[10010][1500]={0};
      //int A[10010][1500];
      //memset(A,0,sizeof(A));
      int r;
      scanf("%d", &r);
      rep(i, r)
	{
	  int t, n, m, s;
	  scanf("%d%d%d%d", &t, &n, &m, &s);
	  if(s == 1)
	    {
	      A[m][t]++;
	    }
	  else
	    {
	      A[m][t+1]--;
	    }
	}
      repi(i, 1, M + 1)
	{
	  rep(t, 1300)
	    {
	      if(t == 0)continue;
	      A[i][t] += A[i][t-1];
	    }
	}

      int q;
      scanf("%d", &q);
      rep(i, q)
	{
	  int ts, te, m;
	  scanf("%d%d%d", &ts,&te,&m);
	  int start = -1;
	  int ans = 0;
	  repi(t, ts, te+1)
	    {
	      if(start == -1)
		{
		  if(A[m][t] > 0)
		    {
		      start = t;
		    }
		}
	      else
		{
		  if(A[m][t] <= 0)
		    {
		      ans += t - start - 1;
		      start = -1;
		    }
		}
	    }
	  if(start != -1)ans += te - start;
	  printf("%d\n", ans);
	}
    }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))
using ll = long long;

int main() {

    int n, m, r, q;
    while (cin >> n >> m, (n or m)) {

        cin >> r;

        vector< pair<int, int> > nt[m+1];

        vector<int> tt(r), nn(r), mm(r), ss(r);
        rep(i, r) {
            cin >> tt[i] >> nn[i] >> mm[i] >> ss[i];
            nn[i]--;
            mm[i]--;
        }

        rep(i, r) {
            if (ss[i] == 1) {
                for (int j=i+1; j<r; ++j) {
                    if (nn[i] == nn[j] and mm[i] == mm[j] and ss[j] == 0 and tt[j] != -1) {
                        nt[mm[i]].push_back({tt[i], tt[j]});
                        tt[j] = -1;
                        break;
                    }
                }
            }
        }

        cin >> q;

        rep(i, q) {
            int ts, te, qm;
            cin >> ts >> te >> qm;

            int ans=0, s=-1, e=-1;
            qm--;

            for (auto se : nt[qm]) {
                int ss = se.first;
                int ee = se.second;

                if (ss > te) break;

                if (s != -1 and e != -1) {
                    if (e >= ss) {
                        e = max(e, min(ee, te));
                    }
                    else {
                        ans += (e - s);
                        s = ss;
                        e = min(ee, te);
                    }
                }

                if (s == -1) {
                    s = max(ts, ss);
                }

                if (e == -1) {
                    e = min(te, ee);
                }

            }

            cout << ans + (e - s) << endl;

        }

    }
}

C++: #include<bits/stdc++.h>
using namespace std;
int tim[10000][750];
int cnt[10000][750];
int main(){
  int N,M;
  while(cin>>N>>M,N+M){
    int r;
    cin>>r;
    memset(tim,0,sizeof(tim));
    for(int i=0;i<r;i++){
      int t,n,m,s;
      cin>>t>>n>>m>>s;
      t-=540,m--;
      tim[m][t+1]+=s?1:-1;
    }
    for(int i=0;i<M;i++){
      int sum=0;
      for(int j=0;j<740;j++){
	tim[i][j+1]+=tim[i][j];
	sum+=!!tim[i][j];
	cnt[i][j]=sum;
      }
    }
    int q;
    cin>>q;
    for(int i=0;i<q;i++){
      int s,e,m;
      cin>>s>>e>>m;
      s-=540,e-=540,m--;
      cout<<cnt[m][e]-cnt[m][s]<<endl;
    }
  }
  return 0;
}

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

int main()
{
	int N, M;
	while (cin >> N >> M && N != 0 || M != 0)
	{
		int r;
		cin >> r;
		static int timeLine[10000 + 1][24 * 60 - 540];
		static int startTime[1000 + 1][10000 + 1];
		for (int i = 0; i < M + 1; i++)
		{
			for (int j = 0; j < 24 * 60 -540;j++)
			{
				timeLine[i][j] = 0;
			}
		}

		for (int i = 0; i < r; i++)
		{
			int t, n, m, s;
			cin >> t >> n >> m >> s;
			if (s == 1)
			{
				startTime[n][m] = t;
			}
			else
			{
				for (int j = startTime[n][m] - 540; j < t- 540; j++)
				{
					timeLine[m][j] = 1;
				}
			}
		}

		int q;
		cin >> q;

		for (int i = 0; i < q; i++)
		{
			int start, end, m;
			cin >> start >> end >> m;
			int ans = 0;
			for (int j = start -540; j < end - 540; j++)
			{
				if (timeLine[m][j] == 1)
				{
					ans++;
				}
			}

			cout << ans << endl;
			
		}
	}
	return 0;
}

C++: #include<iostream>
#define T 1261
using namespace std;main(){int N,M,r,q,t[T],n[T],m[T],s[T],i,c,a;for(;cin>>N>>M>>r,N;){for(i=r;i-->0;)cin>>t[i]>>n[i]>>m[i]>>s[i];for(cin>>q;q-->0;cout<<a<<endl){cin>>N>>M>>c;int P[T]={0};for(i=r;i-->0;)if(m[i]==c)P[t[i]]+=s[i]-1?-1:1;for(a=i=0;++i<T;)P[i]+=P[i-1];for(i=N;i<M;i++)a+=P[i]>0;}}}