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>

using namespace std;

int main(){
	int n, m;
	while(cin >> n >> m){
		if(!n) break;
		cin >> n;
		vector <int> count(m, 0);
		vector < vector< pair<int,int> > > rec(m);
		int t, p, s;
		for(int i=0;i<n;i++){
			cin >> t >> p >> p >> s;
			p--;
			if(s == 1){
				if(count[p] == 0)
					rec[p].push_back(make_pair(t,t));
				count[p]++;
			} else {
				count[p]--;
				if(count[p] == 0)
					rec[p][rec[p].size()-1].second = t;
			}
		}
		cin >> n;
		for(int i=0;i<n;i++){
			int ans = 0;
			cin >> s >> t >> p;
			p--;
			for(int j=0;j<rec[p].size();j++){
				ans += max(0, min(t, rec[p][j].second) - max(s, rec[p][j].first));
			}
			cout << ans << endl;
		}
	}
}

C++: #include<iostream>
#include<cassert>
#include<ctime>
#include<climits>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include<list>
#include<sstream>
#include<string>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<stack>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define EPS 1e-10
#define F first
#define S second
#define all(n) n.begin(),n.end()
#define pb push_back
#define insert(a,b,c,d) PP(P(a,b),P(c,d))
#define INF (1<<27)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<P,P> PP;
const int dx[4] = {0,1,0,-1};
const int dy[4] = {1,0,-1,0};
int N,M,r,q;
vector<P> info[10001];
//map<int,bool> Index;
 
int main(){
 
  while(true){
    cin >> N >> M;
    if(N+M == 0)break;
    rep(i,10000)
      info[i].clear();
    //Index.clear();
    stack<char> staq[M+1];
 
    cin >> r;
    rep(i,r){
      int t,n,m,s;
      cin >> t >> n >> m >> s;
      if(s == 1){
    if(staq[m].empty()){
      info[m].pb(P(t,-1));
      //Index[m] = true;
      
    }
    staq[m].push('a');
      }
      else{
    //if(index[m]){
    assert(info[m].size()-1 >= 0);
    info[m][info[m].size()-1].S = max(info[m][info[m].size()-1].S,t);
    staq[m].pop();
    //Index[m] = false;
    //}
      }
    }
    //cout << "deg --------" << endl;
 
    //rep(i,M){
    //cout << "strudent is " << i+1 << endl;
    //rep(j,info[i+1].size()){
    //cout << info[i+1][j].F << " "  << info[i+1][j].S << endl;
    //}
    //}
    //cout << "M = " << M << endl;
    cin >> q;
    rep(i,q){
      int ts,te,m,total;
      cin >> ts >> te >> m;
      total = 0;
      //assert(!Index[m]);
      assert(staq[m].empty());
      //cout << "info[" << i << "].size = " << info[m].size() << endl;
      rep(j,info[m].size()){
 
    if(ts <= info[m][j].F && info[m][j].S <= te){
      total += info[m][j].S - info[m][j].F;
    }
    else if(info[m][j].F <= ts && te <= info[m][j].S ){
      total += te - ts;
    }
    else if(info[m][j].F <= ts && ts <= info[m][j].S && info[m][j].S <= te){
      total += info[m][j].S - ts;
    }
    else if(ts <= info[m][j].F && info[m][j].F <= te && te <= info[m][j].S){
      total += te - info[m][j].F;
    }
    else if((te < info[m][j].F) || (ts > info[m][j].S)){
      //  cout << "MISS!" << endl;
    }
    else assert(false);
      }
 
      cout << total << endl;
    }
 
 
  }
 
return 0;
}

C++: #include <cstdio>

#pragma warning(disable: 4996)

#define MAX_M 10000

int N, M, R, Q, t1, n1, m1, s1, t2, m2, n2, s2, s, e, m;

int record[MAX_M + 1][1260];

int main()
{
	while (true)
	{
		scanf("%d", &N);
		scanf("%d", &M);

		if (N == 0 && M == 0) { break; }

		scanf("%d", &R);

		for (int i = 0; i < (R / 2); i++)
		{
			scanf("%d", &t1);
			scanf("%d", &n1);
			scanf("%d", &m1);
			scanf("%d", &s1);
			scanf("%d", &t2);
			scanf("%d", &n2);
			scanf("%d", &m2);
			scanf("%d", &s2);

			for (int j = t1; j < t2; j++)
			{
				record[m1][j] = 1;
			}
		}

		scanf("%d", &Q);

		for (int i = 0; i < Q; i++)
		{
			scanf("%d", &s);
			scanf("%d", &e);
			scanf("%d", &m);

			int count = 0;

			for (int j = s; j < e; j++)
			{
				count += record[m][j];
			}

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

		for (int i = 1; i <= M; i++)
		{
			for (int j = 540; j < 1260; j++)
			{
				record[i][j] = 0;
			}
		}
	}

	return 0;
}

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

#define T 1261
#define R 1001

int main()
{
  int N,M,r,q,t[R],n[R],m[R],s[R],i,a,b,c,ans;
  while(cin>>N>>M,N|M){
    cin>>r;
    for(i=0;i<r;i++)cin>>t[i]>>n[i]>>m[i]>>s[i];
    cin>>q;
    while(q-->0){
      cin>>a>>b>>c;
      int time[T]={0};
      for(i=0;i<r;i++){
	if(m[i]!=c)continue;
	time[t[i]] += s[i]==1?1:-1;
      }
      for(i=1;i<T;i++)time[i]+=time[i-1];
      ans=0;
      for(i=a;i<b;i++)ans += time[i]>0;
      cout<<ans<<endl;
    }
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=0;i<n;i++)
#define fordebug int hoge;cin>>hoge;
int a[10000][2000];
int main(){
  while(1){
    int n,m;
    cin>>n>>m;
    if(n==0&&m==0) break;
    int r;
    cin>>r;
    lp(i,r){
      int t,nn,mm,s;
      cin>>t>>nn>>mm>>s;
      if(s==0) a[mm][t+1]=-1; 
      else a[mm][t]=1;
    }
    lp(i,m+1){
      for(int j=1;j<2000;j++){
	a[i][j]+=a[i][j-1];
      }
    }
    int q;
    cin>>q;
    lp(i,q){
      int x,y,z;
      cin>>x>>y>>z;
      int count=0;
      for(int j=x;j<=y;j++){
	if(a[z][j-1]==0&&a[z][j]==1&&j!=x) count--;
	if(a[z][j]!=0) count++;
	if(j==x&&a[z][j]==1) count--;
      }
      cout<<count<<endl;
    }
    lp(i,m+1){
      lp(j,2000){
	a[i][j]=0;
      }
    }
  }
  return 0;
}