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 <map>
using namespace std;

int solve(vector<multimap<int,bool> > &record,int ts,int te,int a){
  int d=0,sum=0,start=0;
  for(multimap<int,bool>::iterator it=record[a-1].begin();it!=record[a-1].end();++it){
    if(!d&&it->second)start=it->first;
    if(it->second)++d;
    else --d;
    if(!d){
      int st,en;
      st=(ts>start)?ts:start;
      en=(te<it->first)?te:it->first;
      sum+=en-st;
    }
  }
  return sum;
}

int main(void){
  int n,m;
  while(cin>>n>>m,n||m){
    int r,q;
    cin>>r;
    vector<multimap<int,bool> > record(m);
    for(int i=0,t,a,b,s;i<r;++i){
      cin>>t>>a>>b>>s;
      record[b-1].insert(make_pair(t,((s)?true:false)));
    }
    cin>>q;
    for(int i=0,ts,te,a;i<q;++i){
      cin>>ts>>te>>a;
      cout<<solve(record,ts,te,a)<<endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>

using namespace std;

const int MAX = 1300;
struct Info{int t,n,m,s;};
typedef pair<int,int> P;
vector<Info> V;
bool used[MAX];
int N,M;

int solve(int ts, int te, int m){

  int ans = 0;

  vector<P> login;
  vector<P> logout;

  for(int i = 0; i < V.size(); i++){
    if(V[i].m == m && V[i].s == 1)
      login.push_back(P(V[i].n,V[i].t));

    if(V[i].m == m && V[i].s == 0)
      logout.push_back(P(V[i].n,V[i].t));
  }

  sort(login.begin(),login.end());
  sort(logout.begin(),logout.end());
  
  for(int i = 0; i < login.size(); i++){
    int ss = max(ts,login[i].second);
    int ee = min(te,logout[i].second);
    //    fill(used+ss,used+ee,true);
    for(int i = ss; i < ee; i++)
      used[i] = true;

  }

  for(int i = ts; i < te; i++)
    if(used[i]) ans++;

  return ans;
}

void init(){
  V.clear();
  memset(used,false,sizeof(used));
}

int main(){

  while(cin >> N >> M && N+M){
    init();
    int r;
    cin >> r;
    while(r--){
      Info f;
      cin >> f.t >> f.n >> f.m >> f.s;
      V.push_back(f);
    }

    int q;
    cin >> q;
    while(q--){
      memset(used,false,sizeof(used));
      int ts,te,m;
      cin >> ts >> te >> m;
      cout << solve(ts,te,m) << endl;
    }

  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int main()
{
	int N,M;
	while(cin>>N>>M,N) 
	{
		int r;cin>>r;
		int t[r],m[r],s[r];
		int n;

		for(int i = 0; i < r; ++i)
		{
			cin>>t[i]>>n>>m[i]>>s[i];
		}

		int q; cin>>q;

		for(int i = 0; i < q; ++i)
		{
			int ts,te,sn;
			cin>>ts>>te>>sn;
			int ans=0,cnt=0,prev=0;
			for(int j = 0; j < r; ++j)
			{
				if(m[j]==sn)
				{
					if(s[j]==1)
					{
						cnt++;
						if(cnt==1)
						{
							if(t[j]<ts) prev=ts;
							else prev=t[j];
						}
					}
					else
					{
						cnt--;
						if(cnt==0)
						{
							if(t[j]<=te) ans+=t[j]-prev;
							else ans+=te-prev;
						}
					}
				}
			}
			//if(cnt!=0) ans+=te-prev;
			cout<<ans<<endl;
		}
	}
  return 0;
}

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

int n[1001][723]={};
int main(){

int N,M;
while(1){
cin>>N>>M;if(N==0&&M==0)break;
int r;
cin>>r;

for(int i=0;i<M;i++)
for(int j=0;j<722;j++)
n[i][j]=0;

for(int i=0;i<r;i++)
{int t,x,m,w;
cin>>t>>x>>m>>w;
t-=540;
if(w==1)n[m][t]+=1;
else{n[m][t]-=1;}
}
for(int i=0;i<=M;i++)
for(int j=0;j<721;j++)
n[i][j+1]+=n[i][j];


int q,t1,t2,y;
cin>>q;


for(int i=0;i<q;i++){
cin>>t1>>t2>>y;
t1-=540;t2-=540;
int c=0;
for(int j=t1;j<t2;j++)
{
	if(n[y][j]!=0){c++;}//cout<<j+540<<" "<<c<<n[y][j]<<endl;}
}
cout<<c<<endl;
}
}
return 0;
}

C++: #include <cstdio>
#include <cassert>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <array>
#include <memory.h>

using namespace std;

#define all(c) begin(c), end(c)
#define dump(c) cerr << "> " << #c << " = " << (c) << endl
#define REP(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int INF = 1001001001;
const double EPS = 1e-10;


template<typename T1,typename T2>
ostream& operator<<(ostream& os, const pair<T1,T2>& p){
  return os << '(' << p.first <<',' << p.second << ')';
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& a){
  os << '[';
  rep(i,a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

int cnt[10010], st[10010], acc[10010][1300];

int main() {
  for(int N, M; cin >> N >> M, (N | M) != 0; ) {
    memset(cnt, 0, sizeof(cnt));
    memset(st, 0, sizeof(st));
    memset(acc, 0, sizeof(acc));
    int r; cin >> r;
    rep(i, r) {
      int t, n, m, s; cin >> t >> n >> m >> s;
      if (s) {
        if(cnt[m] == 0) st[m] = t;
        cnt[m]++;
      }
      else {
        cnt[m]--;
        if(cnt[m] == 0) {
          REP(j, st[m], t) acc[m][j] = 1;
        }
      }
    }

    int q; cin >> q;
    rep(i, q) {
      int s, e, m; cin >> s >> e >> m;
      int cnt = 0;
      REP(i, s, e) cnt += acc[m][i];
      cout << cnt << endl;
    }
  }
  
  
  return 0;
}