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>

using namespace std;

class Record {
    public:
        int t, n, m, s;
};

int compute_time(int s, int e, vector<Record> records) {
    int count = 0;
    int time = 0;
    int table[1261];
    for (int i=0; i<=1260; i++) {
        table[i] = 0;
    }
    for (int i=0; i<(int)records.size(); i++) {
        table[records[i].t] += 2*records[i].s-1;
    }
    for (int i=0; i<=1260; i++) {
        count += table[i];
        if (count > 0 && s <= i && i < e) {
            time++;
        }
    }
    return time;
}

int main() {
    int n, m, r, q;
    while (true) {
        vector<Record> records[10001];
        cin >> n >> m;
        if (n == 0) {
            return 0;
        }
        for (int i=0; i<=10000; i++) {
            records[i].clear();
        }
        cin >> r;
        for (int i=0; i<r; i++) {
            Record rec;
            cin >> rec.t >> rec.n >> rec.m >> rec.s;
            records[rec.m].push_back(rec);
        }
        cin >> q;
        for (int i=0; i<q; i++) {
            int s, e, m;
            cin >> s >> e >> m;
            cout << compute_time(s, e, records[m]) << endl;
        }
    }
}

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

const int MAX_M = 10001;
const int MAX_N = 1001;
struct LOG{
	int m;
	int in;
	int out;
};

bool comp(LOG l1, LOG l2){
	return l1.in < l2.in;
}

int main(){
	int N, M;
	while(cin >> N >> M){
		if(N == 0 && M == 0) break;
		vector<LOG> vl[MAX_M];
		stack<LOG> sl[MAX_N];
		int r, q;
		cin >> r;
		for(int i = 0; i <r; i++){
			int t, n, m, s;
			cin >> t >> n >> m >> s;
			LOG l;
			if(s){
				l.in = t;
				sl[n].push(l);
			}else{
				l = sl[n].top();
				sl[n].pop();
				l.out = t;
				vl[m].push_back(l);
			}
		}
		cin >> q;
		for(int i = 0; i < q; i++){
			int ts, te, m;
			cin >> ts >> te >> m;
			int time = 0;
			sort(vl[m].begin(), vl[m].end(), comp);
			for(vector<LOG>::iterator iter = vl[m].begin();
				iter != vl[m].end(); iter++)
			{
				int ti = (*iter).in;
				int to = (*iter).out;
				if(ti >= te) break;
				if(ts >= to) continue;
				if(ts >= ti && te <= to){
					time += te - ts;
					break;
				}else if(ts >= ti && te >= to){
					time += to - ts;
					ts = to;
				}else if(ts <= ti && te <= to){
					time += te - ti;
					break;
				}else{
					time += to - ti;
					ts = to;
				}
				if(ts >= te) break;
			}
			cout << time << endl;
		}
	}
	cout << endl;
}

C++: #include<stdio.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <string>

using namespace std;

int n, m, r, q, t[10000][1300];

int main() {

	
	while (1) {

		for (int i = 0; i < 10000; i++) {
			for (int j = 0; j < 1300; j++) {
				t[i][j] = 0;
			}
		}
		cout << "fin1" << endl;

		cin >> n >> m;
		if (n==0 && m==0) break;

		cin >> r;
		for (int i = 0; i < r; i++) {
				int tt, tn, tm, ts;
				cin >> tt >> tn >> tm >> ts;
				if (ts == 1) t[tm-1][tt]++;
				else t[tm - 1][tt+1]--;
		}
		cout << "fin2" << endl;

		for (int i = 0; i < m; i++) {
			for (int j = 1; j <1300; j++) {
				//if(750<j && j<800) cout << "t[i][j-1]" << t[i][j - 1] << endl;
				t[i][j] += t[i][j-1];
			}
		}

		cin >> q;
		for (int i = 0; i < q; i++) {
			int ts, te, tm;
			cin >> ts >> te >> tm;
			int ans=0;
			bool flag = false;
			for (int j = ts; j < te; j++) {
				if (t[tm - 1][j]>0) ans++, flag = true;
				if (flag && t[tm - 1][j]==0) ans--,flag = false;
			}
			cout << ans << endl;
		}
	}

	return 0;
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

typedef array<int, 2> timecard;

int analyze(int ts, int te, vector<timecard> &history) {
  int sum = 0;

  for (auto x : history) {
    if (ts < x[0]) {
      if (te <= x[0]) {
        break;
      } else {
        ts = x[0];
      }
    }
    // x[0] <= ts
    if (x[1] <= ts) {
      continue;
    }

    // x[0] <= ts < x[1], x[0] < te
    if (te <= x[1]) {
      sum += te - ts;
      break;
    } else {
      sum += x[1] - ts;
    }
  }

  return sum;
}

int main() {
  constexpr int LOGIN = 1;
  constexpr int LOGOUT = 0;
  int N, M, r, q, t, n, m, s, ts, te;

  while (cin >> N >> M && N != 0) {
    vector<vector<timecard>> user_histories(M + 1);
    vector<vector<int>> buffer(M + 1);
    cin >> r;
    rep(_, r) {
      cin >> t >> n >> m >> s;
      if (s == LOGIN) {
        buffer[m].push_back(t);
      } else {
        auto _ts = buffer[m].back();
        buffer[m].pop_back();
        if (buffer[m].size() == 0) {
          user_histories[m].push_back(timecard{_ts, t});
        }
      }
    }

    cin >> q;
    rep(_, q) {
      cin >> ts >> te >> m;
      cout << analyze(ts, te, user_histories[m]) << endl;
    }
  }
}