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 <bits/stdc++.h>
using namespace std;

#define dump(x) cout << (x) << '\n'
#define Int int64_t
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()

Int INF = 1e18;
int inf = 1e9;
Int mod = 1e9+7;

int main() {
    vector<int> res;
    while (true) {
        int n, m;
        cin >> n >> m;
        if (n == 0) break;
        int r;
        cin >> r;
        vector<pair<int, int>> v[1001];
        vector<int> a(r), b(r), c(r), d(r);
        for (int i = 0; i < r; i++) {
            cin >> a[i] >> b[i] >> c[i] >> d[i];
            v[b[i]].emplace_back(a[i], c[i]);
        }
        int q;
        cin >> q;
        for (int i = 0; i < q; i++) {
            int a, b, c;
            cin >> a >> b >> c;
            vector<bool> time(60 * 24, false);
            vector<int> used;
            for (int j = 1; j <= n; j++) {
                for (int k = 0; k < (int)v[j].size(); k++) {
                    if (v[j][k].se == c) {
                        used.push_back(v[j][k].fi);
                    }
                }
            }
            for (int j = 0; j < (int)used.size() - 1; j+=2) {
                for (int k = used[j]; k < used[j + 1]; k++) {
                    time[k] = true;
                }                
            }
            int cnt = 0;
            for (int j = a; j < b; j++) {
                if (time[j]) {
                    cnt++;
                }
            }
            res.push_back(cnt);
        }
    }
    for (int i = 0; i < (int)res.size(); i++) {
        dump(res[i]);
    }
    return 0;
}

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

bool use[10000][1260];
int pc[1000];

int main(){
    int n, m, r, q;
    while(cin >> n >> m, n+m){
        memset(use, 0, sizeof(use));
        memset(pc, 0, sizeof(pc));

        cin >> r;
        int a, b, c, d;
        for(int i = 0; i < r; i++){
            cin >> a >> b >> c >> d;
            b--, c--;
            if(d == 1){
                pc[b] = a;
            }else if(d == 0){
                for(int i = pc[b]; i < a; i++){
                    use[c][i] = true;
                }
                pc[b] = 0;
            }
        }

        cin >> q;
        int ts, te, x;
        for(int i = 0; i < q; i++){
            cin >> ts >> te >> x;
            x--;
            int cnt = 0;
            for(int j = ts; j < te; j++)    if(use[x][j])   cnt++;
            cout << cnt << endl;
        }
    }

    return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;


int main (){
  
  int pc,stu;
  
  while(1){

    cin >>pc>>stu;
    if (pc==0&&stu==0)break;
    
    int i,j,k,srec,sque,t,n,m,s,ts,te,sum;
    int record[pc][721];
    
    for(i=0;i<pc;i++){
      for(j=0;j<721;j++){
	record[i][j]=0;
      }
    }

    //record入力
    cin >>srec;
    for(i=0;i<srec;i++){
      cin >>t>>n>>m>>s;
      
      if (s==1){
	//ログイン
	for(j=t-540;j<721;j++){
	  record[n][j] = m;
	}
      }else{
	//ログアウト
	for(j=t+1-540;j<721;j++){
	  record[n][j] = 0;
	}
      }
    }
    
    //記録チェック
    sum = 0;
    cin >> sque;
    for(i=0;i<sque;i++){
      cin >> ts >>te >>m;
      for(j=ts-540;j<te+1-540;j++){
	for(k=0;k<pc;k++){
	  if (record[k][j]==m){
	    sum += 1;
	    break;
	  }
	}
      }
    }
    
    cout << sum <<endl;
  }
    
  return 0;
}

C++: #include<bits/stdc++.h>
#define fi first
#define se second
#define show(x) cerr<<#x<<"="<<x<<"\n"
typedef long long ll;
using namespace std;
//const ll MOD=(ll)1e9+7;
//const ll inf=(ll)1e14;
const int dy[]={1,0,-1};
const int dx[]={1,0,-1};
vector<pair<int,int> > v[10001];
int vv[10001][1000];

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout<<fixed;
#ifdef LOCAL_DEFINE
  FILE *stream1;
  //FILE *stream2;
  stream1=freopen("in","r",stdin);
  //stream2=freopen("out","w",stdout);
  if(stream1==NULL)return 0;
  //if(stream2==NULL)return 0;
#endif
  int n,m;
  while(cin>>n>>m,n){
    int r,q;cin>>r;
    vector<pair<int,int> > v[10001];
    for(int i=0;i<r;i++){
      int a,b,c,d;cin>>a>>b>>c>>d;
      c--;
      a-=540;
      if(d==0){
        v[c].push_back({b,a});
      }else{
        v[c].push_back({b,a});
      }
    }
    for(int i=0;i<m;i++){
      for(int j=0;j<800;j++){
        vv[i][j]=0;
      }
    }
    for(int i=0;i<m;i++){
      sort(v[i].begin(),v[i].end());
      for(int j=0;j<(int)v[i].size();j+=2){
        vv[i][v[i][j].se]++;
        vv[i][v[i][j+1].se]--;
      }
    }
    for(int i=0;i<m;i++){
      for(int j=0;j<800;j++){
        if(j==0)continue;
        vv[i][j]=vv[i][j-1]+vv[i][j];
      }
    }
    cin>>q;
    for(int i=0;i<q;i++){
      int ts,te,tm;cin>>ts>>te>>tm;
      tm--;
      ts-=540;
      te-=540;
      int res=0;
      for(int j=ts;j<te;j++){
        if(vv[tm][j]>0)res++;
      }
      cout<<res<<endl;
    }
  }
#ifdef LOCAL_DEFINE
  cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
  fclose(stream1);
  //fclose(stream2);
#endif
  return 0;
}