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<cstdio>
#include<stack>
#include<vector>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef pair<int,int> P;
stack<P> stu[10003];
vector<P> vec[10003];
int N,M,r;
int t[1003],n[1003],m[1003],s[1003];
int q;
int qs[55],qe[55],qm[55];

int cookies(int sx,int sy,int lx,int ly){
  return max(min(sy,ly)-max(sx,lx),0);
}

int main(){
  while(1){
    memset(t,0,sizeof(t));
    memset(n,0,sizeof(n));
    memset(m,0,sizeof(m));
    memset(s,0,sizeof(s));
    memset(qs,0,sizeof(qs));
    memset(qe,0,sizeof(qe));
    memset(qm,0,sizeof(qm));
    for(int i=0;i<10003;i++)vec[i].resize(0);
    for(int i=0;i<10003;i++){
      while(1){
        if(!stu[i].empty()) stu[i].pop();
        else break;
      }}

    scanf("%d%d",&N,&M);
    if(N==0 && M==0) break;
    scanf("%d",&r);
    for(int i=0;i<r;i++){
      scanf("%d%d%d%d",&t[i],&n[i],&m[i],&s[i]);
      if(s[i]==1) stu[m[i]].push(P(t[i],n[i]));
      if(s[i]==0) {
        P p=stu[m[i]].top();
        stu[m[i]].pop();
        if(stu[m[i]].empty()) vec[m[i]].push_back(P(p.first,t[i]));
      }
    }
    scanf("%d",&q);
    int i;
    for(i=0;i<q;i++) {
      scanf("%d%d%d",&qs[i],&qe[i],&qm[i]);

      int res=0;
      for(int j=0;j<vec[qm[i]].size();j++){
        res+=cookies(qs[i],qe[i],vec[qm[i]][j].first,vec[qm[i]][j].second);
      }

      printf("%d\n",res);
    }

  }
  return 0;
}

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

#define rep(i,x,y) for(int (i)=(x);(i)<(y);(i)++)

int main()
{
  int N,M,r,q;

  while(true){
    cin>>N>>M;
    if(N==0&&M==0){
      break;
    }
    vector<vector<int>> map(M,vector<int>(1260-540+1,0));

    cin>>r;

    rep(i,0,r){
      int t,n,m,s;
      cin>>t>>n>>m>>s;
      if(s==1){
        map[m-1][t-540]++;
      }else{
        map[m-1][t-540]--;
      }
    }

    rep(i,0,M){
      rep(j,0,1260-540){
        map[i][j+1]+=map[i][j];
      }
    }

    cin>>q;

    rep(i,0,q){
      int ts,te,num;
      cin>>ts>>te>>num;
      int count=0;
      rep(j,ts-540,te-540){
        if(map[num-1][j]>=1){
          count++;
        }
      }
      cout<<count<<endl;
    }
  }
  return 0;
}

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

int main() {
    while(true) {
        vector<vector<int>> log;
        int N, M; cin >> N >> M;
        if (N == 0 && M == 0) break;
        for (int i = 0; i < M; i++) {
            log.push_back(vector<int>(721));
        }
        int r; cin >> r;
        for (int i = 0; i < r; i++) {
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            m--;
            if (s == 0) log[m][t - 540]--;
            else        log[m][t - 540]++;
        }
        for (int m = 0; m < M; m++) {
            for(int t = 1; t <= 720; t++) {
                log[m][t] += log[m][t-1];
            }
        }
        int q; cin >> q;
        for (int i = 0; i < q; i++) {
            int ans = 0;
            int s, e, m; cin >> s >> e >> m;
            m--;
            for(int t = s; t < e; t++) {
                if (log[m][t - 540] > 0) ans++;
            }
            cout << ans << endl;
        }
    }
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair

#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)

const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

int main(){
    int n, m;
    while(cin >> n >> m, n+m){
        int r;
        cin >> r;
        vector<vector<int>> li, lo;
        rep(i, r){
            vector<int> v(4);
            cin >> v[2] >> v[0] >> v[1] >> v[3];
            if(v[3] == 1) li.push_back(v);
            else  lo.push_back(v);
        }
        sort(li.begin(), li.end());
        sort(lo.begin(), lo.end());
        int o;
        cin >> o;
        rep(i, o){
            int s, e, m;
            cin >> s >> e >> m;
            bool b[2000];
            fill(b, b+2000, false);
            for(int i=0; i< li.size(); i++){
                if(li[i][1] == m){
                    for(int j=li[i][2]; j <= lo[i][2]; j++) b[j] = true;
                }
            }
            int ret = 0;
            for(int i=s; i <= e; i++){
                if(b[i]) ret++;
                if(b[i]&&(!b[i+1] || i == e))ret--;
            }
            cout << ret << endl;
        }
    }
}