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<cstdio>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
	typedef pair<int,int>P;
	typedef vector<P>V;
	int N,M,r,q,t,n,m,s,e,i,j;
	while(scanf("%d%d",&N,&M),N)
	{
		P p;
		vector<V>S(M+1);
		V T(M+1);
		scanf("%d",&r);
		for(i=0;i<r;++i)
		{
			scanf("%d%d%d%d",&t,&n,&m,&s);
			if(s){if(!T[m].second++)T[m].first=t;}
			else{if(!--T[m].second)S[m].push_back(P(T[m].first,t));}
		}
		scanf("%d",&q);
		for(i=0;i<q;++i)
		{
			scanf("%d%d%d",&s,&e,&m);
			for(r=j=0;j<S[m].size();++j)
			{
				p=S[m][j];
				p.first=max(p.first,s);
				p.second=min(p.second,e);
				if(p.first<p.second)r+=p.second-p.first;
			}
			printf("%d\n",r);
		}
	}
	return 0;
}

C++: /*
 * Analyzing Login/Logout Records
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1148
 */

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
//#define LOG(...)
#define FOR(i, a, b) for(int i=(int)(a); i<(int)(b); ++i)
#define REP(i, n) for(int i=0; i<(int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main() {
  int N, M;
  while(cin >> N >> M, N | M) {
    vector<vector<pii>> use(M);
    vi tim(M, 2000);
    vi num(M, 0);
    int r;
    cin >> r;
    REP(i, r){
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      m--;
      tim[m] = min(tim[m], t);
      num[m]+= s * 2 - 1;
      if(num[m] == 0) {
        use[m].push_back({tim[m], t});
        tim[m] = 2000;
      }
    }

    int q;
    cin >> q;
    REP(i, q){
      int a, b, m;
      cin >> a >> b >> m;
      m--;
      int ans = 0;
      for(pii t : use[m]) {
        ans += max(min(t.second, b) - max(t.first, a), 0);
      }
      cout << ans << endl;
    }
  }
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main(){
	int n;
	while(cin>>n>>n,n){
		vvi in(n,vi(2000));
		int m;
		cin>>m;
		vvi tmp(m,vi(4));
		rep(i,m)rep(j,4)cin>>tmp[i][j];
		rep(i,m)if(!tmp[i][3]){
			for(int j=i-1;j>=0;j--){
				if(tmp[i][1]==tmp[j][1]&&tmp[i][2]==tmp[j][2]){
					loop(k,tmp[j][0]+1,tmp[i][0]+1)in[tmp[i][2]-1][k]++;	
					break;
				}
			}
		}
		//while(cin>>m,m)cout<<in[0][m]<<endl;
		cin>>m;
		while(m--){
			int a,b,c;
			cin>>a>>b>>c;
			int out=0;
			loop(i,a+1,b+1)if(in[c-1][i])out++;
			cout<<out<<endl;
		}
	}
}

C++: //Analyzing Login/Logout Records
#include<bits/stdc++.h>
using namespace std;

int pc[1000][721], st[10000][721];

int main(){
  int N, M;
  while(cin>>N>>M, !(N==0&&M==0)){
    int r;
    memset(pc, 0, sizeof(pc)); memset(st, 0, sizeof(st));
    cin>>r;
    while(r--){
      int t, n, m, s;
      cin>>t>>n>>m>>s;
      pc[n-1][t-540]=m;
      if(s==0){
	st[m-1][t-540]=1;
	t--;
	while(true){
	  st[m-1][t-540]=1;
	  if(pc[n-1][t-540]==m || t-540==0)break;
	  t--;
	}
      }
    }
    int q;
    cin>>q;
    while(q--){
      int ts, te, m;
      cin>>ts>>te>>m;
      int cnt=0;
      bool flag=false;
      for(int i=ts-540; i<=te-540; i++){
	if(st[m-1][i]==1 && flag)
	  cnt++;
	if(st[m-1][i]==1)flag=true;
	else flag=false;
      }
      cout<<cnt<<endl;
    }
  }
  return 0;
}

C++: #include<iostream>
 
using namespace std;
 
#define MMAX 10000
#define tMAX (60*24)

int main(void){
 
    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];
 
    while (cin >> N >> M){
        if (N == 0 && M == 0) break;
        for (int i = 0; i <= MMAX; 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]--;
            else data[m1][t]++;
        }
        for (int i = 1; i <= M; i++){
            num = 0;
            for (int j = 0; j <= tMAX; j++){
                num += data[i][j];
                data[i][j] = num;
            }
        }
		cin>>q;
        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;
}

C++: #include<bits/stdc++.h>
using namespace std;
int x[10000][1440],y[10000],n,m,r,q,i,j,a,b,c,d,s;
int main(){
while(true){
memset(x,0,sizeof(x));cin>>n>>m>>r;if(n==0){break;}
for(i=0;i<r;i++){cin>>a>>b>>c>>d;
if(d==1){y[c]++;for(j=a;j<1440;j++){x[c][j]=1;}}
else{y[c]--;if(y[c]==0){for(j=a;j<1440;j++){x[c][j]=0;}}}}
cin>>q;for(i=0;i<q;i++){s=0;cin>>a>>b>>c;for(j=a;j<b;j++){if(x[c][j]==1){s++;}}
cout<<s<<endl;}}}