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

int main()
{
  int N,M,r,q,t,n,m,s,S[10000][720],ts,te,time;

  while(1){
    cin>>N>>M;
    if(N==0 && M==0)break;
    for(int i=0;i<M;i++){
      for(int j=0;j<720;j++)S[i][j]=0;
    }
    cin>>r;
    for(int i=0;i<r;i++){
      cin>>t>>n>>m>>s;
      if(s==1)S[m-1][t-540]=1;
      else S[m-1][t-540]=-1;
    }
    for(int i=0;i<M;i++){
      for(int j=1;j<720;j++){
        S[i][j]=S[i][j]+S[i][j-1];
      }
    }
    cin>>q;
    for(int i=0;i<q;i++){
      cin>>ts>>te>>m;
      time=0;
      for(int j=ts-540;j<te-540;j++){
	if(S[m-1][j]>0)time++;
      }
      cout<<time<<endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#include<cassert>
#include<ctime>
#include<climits>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include<list>
#include<sstream>
#include<string>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<stack>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define EPS 1e-10
#define F first
#define S second
#define all(n) n.begin(),n.end()
#define pb push_back
#define insert(a,b,c,d) PP(P(a,b),P(c,d))
#define INF (1<<27)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<P,P> PP;
const int dx[4] = {0,1,0,-1};
const int dy[4] = {1,0,-1,0};
int N,M,r,q;
vector<P> info[10001];
//map<int,bool> Index;

int main(){

  while(true){
    cin >> N >> M;
    if(N+M == 0)break;
    rep(i,10000)
      info[i].clear();
    //Index.clear();
    stack<char> staq[M+1];

    cin >> r;
    rep(i,r){
      int t,n,m,s;
      cin >> t >> n >> m >> s;
      if(s == 1){
	if(staq[m].empty()){
	  info[m].pb(P(t,-1));
	  //Index[m] = true;
	 
	}
	staq[m].push('a');
      }
      else{
	//if(index[m]){
	assert(info[m].size()-1 >= 0);
	info[m][info[m].size()-1].S = max(info[m][info[m].size()-1].S,t);
	staq[m].pop();
	//Index[m] = false;
	//}
      }
    }
    //cout << "deg --------" << endl;

    //rep(i,M){
    //cout << "strudent is " << i+1 << endl;
    //rep(j,info[i+1].size()){
    //cout << info[i+1][j].F << " "  << info[i+1][j].S << endl;
    //}
    //}
    //cout << "M = " << M << endl;
    cin >> q;
    rep(i,q){
      int ts,te,m,total;
      cin >> ts >> te >> m;
      total = 0;
      //assert(!Index[m]);
      assert(staq[m].empty());
      //cout << "info[" << i << "].size = " << info[m].size() << endl;
      rep(j,info[m].size()){

	if(ts <= info[m][j].F && info[m][j].S <= te){
	  total += info[m][j].S - info[m][j].F;
	}
	else if(info[m][j].F <= ts && te <= info[m][j].S ){
	  total += te - ts;
	}
	else if(info[m][j].F <= ts && ts <= info[m][j].S && info[m][j].S <= te){
	  total += info[m][j].S - ts;
	}
	else if(ts <= info[m][j].F && info[m][j].F <= te && te <= info[m][j].S){
	  total += te - info[m][j].F;
	}
	else if((te < info[m][j].F) || (ts > info[m][j].S)){
	  //  cout << "MISS!" << endl;
	}
	else assert(false);
      }

      cout << total << endl;
    }


  }

return 0;
}

C++: #include <bits/stdc++.h>
#define INF 1e18
#define int long long
#define Rep(i, a, n) for (int i = (a); i < (n); i++)
#define Repr(i, n, a) for (int i = (n); i >= (a); i--)
#define rep(i, n) Rep(i, 0, n)
#define repr(i, n) Repr(i, n, 0)
#define all(a) a.begin(), a.end()
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;

int a[1010][1310];

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, m;
    while (1)
    {
        cin >> n >> m;
        if (n == 0 && m == 0)
            break;
        rep(i, 1010) rep(j, 1310) a[i][j] = 0;
        int r;
        cin >> r;
        rep(i, r)
        {
            int T, N, M, S;
            cin >> T >> N >> M >> S;
            int x = (S == 1 ? 1 : -1);
            T++;
            a[M - 1][T] += x;
        }
        rep(i, m) rep(j, 1300) a[i][j + 1] += a[i][j];
        rep(i, m) rep(j, 1300) if (a[i][j] > 1) a[i][j] = 1;
        rep(i, m) rep(j, 1300) a[i][j + 1] += a[i][j];
        int q;
        cin >> q;
        while (q--)
        {
            int A, B, C;
            cin >> A >> B >> C;
            C--;
            cout << a[C][B] - a[C][A] << endl;
        }
    }
}