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 <map>
#include <set>
#include <list>
#include <cstdio>
#include <cmath>
#include <deque>
#include <stack>
#include <bitset>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

#define fr first
#define sc second

typedef pair< int, int > iP;

const int INF = 2 << 28;
const double EPS = 1e-10;

int main() {
    
    int N, M;
    while(cin >> N >> M, N) {
        vector< vector< iP > > pc(N + 1);
        int r; cin >> r;
        while(r--) {
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            pc[n].push_back(iP(t, m));
        }
        int q; cin >> q;
        while(q--) {
            int ans = 0;
            int begin, end, m; cin >> begin >> end >> m;
            vector< bool > used(1270, true);
            for(int i = 1; i <= N; i++) {
                for(int j = 0; j < pc[i].size(); j += 2) {
                    if(pc[i][j].sc == m) {
                        for(int k = max(begin, pc[i][j].fr); k < min(pc[i][j +  1].fr, end); k++) {
                            if(used[k]) ans++, used[k] = false;
                        }
                    }
                }
            }
            cout << ans << endl;
        }
    }
}

C++: #include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

vector<pair<int,int> > ary[10001];

int main(){ _;
  int N,M,t,n,m,s,r,q,ts,te;
  while(cin>>N>>M,(N|M)!=0){
    cin>>r;
    REP(i,r){
      cin>>t>>n>>m>>s;
      ary[m].push_back(make_pair(t,s==1?1:-1));
    }
    cin>>q;
    REP(i,q){
      cin>>ts>>te>>m;
      map<int,int> a;
      for(auto p:ary[m]){
	if(p.first<ts)
	  a[ts]+=p.second;
	else if(p.first>te)
	  a[te]+=p.second;
	else
	  a[p.first]+=p.second;
      }
      int sum=0,start=0,login=0;
      for(auto aa:a){
	if(login==0&&aa.second>0){
	  start=aa.first;
	}
	login+=aa.second;
	if(login==0&&aa.second<0){
	  sum+=aa.first-start;
	}
      }
      cout<<sum<<endl;
    }
    REP(i,M+1){
      ary[i].clear();
    }
  }
}

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

using namespace std;

int main(){
	while(true){
		int N, M;
		cin >> N >> M;
		if(N == 0 && M == 0){ break; }
		int r;
		cin >> r;
		vector< vector<int> > logs(M + 1, vector<int>(1261));
		for(int i = 0; i < r; ++i){
			int t, n, m, s;
			cin >> t >> n >> m >> s;
			logs[m][t] += (s > 0) ? 1 : -1;
		}
		int q;
		cin >> q;
		for(int i = 0; i < q; ++i){
			int ts, te, m;
			cin >> ts >> te >> m;
			vector<int> integral(logs[m]);
			for(int j = 0, s = 0; j < integral.size(); ++j){
				s += integral[j];
				integral[j] = s;
			}
			int answer = 0;
			for(int j = ts; j < te; ++j){
				if(integral[j] > 0){ ++answer; }
			}
			cout << answer << endl;
		}
	}
	return 0;
}

C++: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    while(true){
        int N,M;
        cin>>N>>M;
        if(N==0)break;
        int r;
        cin>>r;
        vector<int> t(r),n(r),m(r),s(r);
        for(int i=0;i<r;i++){
            cin>>t[i]>>n[i]>>m[i]>>s[i];
        }
        int q;
        cin>>q;
        vector<int> ts(q),te(q),qm(q);
        for(int i=0;i<q;i++){
            cin>>ts[i]>>te[i]>>qm[i];
        }
        for(int i=0;i<q;i++){
            int sum=0;
            int start=0;
            int PCnum=0;
            for(int j=0;j<r;j++){
                if(m[j]==qm[i]){
                    if(s[j]==1){
                        PCnum++;
                        if(PCnum==1)
                            start=t[j];
                    }else{
                        PCnum--;
                        if(PCnum==0)
                            sum+=max(0,min(t[j],te[i])-max(ts[i],start));
                    }
                }
            }
            cout<<sum<<endl;
        }
    }
}

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

int main(){
    int n,m,r,q;
    while(cin>>n>>m,n){
        cin>>r;
        vector<vector<int>> usert(m);
        vector<int> userh(m,0);
        for(int i=0;i<r;i++){
            int t,n,m,s;
            cin>>t>>n>>m>>s;
            m--;
            if(s==1){
                if(userh[m]==0)
                    usert[m].push_back(t);
                userh[m]++;

            }
            if(s==0){
                if(userh[m]==1)
                    usert[m].push_back(t);
                userh[m]--;

            }


        }
        cin>>q;
        for(int i=0;i<q;i++){
            int ts,te,m;
            cin>>ts>>te>>m;
            m--;
            int cnt=0;
            bool isusin=0;
            for(int i=0;i<usert[m].size();i++){
                if(!isusin){
                    isusin=1;
                }
                else{
                    isusin=0;

                    int span=min(usert[m][i],te)-max(usert[m][i-1],ts);
                    if(span>=0)
                        cnt+=span;
                }
            }
            cout<<cnt<<endl;
        }





    }
}