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 <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it = (c).begin(); it != (c).end(); it++)
#define X first
#define Y second

using namespace std;

typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
const int T = 721;
const int DEL = 540;
const int MM = 10001;
const int NN = 1001;

bool t_t[MM][T];

int main(void){
    for(int N, M; cin >> N >> M, N;){
        memset(t_t, false, MM * T);
     
        map<pii, int> dic;
        int r; cin >> r;
        while(r--){
            int t, n, m, s; cin >> t >> n >> m >> s;
            t -= DEL;

            if(!s)
                for(int i = dic[mp(m, n)]; i < t; i++)
                    t_t[m][i] = true;
            else dic[mp(m, n)] = t; 
        }   
        
        int q; cin >> q;
        while(q--){
            int ts, te, m; cin >> ts >> te >> m;
            ts -= DEL, te -= DEL;

            cout << count(t_t[m] + ts, t_t[m] + te, true) << endl;
        }
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long ll;


int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	while(1){
		int n,m; cin >> n >> m;
		if(n==0&&m==0)break;
		vector<vector<int>> login(m,vector<int>(1300,0));
		int r; cin >> r;
		while(r--){
			int a,b,c,d; cin >> a >> b >> c >> d;
			c--;
			if(d==1){
				login[c][a+1]++;
			}else{
				login[c][a+1]--;
			}
		}
		for(int i=0;i<m;i++){
			for(int j=1;j<1300;j++){
				login[i][j]+=login[i][j-1];
			}
		}
		int query; cin >> query;
		while(query--){
			int a,b,c; cin >> a >> b >> c;
			c--;
			int res=0;
			for(int i=a+1;i<=b;i++){
				if(login[c][i])res++;
			}
			printf("%d\n",res );
		}
	}
}

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

int main(){
	int n,m,r,q;
	static int cnt[10000][1261];
	while(cin >> n >> m,n){
		for(int i = 0;i < m;i++){
			for(int j = 0;j <= 1260;j++) cnt[i][j] = 0;
		}
		cin >> r;
		for(int i = 0;i < r;i++){
			int t,a,b,s;
			cin >> t >> a >> b >> s; b--;
			if(s) cnt[b][t]++;
			else cnt[b][t]--;
		}
		for(int i = 0;i < m;i++){
			for(int j = 1;j <= 1260;j++){
				cnt[i][j] += cnt[i][j - 1];
				//if(j >= 774 && j <= 881) cout << cnt[i][j] << " ";
			}
			//cout << endl;
		}
		for(int i = 0;i < m;i++){
			for(int j = 1;j <= 1260;j++){
				if(cnt[i][j]) cnt[i][j] = 1;
			}
		}
		for(int i = 0;i < m;i++){
			for(int j = 1;j <= 1260;j++){
				cnt[i][j] += cnt[i][j - 1];
			}
		}
		cin >> q;
		for(int i = 0;i < q;i++){
			int s,t,b;
			cin >> s >> t >> b; b--;
			cout << cnt[b][t - 1] - cnt[b][s - 1] << endl;
		}
	}
	return 0;
}

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

using namespace std;

int N, M, r, q;
bool use[1261] = {false};

class Log {
private:
    int t_;
    int n_;
    int m_;
    int s_;
public:
    Log(int t, int n, int m, int s) :
        t_(t), n_(n), m_(m), s_(s) {}

    int t() {
        return t_;
    }

    int n() {
        return n_;
    }

    int m() {
        return m_;
    }

    int s() {
        return s_;
    }
};

class Question {
private:
    int ts_;
    int te_;
    int m_;
public:
    Question(int ts, int te, int m) :
        ts_(ts), te_(te), m_(m) {}
};

int solve(vector<Log>& logs, int ts, int te, int m) {
    int ans = 0;
    vector<int> login;
    vector<int> logout;

    for (int i = 0; i < 1261; ++i) {
        use[i] = false;
    }

    for (int i = 1; i < N + 1; ++i) {
        for (auto& e : logs) {
            if (e.m() == m && e.s() == 0 && e.n() == i) {
                logout.push_back(e.t());
            }
            else if (e.m() == m && e.s() == 1 && e.n() == i) {
                login.push_back(e.t());
            }
        }
    }

    const int size = login.size();

    // for (int i = 0; i < size; ++i) {
    //     cout<<login[i]<<" - "<<logout[i]<<endl;
    // }
    

    for (int i = 0; i < size; ++i) {
        for (int j = login[i]; j < logout[i]; ++j) {
            use[j] = true;
        }
    }

    for (int time = 540; time <= 1260; ++time) {
        if  (use[time] && ts <= time && time < te) {
            ans++;
            //cout<<"time : "<<time<<", ans : "<<ans<<endl;
        }
    }

    return ans;
}

int main() {
    int t, n, m, s;
    int ts, te;
    vector<Log> log;

    while(cin>>N>>M, N||M) {
        cin>>r;
        for (int i = 0; i < r; ++i) {
            cin>>t>>n>>m>>s;
            log.push_back(Log(t, n, m, s));
        }

        cin>>q;
        for (int i = 0; i < q; ++i) {
            cin>>ts>>te>>m;
            cout<<solve(log, ts, te, m)<<endl;
        }
        
        log.clear();
    }

    return 0;
}