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 <utility>
#include <cstring>
using namespace std;

const int MAX_TIME = 721;
const int DIFF = 540;

struct Computer {
    vector<pair<int, int> > login;
    vector<pair<int, int> > logout;
};

struct Student {
    bool isUsing[MAX_TIME];
};

int main() {
    while (true) {
        int n, m;
        cin >> n >> m;
        if ((n | m) == 0) {
            break;
        }
        int r;
        cin >> r;
        vector<Computer> computers(n + 1);
        for (int i = 0; i < r; i++) {
            int t, pc, user, s;
            cin >> t >> pc >> user >> s;
            t -= DIFF;
            if (s) {
                computers[pc].login.push_back(make_pair(user, t));
            } else {
                computers[pc].logout.push_back(make_pair(user, t));
            }
        }
        vector<Student> students(m + 1);
        for (int i = 1; i <= m; i++) {
            memset(students[i].isUsing, 0, sizeof(students[i].isUsing));
            for (int j = 1; j <= n; j++) {
                for (int k = 0, size = computers[j].login.size(); k < size; k++) {
                    if (computers[j].login[k].first == i) {
                        fill(students[i].isUsing + computers[j].login[k].second, students[i].isUsing + computers[j].logout[k].second, true);
                    }
                }
            }
        }
        int q;
        cin >> q;
        for (int i = 0; i < q; i++) {
            int start, end, user;
            cin >> start >> end >> user;
            start -= DIFF;
            end -= DIFF;
            int time = 0;
            for (int j = start; j < end; j++) {
                if (students[user].isUsing[j]) {
                    time++;
                }
            }
            cout << time << endl;
        }
    }
}

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

const int b = 540;
int main(){
    while(1){
        int pc,stu;
        cin >> pc >> stu;
        if(pc==0) break;
        
        int r;
        cin >> r;
        vector<vector<int> > log(stu+1, vector<int>(730, 0));
        for(int i=0; i<r; i++){
            int t,n,m,s;
            cin >> t >> n >> m >> s;
            log[m][t-b] += s*2-1;
        }
        for(int i=1; i<=stu; i++){
            for(int j=1; j<730; j++){
                log[i][j] += log[i][j-1];
            }
        }
        
        int q;
        cin >> q;
        for(int i=0; i<q; i++){
            int s,e,who;
            cin >> s >> e >> who;
            int count=0;
            for(int j=s-b; j<e-b; j++){
                if(log[who][j]>0) count++;
            }
            cout << count << endl;
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int main(){
	int n,p;
	while(cin>>n>>p,n){
		int q,a[p][1261-540],ch[p][1261-540],b,c,d,e;
		r(i,0,p)r(j,0,1261-540)ch[i][j]=a[i][j]=0;
		cin>>q;
		r(i,0,q){
			cin>>b>>c>>d>>e;
			b-=540;
			if(e==1){a[d-1][b]++;ch[d-1][b]=c;}
			else{
				for(int j=b-1;j>=0;j--){
					if(ch[d-1][j]==c)break;
					a[d-1][j]++;
				}
			}
		}
		cin>>q;
		/*while(q--){
			int s=0;
			cin>>b>>c>>d;
			b-=540;
			c-=540;
			r(i,b,c)if(a[d-1][i])s++;
			cout<<s<<endl;
		}*/
	}
}

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

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

struct usedata{
	int in;
	int out;
	int pc;
};

int main(){
	int n, m;
	while(1){
		cin >> n >> m;
		if(n == 0 || m == 0) break;

		int r, q;
		cin >> r;

		vector<vector<usedata> > student(m);
		vector<vector<usedata> >::iterator stuite;
		vector<usedata>::iterator dataite;
		for(int i = 0; i < r; i++){
			int t, s, n1, m1;
			cin >> t >> n1 >> m1 >> s;
			//stuite = student.begin() + (m1-1);
			stuite = student.begin() + (m1-1);
			dataite = (*stuite).end() -1;
			if(s){
				usedata u;
				u.in = t;
				u.pc = n1;
				(*stuite).push_back(u);
			}else{
				while(1){
					if((*dataite).pc == n1){
						(*dataite).out = t;
						break;
					}else{
						dataite--;
					}
				}
			}
		}

		cin >> q;
		for(int i = 0; i < q; i++){
			int ts, te, m2;
			cin >> ts >> te >> m2;
			int qt = te - ts;
			vector<int> usetime(qt);
			vector<int>::iterator usetimeite = usetime.begin();
			while(usetimeite != usetime.end()){
				*usetimeite = 0;
				usetimeite++;
			}
			stuite = student.begin() + (m2-1);
			dataite = (*stuite).begin();
			while(dataite != (*stuite).end()){
				if(ts < (*dataite).out && (*dataite).in < te){
					int in1 = (*dataite).in;
					int out1 = (*dataite).out;
					if(ts > in1) in1 = ts;
					if(te < out1) out1 = te;
					in1 -= ts;
					out1 -= ts;
					usetimeite = usetime.begin() + in1;
					int j = 0;
					while(j < out1 - in1){
						*usetimeite = 1;
						*usetimeite++;
						j++;
					}

				}
				dataite++;
			}

			int ans = 0;
			usetimeite = usetime.begin();
			while(usetimeite != usetime.end()){
				if(*usetimeite) ans++;
				//cout << *usetimeite;
				usetimeite++;
			}

			cout << ans << endl;

		}
	}

	return 0;
}