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>
using namespace std;
 
int main() {
    int n,m,r,q,t,nn,mm,s,ts,te;
    int si[10001]={};
    int ei[10001]={};
    int pcn[10001]={};
    int lts[1000]={};
    int lte[1000]={};
    int ni[1000]={};
    while(1){
        cin >> n >> m;
        if(n==0){return 0;}
        for(int i=1;i<=m;i++){
            si[i]=-1;
            pcn[i]=0;
        }
        cin >> r;
        int pi=0;
        for(int i=0;i<r;i++){
            cin >> t >> nn >> mm >> s;
            if(mm==3212){cout << t << " " << s << endl;}
            if(s){
                pcn[mm]++;
                if(pcn[mm]==1){
                    if(si[mm]==-1){
                        si[mm]=pi;
                    }else{
                        ni[ei[mm]]=pi;
                    }
                        ei[mm]=pi;
                        lts[pi]=t;
                        ni[pi]=-1;
                        pi++;
                }
            }else{
                pcn[mm]--;
                if(pcn[mm]==0){
                    lte[ei[mm]]=t;
                }
            }
        }
        cin >> q;
        for(int i=0;i<q;i++){
            cin >> ts >> te >> mm;
            int tt=0;
            for(int j=si[mm];j!=-1;j=ni[j]){
                if(lts[j]<ts && lte[j]>=ts && lte[j]< te){
                    tt=lte[j]-ts;
                }
                if(lts[j]<ts && lte[j]>=te){
                    tt=te-ts;
                    break;
                }
                if(lts[j]>=ts && lte[j]<te){
                    tt+=lte[j]-lts[j];
                }
                if(lts[j]>=ts && lts[j]<te && lte[j]>=te){
                    tt+=te-lts[j];
                    break;
                }
                if(lts[j]>=te){
                    break;
                }
            }
            cout << tt << endl;
        }
    }
}

C++: #include <iostream>
#include <vector>
#include <array>
using namespace std;

int main() {
    int N, M;
    while(cin >> N >> M, N) {
        int r; cin >> r;
        vector<int> pc(N, -1);
        vector<array<short, 1261>> v(M);
        for(int i=0; i<r; ++i) { // O(rt)
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            if(s == 1) {
                pc[n-1] = t;
            } else {
                fill(v[m-1].begin() + pc[n-1], v[m-1].begin()+t, 1);
                pc[n-1] = -1;
            }
        }

        for(int i=0; i<v.size(); ++i) {
            for(int j=1; j<v[i].size(); ++j) {
                v[i][j] += v[i][j-1];
            }
        }
        int q;
        cin >> q;
        for(int i=0; i<q; ++i) {
            int ts, te, m;
            cin >> ts >> te >> m;
            cout << v[m-1][te-1] - v[m-1][ts-1] << endl;
        }
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,n1,n2) for(int i=n1;i<n2;i++)
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef long long int ll;
typedef pair<ll,ll> Pi;
const int INF=(ll)(1LL<<31)-1;
const ll INFl=(ll)9223372036854775807;
const int MAX=10000;
const ll MOD=(ll)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a, b)*b;}
//int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int N,M;
int r,q;
int a[1010],b[1010],c[1010],d[1010];

int main(){
	while(1){
		cin>>N>>M;
		if(N==0&&M==0)break;
		cin>>r;
		REP(i,r){
			cin>>a[i]>>b[i]>>c[i]>>d[i];
		}
		
		cin>>q;
		for(int i=0;i<q;i++){
			int ans=0;
			int ts,te,m;
			cin>>ts>>te>>m;
			for(int sec=ts;sec<te;sec++){
				int login=0;
				for(int j=0;j<r;j++){
					if(c[j]==m){
						if(a[j]<=sec){
							if(d[j]==0)login--;
							else login++;
						}
					}
				}
				if(login)ans++;
			}
			cout<<ans<<endl;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
const long double PI = acos(-1);
typedef long long ll;
typedef pair<int64_t,int64_t> pll;
int dx[]={1,-1,0,0,1,-1,1,-1};
int dy[]={0,0,1,-1,1,-1,-1,1};
#define rep(i,a,b) for (int64_t i = (a); i < (b); i++)
#define REP(i,n) rep(i,0,n)
#define ALL(a) begin(a),end(a)
#define sz(s) (s).size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second

int main()
{
    int N,M;
    while(cin>>N>>M,N)
    {
        vector<int >c(M,0);
        vector<vector<pair<int,int> > >student(M);
        int r;cin>>r;
        REP(i,r)
        {
            int t,n,m,s;
            cin>>t>>n>>m>>s;n--;m--;
            if(s)
            {
                if(c[m]==0)student[m].pb(mp(t,t));
                c[m]++;
            }else
            {
                c[m]--;
                if(c[m]==0)student[m].back().se=t;
            }
        }
        int q;cin>>q;
        REP(i,q)
        {
            int ts,te,m;
            cin>>ts>>te>>m;m--;
            int ans=0;
            REP(j,student[m].size())
            {
                ans+=max(0,min(te,student[m][j].se)-max(ts,student[m][j].fi));
            }
            cout<<ans<<endl;
        }
    }
}