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 <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <set>
#include <map>
#include <cstring>
#include <cstdio>
#include <stack>
#define INF 1000000000
#define rep(i,n) for(int i =0;i<n;i++)
using namespace std;
typedef long long int ll;



int main(){
    for(;;){
        int N,M;
        cin >> N>>M;
        if(!N and !M)break;
        int r;
        cin >> r;
        int g[1001][721];
        map<pair<int, int>, int> hoge;
        memset(g, 0, sizeof(g));
        for (int i=0; i<r; i++) {
            int t,n,m,s;
            cin >>t>>n>>m>>s;
            t-=540;
            if (s==1) {
                g[m][t] = 1;
                hoge[{m,n}]=t;
            }else{
                for (int j=hoge[{m,n}];j<t; j++) {
                    g[m][j]=1;
                }
            }
        }
        int q;
        cin >>q;
        for (int qq = 0; qq < q; qq++) {
            int ts,te,m,ans = 0;
            cin >> ts >> te >>m;
            for (int i= ts-540; i<te-540; i++) {
                if (g[m][i] == 1) {
                    ans++;
                }
            }
            cout << ans <<endl;
        }
    }
}

C++: #include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <tuple>
#include <queue>
#include <set>
#include <cstdio>
#include <climits>
#include <cmath>
#include <array>
#include <functional>
#include <sstream>
#include <list>
#include <set>
const int MOD=1000000007;
const int INF=1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const double eps=1e-9;
const int inf=1e9;
typedef pair<int,int> P;
struct Point
{
  double x,y;
  Point(){x=0;y=0;}
  Point(double d_x,double d_y){x=d_x,y=d_y;}
  double operator*(Point obj){return obj.x*x+obj.y*y;}
  double operator%(Point obj){return obj.y*x-obj.x*y;}
  Point operator*(double b){Point tmp;tmp.x=x*b;tmp.y=y*b;return tmp;}
  Point operator/(double b){Point tmp;tmp.x=x/b;tmp.y=y/b;return tmp;}
  Point operator+(Point obj){Point tmp;tmp.x=x+obj.x;tmp.y=y+obj.y;return tmp;}
  Point operator-(){Point tmp;tmp.x=-x;tmp.y=-y;return tmp;}
  Point operator-(Point obj){Point tmp;tmp.x=x-obj.x;tmp.y=y-obj.y;return tmp;}
  Point operator-=(Point obj){x-=obj.x;y-=obj.y;return *this;}
  Point operator+=(Point obj){x+=obj.x;y+=obj.y;return *this;}
  Point operator/=(double b){x=x/b;y=y/b;return *this;}
  Point operator*=(double b){x=x*b;y=y*b;return *this;}
  double size(){return hypot(x,y);}
  Point unit(){return Point(x/size(),y/size());}
  Point normal(){return Point(y,-x);}
  double atan(){return atan2(y,x);}
};

bool operator<(Point a,Point b){return a.x!=b.x?a.x<b.x:a.y<b.y;}
bool operator>(Point a,Point b){return b<a;}
bool operator<=(Point a,Point b){return !(b<a);}
bool operator>=(Point a,Point b){return !(a<b);}
bool operator==(Point a,Point b){return (a-b).size()<eps;}
bool operator!=(Point a,Point b){return !(a==b);}

bool equal(double a,double b){return abs(a-b)<eps;}
double cross(Point a,Point b){return a%b;}
double dot(Point a,Point b){return a*b;}

int ccw(Point a,Point b,Point c)
{
  b=b-a;
  c=c-a;
  if(b%c>0) return +1;
  else if(b%c<0)return -1;
  else if(b*c<0) return +2;
  else if(b.size()<c.size()) return -2;
  else return 0;
}


class history
{
public:
  int pc,start,end;
  history(int p,int s,int e)
  {
    pc=p;start=s;end=e;
  }
};
int main(int argc,char const* argv[])
{
  vector<history> hist[10005];
  int N,M;
  int r,t,n,m,s,q,ts,te;
  int imos[1265];
  while(1)
    {
      cin >> N >> M;
      if(!N) break;
      cin >> r;
      for(int i=0;i<r;i++)
	{
	  cin >> t >> n >> m >> s;
	  if(s==1)
	    {
	      hist[m].push_back(history(n,t,-1));
	    }
	  else
	    {
	      for(int j=0;j<hist[m].size();j++)
		{
		  if(hist[m][j].pc==n && hist[m][j].end==-1)
		    {
		      hist[m][j].end=t;
		      break;
		    }
		}
	    }
	}
      cin >> q;
      for(int i=0;i<q;i++)
	{
	  cin >> ts >> te >> m;
	  for(int j=0;j<1265;j++) imos[j]=0;
	  for(int j=0;j<hist[m].size();j++)
	    {
	      if(hist[m][j].start<te && hist[m][j].end > ts)
		{
		  imos[max(hist[m][j].start,ts)]+=1;
		  imos[min(hist[m][j].end,te)]-=1;
		}
	    }
	  int ans=0,cnt=0,start;
	  for(int j=0;j<1265;j++)
	    {
	      if(cnt==0 && imos[j] > 0)
		{
		  start=j;
		}
	      if(cnt>0 && cnt+imos[j]==0)
		{
		  ans+=(j-start);
		}
	      cnt+=imos[j];
	    }
	  cout << ans << endl;
	}
    }
  return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<functional>
#include<map>
#include<set>
#include<math.h>

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

using namespace std;

int n,m;

//時刻,学生
int data[1300][10003];

void solve() {
	rep(i,1300) rep(j,10003) data[i][j]=0;
	int r;
	cin>>r;
	rep(i,r) {
		int a,b,c,d;
		cin>>a>>b>>c>>d;
		if(d==1) {
			data[a][c]=1;
		}
		else {
			data[a][c]=-1;
		}
	}

	rep(i,m+1) {
		for(int j=1;j<1300;j++) {
			data[j][i]+=data[j-1][i];
		}
	}

	int q;
	cin>>q;
	rep(i,q) {
		int a,b,c;
		cin>>a>>b>>c;
		int ans=0;
		for(int j=a;j<b;j++) {
			if(data[j][c]!=0) {
				ans++;
				//printf("[debug] %d,%d = %d\n",j,c,data[j][c]);
			}
		}
		//cout<<"ans="<<ans<<endl;
		cout<<ans<<endl;
	}

}

int main() {

	while (cin>>n>>m)
	{
		if(n+m==0) {
			return 0;
		}
		solve();
	}

	return 0;
}