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 <algorithm>
#include <vector>
#include <cstdio>
using namespace std;

int main() {

  int N,M,r,q;
  vector <int> t,n,m,s;
 

  while(1) {
    
    cin >> N >> M;
    if(N == 0 && M == 0 ) break;
    
    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];
    }

    vector <int> s2;
    cin >> q;
    int ts,te,st;
    for(int Q=0;Q<q;Q++) {
    
      cin >> ts >> te >> st;

      s2.clear();
      s2.resize(N+1);
      for(int i=0;i<=N;i++) s2[i] = 0;

      int count = 0, time = t[0]-5;
      int start=0;
      
      while(1) {
	time++;
	if(time == te) break;
	
	if(t[start] == time){
	  start++;
	  
	  if(m[start-1] == st) {
	    s2[n[start-1]] = s[start-1];
	    //  printf("time:%d comp%d = %d\n",time,n[start-1],s[start-1]);
	    
	  }
	
	  if(t[start-1] == t[start]) {
	    //  cout <<"continue"<< endl;
	    time--;
	    continue;
	  } 
	}
	
	if(ts <= time) {
	  // cout << time <<" " << count<< endl;
	  int sum = 0;
	  for(int i=1;i<=N;i++) sum += s2[i];
	  // cout << "sum = "<<sum << endl;
	  if(sum != 0) count++;
	}
	
	
      }

      cout << count << endl;

    }

    t.clear();
    m.clear();
    s.clear();
    n.clear();
  
  }
  
  return 0;

}

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

#define max(a,b) (a > b ? a : b)
#define min(a,b) (a < b ? a : b)

typedef struct {
	int m, n, s, d;
} T;

typedef struct {
	int s, d;
} P;

int N, M, r, q;
vector<P> a[10001];
T t[1000];

int closs(T &tm) {
	for (int i = 0; i < a[tm.m].size(); i++) {
		if ((tm.d >= a[tm.m][i].s && tm.d <= a[tm.m][i].d) || (tm.s >= a[tm.m][i].s && tm.s <= a[tm.m][i].d) || (tm.s <= a[tm.m][i].s && tm.d >= a[tm.m][i].d)) {
			return i;
		}
	}
	return -1;
}

int main(){
	int _t, _n, _m, _s, _d;
	int ite, tmp, ans;
	while (cin >> N >> M && N | M != 0) {
		for (int i = 0; i <= M; i++) {
			a[i] = vector<P>();
		}
		ite = 0;
		cin >> r;
		for (int I = 0; I < r; I++) {
			cin >> _t >> _n >> _m >> _s;
			if (_s == 1) {
				t[ite++] = { _m,_n,_t,0 };
			}
			else {
				for (int i = ite - 1; i >= 0; i--) {
					if (t[i].m == _m && t[i].n == _n) {
						t[i].d = _t;
						tmp = closs(t[i]);
						if (tmp == -1) {
							a[_m].push_back({ t[i].s,t[i].d });
						}
						else {
							a[_m][tmp].d = max(a[_m][tmp].d, t[i].d);
							a[_m][tmp].s = min(a[_m][tmp].s, t[i].s);
						}
						break;
					}
				}
			}
		}
		cin >> q;
		for (int I = 0; I < q; I++) {
			cin >> _s >> _d >> _m;
			ans = 0;
			for (int i = 0; i < a[_m].size(); i++) {
				if (a[_m][i].s >= _s) {
					if (a[_m][i].d >= _d) {
						ans += _d - a[_m][i].s;
					}
					else {
						ans += a[_m][i].d - a[_m][i].s;
					}
				}
				else if (a[_m][i].d <= _d) {
					ans += a[_m][i].d - _s;
				}
				else {
					ans += _d - _s;
				}
			}
			cout << ans << endl;
		}
	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int N,M;
  while(cin>>N>>M,N){
    int r,t,n,m,s;
    vector<int> d[10001];
    cin>>r;
    for(int i=0;i<r;i++){
      cin>>t>>n>>m>>s;
      if(!s)s=-1;
      d[m].push_back(s*t);
    }
    int q,ts,te,a;
    cin>>q;
    for(int i=0;i<q;i++){
      int ans=0,k=0;
      cin>>ts>>te>>a;
      for(int j=0;j<d[a].size();j++){
	if(!k&&ts<d[a][j])ts=d[a][j];
	if(d[a][j]>0)k++;
	else k--;
	if(!k)ans+=max(0,min(te,-d[a][j])-ts);
      }
      cout<<ans<<endl;
    }
  }
  return 0;
}

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

#define REP(i, n) for(int i = 0; i < (int) (n); i++)
#define ALL(v) (v).begin(), (v).end()
#define INF 1e9

using namespace std;

int use[10000][750];

int main(){
  int n, m;
  while(cin >> n >> m and n + m){
    memset(use, 0, sizeof(use));
    vector<int> logs[m];
    int machine[m] = {0};
    int r; cin >> r;
    REP(i, r){
      int tr, nr, mr, sr; cin >> tr >> nr >> mr >> sr;
      mr--;
      if(sr){
        if(machine[mr] == 0) logs[mr].push_back(tr);
        machine[mr]++;
      }else{
        machine[mr]--;
        if(machine[mr] == 0) logs[mr].push_back(tr);
      }
    }
    REP(i, m) REP(j, logs[i].size()){
      int from = logs[i][j] - 540;
      int to = logs[i][++j] - 540;
      for(int k = from+1; k < to+1; k++) use[i][k] = 1;
    }
    REP(i, m) REP(j, 749) use[i][j+1] += use[i][j];
    int q; cin >> q;
    REP(i, q){
      int ts, te, mq;
      cin >> ts >> te >> mq;
      mq--;
      cout << use[mq][te-540] - use[mq][ts-540] << endl;
    }
  }
  return 0;
}