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>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
struct S{
	int t,n,m,s;
	S(int t,int n,int m,int s):t(t),n(n),m(m),s(s){}
};
int main(){
	int r,q,t,n,m,s,ans;
	while(cin>>r>>q,r){
		vector<S> lg; cin>>r;
		rep(i,r)cin>>t>>n>>m>>s,lg.push_back(S(t,n,m,s));
		cin>>q; rep(i,q){
			cin>>s>>t>>m; n=ans=0;
			rep(i,r-1){
				n+=lg[i].m==m&&lg[i].s;
				if(n>0)ans+=max(min(lg[i+1].t,t)-max(lg[i].t,s),0);
				n-=lg[i+1].m==m&&!lg[i+1].s;
			}
			cout<<ans<<endl;
		}
	}
	return 0;
}

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

int main(){

    //??\???
    while(1){
        int N, M; cin >> N >> M;
        if(N == 0) break;
    

        int r; cin >> r;
        vector<vector<int> > imos(M + 1, vector<int> (1000, 0));
        for(int i = 0; i < r; i++){
            int t, n, m, s; cin >> t >> n >> m >> s;
            t -= 500;
            if(s == 1) imos[m][t]++;
            else imos[m][t]--;
        }

        for(int i = 1; i <= M; i++){
            for(int j = 0; j < 1000; j++){
                imos[i][j+1] += imos[i][j];
            }
        }

        int q; cin >> q;
        for(int i = 0; i < q; i++){
            int s, e, m; cin >> s >> e >> m;
            s -= 500;
            e -= 500;
            int ans = 0;
            for(int j = s; j < e; j++){
                if(imos[m][j] > 0) ans++;
            }

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

}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <queue>
#include <complex>
 
#define INF 100000000
#define EPS 1e-9
 
using namespace std;
 
typedef long long ll;
typedef pair<int, int> P;

#define MAX_N 1001
#define MAX_M 10001
#define MAX_R 1001
#define MAX_Q 50

int N, M;
int r;
int q;

const int GETA = 540;

vector<int> ans_list;

int main(){

  while(true){

    cin >> N >> M;
    if(N == 0)
      break;

    int L[MAX_M];
    int PC[MAX_N][1000];
    memset(PC,0,sizeof(PC));

    cin >> r;

    for(int i = 0; i < r; i++){
      int t,n,m,s;
      cin >> t >> n >> m >> s;
      t -= GETA;
      if(s == 1)
	L[n] = t;
      else{
	for(int i = L[n]; i < t; i++){
	  PC[n][i] = m;
	}
      }
    }

    cin >> q;
    for(int i = 0; i < q; i++){
      int ts, te, m;
      cin >> ts >> te >> m;
      int ans = 0;
      ts -= GETA;
      te -= GETA;
      //cout << "******  " << i << endl;
      for(int t = ts; t < te; t++){
	for(int j = 1; j <= N; j++){
	  if(PC[j][t] == m){
	    //cout << j << " " << t << endl;
	    ans++;
	    break;
	  }
	}
      }
    
      ans_list.push_back(ans);
    }

  }

  for(int i = 0; i < ans_list.size(); i++){
    cout << ans_list[i] << endl;
  }
  
  
  
  return 0;
  
}

C++: #include <stdio.h>
#include <vector>
#include <utility>

#define rep(i, n) for(int i=0; i<(int)(n); i++)
using namespace std;

int main() {
	int N, M;
	for(;;) {
		scanf("%d%d", &N, &M);
		if(N==0 && M==0) return 0;
		vector<vector<pair<int, int> > > h(M);
		int r;
		scanf("%d", &r);
		rep(i, r) {
			int t, n, m, s;
			scanf("%d%d%d%d", &t, &n, &m, &s);
			h[m-1].push_back(make_pair(t, s));
		}
		int q;
		scanf("%d", &q);
		rep(i, q) {
			int t1, t2, m;
			scanf("%d%d%d", &t1, &t2, &m);
			int l=0;
			int ts=-1;
			int t=0;
			rep(j, h[m-1].size()) {
				if(h[m-1][j].first>t2) {
					if(l) t += t2-ts;
					break;
				}
				if(h[m-1][j].second) {
					if(l==0) ts=max(t1, h[m-1][j].first);
					l++;
				}
				else {
					l--;
					if(l==0) {
						if(h[m-1][j].first>ts) t += h[m-1][j].first-ts;
					}
				}
			}
			printf("%d\n", t);
		}
	}
}

C++: // AOJ 1148

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef pair <int, int> P;
int data[1500][1500];

int main(void)
{
	int N, M;
	while (cin >> N >> M, N && M){
		for (int i = 0; i < 1500; i++){
			for (int j = 0; j < 1500; j++){
				data[i][j] = 0;
			}
		}

		int r;
		int t, n, m, s;
		cin >> r;
		for (int i = 0; i < r; i++){
			cin >> t >> n >> m >> s;
			for (int j = t; j < 1500; j++){
				data[n][j] = (s == 1) ? m : 0;
			}
		}

		int q;
		int ts, te;
		cin >> q;
		for (int i = 0; i < q; i++){
			cin >> ts >> te >> m;
			int ret = 0;
			for (int j = ts; j < te; j++){
				for (int i = 0; i <= N; i++){
					if (data[i][j] == m){
						ret++;
						break;
					}
				}
			}
			cout << ret << endl;
		}
	}

	return 0;
}