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 <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <numeric>

using namespace std;
using ll = long long;

int main()
{
    while (true) {
        int N, M;
        cin >> N >> M;
        if (N == 0 and M == 0) {
            break;
        }
        int r;
        cin >> r;
        constexpr int TIM = 720;
        vector<vector<int>> use(N, vector<int>(TIM + 1, 0));
        for (int i = 0; i < r; i++) {
            int t, n, m, s;
            cin >> t >> n >> m >> s;
            n--;
            t -= 540;
            if (s == 1) {
                use[n][t] = m;
            } else {
                for (int T = t - 1; T >= 0; T--) {
                    if (use[n][T] != m) {
                        use[n][T] = m;
                    } else {
                        break;
                    }
                }
            }
        }
        int q;
        cin >> q;
        for (int i = 0; i < q; i++) {
            int ts, te, m;
            cin >> ts >> te >> m;
            ts -= 540;
            te -= 540;
            int sum = 0;
            for (int t = ts; t < te; t++) {
                bool u = false;
                for (int j = 0; j < N; j++) {
                    if (use[j][t] == m) {
                        u = true;
                        break;
                    }
                }
                if (u) {
                    sum++;
                }
            }
            cout << sum << endl;
        }
    }

    return 0;
}

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



int main(){
	while(true){
		vector<int> curlog;
		vector< list< pair<int,int> > > log;
		int N,M,r,q;
		cin>>N>>M>>r;
		if(N==0)break;
		curlog.resize(N);
		log.resize(M);
		for(int i=0;i<r;i++){
			int t,n,m,s;
			cin>>t>>n>>m>>s;
			if(s){
				curlog[n-1]=t;
			}else{
				log[m-1].push_back(pair<int,int>(curlog[n-1],t));
			}
		}
		cin>>q;
		for(int i=0;i<q;i++){
			int s,e,m,a=0;
			list< pair<int,int> > logcop;
			cin>>s>>e>>m;
			logcop=log[m-1];
			logcop.sort();
			for(list< pair<int,int> >::iterator ita=logcop.begin();ita!=logcop.end();++ita){
				int start=(*ita).first,last=(*ita).second;
				list< pair<int,int> >::iterator itb=ita;
				++itb;
				for(;itb!=logcop.end();){
					if((*itb).first>start&&(*itb).first<last){
						if((*itb).second>last)last=(*itb).second;
						itb=logcop.erase(itb);
					}else{
						++itb;
					}
				}
				if(start<s)start=s;
				if(last>e)last=e;
				a+=last-start;
			}
			cout<<a<<endl;
		}
	}
	return 0;
}

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

using namespace std;

typedef pair<int, int> time_log;

struct pc_log{
	int m;
	time_log log;
};

typedef struct pc_log pc_log;

int main() {
	int N, M;
	while(cin >> N >> M && N && M) {
		int r; cin >> r;
		vector<vector<pc_log> > log(N);

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

			if(s) { // login
				pc_log tmp;
				tmp.m = m;
				tmp.log.first = t;
				tmp.log.second = -1;

				log[n-1].push_back(tmp);
			}
			else { // logout
				log[n-1][log[n-1].size()-1].log.second = t;
			}
		}

		int q; cin >> q;

		for(int i = 0; i < q; i++) {
			int ts, te, m; cin >> ts >> te >> m;

			vector<time_log> st_log;
			for(int j = 0; j < N; j++) {
				for(int k = 0; k < log[j].size(); k++) {
					if(log[j][k].m == m) {
						st_log.push_back(log[j][k].log);
					}
				}
			}
			sort(st_log.begin(), st_log.end());

			int res = 0;
			int cur = ts;

			for(int j = 0; j < st_log.size(); j++) {
				while(ts > st_log[j].second) j++;
				if(!j) {
					if(ts < st_log[j].first) {
						res += (st_log[j].second - max(cur, st_log[j].first));
					}
					cur = st_log[j].second;
				}
				else {
					if(cur < st_log[j].first) {
						res += (st_log[j].second - st_log[j].first);
						cur = st_log[j].second;
					}
					else { // cur >= st_log[j].first
						if(cur < st_log[j].second) {
							res += (st_log[j].second - cur);
							cur = st_log[j].second;
						}
					}
				}
			}
			if (cur > te) {
				res -= cur - te;
			}
			cout << res << endl;
		}
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;++i)
using namespace std;
int main(){
int u,r,q,a,b,c;
while(cin>>r,r){cin>>r>>r;
vector<int>t(r),m(r),s(r);
r(i,r)cin>>t[i]>>m[i]>>m[i]>>s[i];cin>>q;
r(i,q){cin>>a>>b>>c;
int an=0,cn=0,p=a;
r(i,t.size()){u=min(t[i],b);
if(cnt)an+=max(u-p,0);
if(m[i]==c){if(s[i])cn++;else cn--;}
p=max(p,u);}
cout<<an<<endl;}}}