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 <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };

struct UnionFind {
	vector<int> v;
	UnionFind(int n) : v(n)  { for (int i = 0; i < n; i++) v[i] = i; }
	int  find(int x)         { return v[x] == x ? x : v[x] = find(v[x]); }
	void unite(int x, int y) { v[find(x)] = find(y); }
};

int main() {
	int n, m, r, q;
	while (cin >> n >> m,n|m) {
		cin >> r;
		vector<int> stu(m, 0);
		vector<vector<pii>> stutime(m);
		REP(i, r) {
			int a, b, c, d;
			cin >> a >> b >> c >> d;
			c--;
			if (d) {
				if (stu[c] == 0)
					stutime[c].push_back(pii(a,0));
				stu[c]++;
			}
			else {
				stu[c]--;
				if (stu[c] == 0)
					stutime[c][stutime[c].size() - 1].second = a;
			}
		}
		cin >> q;
		REP(i, q) {
			int a, b, c;
			cin >> a >> b >> c;
			c--;
			int ans = 0;
			REP(j, stutime[c].size()) {
				if (stutime[c][j].first <= b && a <= stutime[c][j].second) {
					ans += min(b, stutime[c][j].second) - max(a, stutime[c][j].first);
				}
			}
			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[10005][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]>0&&j!=x) count--;
	if(a[z][j]!=0) count++;
	if(j==x&&a[z][j]>0) count--;
      }
      cout<<count<<endl;
    }
    lp(i,m+1){
      lp(j,2000){
	a[i][j]=0;
      }
    }
  }
  return 0;
}

C++: #include<iostream>

using namespace std;

#define MMAX 100000

int main(){

	int N, M;
	int r;
	int t, n, m1, s;
	int q;
	int ts, te, m2;
	int num,cnt;
	int data[MMAX+100][1400];

	while (1){
		cin >> N >> M;
		if (N == 0 && M == 0) break;
		for (int i = 1; i <= M; i++){
			for (int j = 0; j < 1400; j++){
				data[i][j] = 0;
			}
		}
		cin >> r;
		for (int i = 0; i < r; i++){
			cin >> t >> n >> m1 >> s;
			if (s == 0) data[m1][t]--;
			else data[m1][t]++;
		}
		cin >> q;
		for (int i = 1; i <= M; i++){
			num = 0;
			for (int j = 0; j < 1400; j++){
				num += data[i][j];
				data[i][j] = num;
			}
		}
		for (int i = 0; i < q; i++){
			cin >> ts >> te >> m2;
			cnt = 0;
			for (int j = ts; j < te; j++){
				if (data[m2][j] >= 1) cnt++;
			}
			cout << cnt << endl;
		}
	}
	return 0;
}

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

class Point{
public:
  int t,n,m,g;
  bool f;
  Point(int t = 0,int n = 0,int m = 0,int g = 0):t(t),n(n),m(m),g(g){}
  bool operator < (const Point &p)const {
    return t < p.t;
  }
};

int main(){
  int i,j,k,n,m,r,q,c;
  Point p[1000];
  while(1){
    cin >> n >> m;
    if(n == 0 && m == 0) break;

    cin >> r;
    c = 0;
    int x1,x2,x3,x4;
    for(i=0;i<r;i++){
      cin >> x1 >> x2 >> x3 >> x4;
      if(x4 == 1){
	p[c].t = x1;
	p[c].n = x2;
	p[c].m = x3;
	p[c].f = false;
	c++;
      }
      else{
	for(j=c-1;j>=0;j--){
	  if(x2 == p[j].n && x3 == p[j].m && !p[j].f){
	    p[j].f = true;
	    p[j].g = x1;
	    break;
	  }
	}
      }
    }

    sort(p,p+c);

    cin >> q;
    int y1,y2,y3,ans,totyu;
    for(i=0;i<q;i++){
      cin >> y1 >> y2 >> y3;
      ans = 0;
      totyu = y1;
      for(j=0;j<c;j++){
	if(p[j].m == y3 && p[j].g > totyu && p[j].g <= y2){
	  if(p[j].t > totyu) ans += p[j].g - p[j].t;
	  else ans += p[j].g - totyu;
	  totyu = p[j].g;
	}
	else if(p[j].m == y3 && y2 > totyu && p[j].g > y2 && y2 > p[j].t){
	  if(p[j].t > totyu) ans += y2 - p[j].t;
	  else ans += y2 - totyu;
	  totyu = y2;
	}
      }
      cout << ans << endl;
    }


  }
  return 0;
}