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>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int sum[10000][1280];
int main() {
	int N, M, r;
	while (scanf("%d%d", &N, &M), N) {
		scanf("%d", &r);
		memset(sum, 0, sizeof(sum));
		rep(i, r) {
			int t, n, m, s; scanf("%d%d%d%d", &t, &n, &m, &s);
			if (s)sum[m - 1][t]++;
			else sum[m - 1][t]--;
		}
		rep(i, M) {
			for (int j = 540; j <= 1260; j++)
				sum[i][j] += sum[i][j - 1];
		}
		int q; scanf("%d", &q);
		rep(i, q) {
			int ts, te, m; scanf("%d%d%d", &ts, &te, &m);
			int cnt = 0;
			for (int j = ts; j < te; j++) {
				if (sum[m - 1][j])cnt++;
			}
			printf("%d\n", cnt);
		}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
 
#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG false
 
typedef long long ll;
typedef pair<int, int> ii;

int main() {
	
	ll N, M;
	while(cin >> N >> M, N, M){
		vector<vector<int>> use(M);
		vector<int> cnt(M);
		int r;
		cin >> r;
		rep(i, r){
			int t, n, m, s;
			cin >> t >> n >> m >> s; m--;
			if(s == 0){//log out
				cnt[m]--;
				if(cnt[m] == 0) use[m].push_back(t);
				
			}else{//log in
				if(cnt[m] == 0) use[m].push_back(t);
				cnt[m]++;
			}
		}
		int q;
		cin >> q;
		rep(i, q){
			int ts, te, m;
			cin >> ts >> te >> m; m--;
			int ans = 0;
			for(int j = 0; j < use[m].size(); j += 2){
				if(ts <= use[m][j+1] && use[m][j] <= te) ans += (min(te, use[m][j+1]) -  max(ts, use[m][j]));
			}
			cout << ans << endl;
		}
		
	}
	
	
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <utility>
#include <stdlib.h>

using namespace std;

int main(void)
{
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) break;
    int r;
    cin >> r;
    int time[r], pc[r], stu[r], log[r];
    

    for (size_t i = 0; i < r; i++) {
      cin >> time[i] >> pc[i] >> stu[i] >> log[i];   
    }
    int que;
    cin >> que;
    int ts[que], te[que], qm[que];
    for (size_t i = 0; i < que; i++) {
      cin >> ts[i] >> te[i] >> qm[i];
    }
    //??\??????

    for (size_t k = 0; k < que; k++) {

      int start = ts[k];
      int end = te[k];
      int diff = end - start;
      int student = qm[k];
      vector<int> timecount;
      timecount.resize(diff);
      for (size_t i = 0; i < diff; i++)
	timecount[i] = 0;

      for (size_t i = 0; i < r; i++) {
	int intime, outtime;

	//question????????????recode ??????????????°????????????
	if (student == stu[i] && log[i]) {
	  intime = time[i];
	  //pc????????§???
	  int pcnum = pc[i]; 
	  int j = i + 1;
	  //?????°??¢???????¨?????????¢???
	  while (true) {
	    if(student == stu[j] && pcnum == pc[j]) {
	      outtime = time[j];

	      if (intime < start) intime = start;
	      if (outtime > end) outtime = end;
	      intime -= start;
	      outtime -= start;

	         
	      for (int l = intime; l < outtime; l++)
		timecount[l] = 1;
	      break;
	    }
	    j++;
	    if (j > r)
	      break;
	  }
	}
      }
      int cnt = 0;
      for (size_t i = 0; i < diff; i++){
	if (timecount[i])
	  cnt++;
      }
      cout << cnt << endl;
     

    }
  }
  
  
  return true;
  
}

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

using namespace std;

const int MaxN = 1001;
const int MaxM = 10001;

int N, M;

int main() {
  int count[MaxM];

  while(cin>>N>>M && !(N==0 && M==0)) {
    vector < vector< pair<int,int> > > logg(MaxM);
    int r;
    cin>>r;
    int t,n,m,s;
    for(int i=0; i<MaxM; i++) { // init
      count[i] = 0;
    }
    for(int i=0; i<r; i++) {
      cin>>t>>n>>m>>s;
      if(s == 1) { // login
	count[m]++;
	if(count[m] == 1) {
	  logg[m].push_back(make_pair(t,t));
	}
      }
      else { // logout
	count[m]--;
	if(count[m] == 0) {
	  logg[m].back().second = t;
	}
      }
    }
    int q;
    cin>>q;
    for(int i=0; i<q; i++) {
      int ts, te, m, ans = 0;
      cin>>ts>>te>>m;
      int ans1, ans2; // 4debug ans1は誤りらしい; エラーデータを弾けないから?
      for(int j=0; j<logg[m].size(); j++) {
	if(logg[m][j].first == logg[m][j].second) {
	  logg[m][j].second = te;
	}
	if(logg[m][j].first < te) {
	  ans1 = min(te, logg[m][j].second)-max(ts, logg[m][j].first);
	}
	else {
	  ans1 = 0;
	}
	ans2 = max(0, min(te,logg[m][j].second)-max(ts,logg[m][j].first));
	
        if(ans1!=ans2) {
	  cout<<"Q: "<<ts<<" "<<te<<endl;
	  cout<<"why: "<<min(te, logg[m][j].second)-max(ts, logg[m][j].first)<<endl;
	  cout<<"val: "<<logg[m][j].first<<" "<<logg[m][j].second<<endl;
	  cout<<"ans: "<<ans1<<" "<<ans2<<endl;
	}
	
        ans += ans2;
      }
      cout<<ans<<endl;
    }
  }
}

C++: #include<iostream>

using namespace std;

#define MMAX 10001
#define tMAX 1261
int main(){

	int N, M;
	int r;
	int t, n, m1, s;
	int q;
	int ts, te, m2;
	int num,cnt;
	int data[MMAX+1][tMAX+1];
return 0;
	while (1){
		cin >> N >> M;
		if (N == 0 && M == 0) break;
		for (int i = 1; i <= M; i++){
			for (int j = 0; j <= tMAX; j++){
				data[i][j] = 0;
			}
		}
		cin >> r;
		for (int i = 0; i < r; i++){
			cin >> t >> n >> m1 >> s;
			if (s == 0) data[m1][t]=-1;
			else data[m1][t]=1;
		}
		cin >> q;
		for (int i = 1; i <= M; i++){
			num = 0;
			for (int j = 0; j <= tMAX; j++){
				num += data[i][j];
				data[i][j] = num;
			}
		}
		for (int i = 0; i < q; i++){
			cin >> ts >> te >> m2;
			cnt = 0;
			for (int j = ts; j < te; j++){
				if (data[m2][j] >= 1) cnt++;
			}
			cout << cnt << endl;
		}
	}
	return 0;
}