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;

int imos[512][2048];

int main()
{
    int n, m;
    
    while (scanf("%d %d", &n, &m) && n){
        int r;
        scanf("%d", &r);
        
        vector<int> xs;
        pair<pair<int, int>, pair<int, int> > p[1024];
        for (int i = 0; i < r; i++){
            int t, n, m, s;
            scanf("%d %d %d %d", &t, &n, &m, &s);
            p[i] = make_pair(make_pair(n, t), make_pair(m, s));
            xs.push_back(m);
        }
        
        sort(xs.begin(), xs.end());
        xs.erase(unique(xs.begin(), xs.end()), xs.end());
        sort(p, p + r);
        
        memset(imos, 0, sizeof(imos));
        
        for (int i = 0; i < r; i++){
            int s = lower_bound(xs.begin(), xs.end(), p[i].second.first) - xs.begin();
            if (p[i].second.second) imos[s][p[i].first.second]++;
            else imos[s][p[i].first.second]--;
        }
        
        for (int i = 0; i < xs.size(); i++){
            for (int j = 540; j <= 1260; j++){
                imos[i][j] += imos[i][j - 1];
            }
            for (int j = 540; j <= 1260; j++){
                imos[i][j] = !!imos[i][j];
                imos[i][j] += imos[i][j - 1];
            }
        }
        
        int q;
        scanf("%d", &q);
        for (int i = 0; i < q; i++){
            int s, e, m;
            scanf("%d %d %d", &s, &e, &m);
            if (binary_search(xs.begin(), xs.end(), m) == 0){
                printf("0\n");
            }
            else {
                m = lower_bound(xs.begin(), xs.end(), m) - xs.begin();
                printf("%d\n", imos[m][e - 1] - imos[m][s - 1]);
            }
        }
    }
    
    return (0);
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))
using ll = long long;

int main() {

    int n, m, r, q;
    while (cin >> n >> m, (n or m)) {

        cin >> r;

        vector< pair<int, int> > nt[m+1];

        vector<int> tt(r), nn(r), mm(r), ss(r);
        rep(i, r) {
            cin >> tt[i] >> nn[i] >> mm[i] >> ss[i];
            nn[i]--;
            mm[i]--;
        }

        rep(i, r) {
            if (ss[i] == 1) {
                for (int j=i+1; j<r; ++j) {
                    if (nn[i] == nn[j] and mm[i] == mm[j] and ss[j] == 0 and tt[j] != -1) {
                        nt[mm[i]].push_back({tt[i], tt[j]});
                        tt[j] = -1;
                        break;
                    }
                }
            }
        }

        cin >> q;

        rep(i, q) {
            int ts, te, qm;
            cin >> ts >> te >> qm;

            int ans=0, s=-1, e=-1;
            qm--;

            for (auto se : nt[qm]) {
                int ss = se.first;
                int ee = se.second;

                if (ts > ee) continue;
                if (ss > te) break;

                if (s != -1 and e != -1) {
                    if (e >= ss) {
                        e = max(e, min(ee, te));
                    }
                    else {
                        ans += (e - s);
                        s = ss;
                        e = min(ee, te);
                    }
                }

                if (s == -1) {
                    s = max(ts, ss);
                }

                if (e == -1) {
                    e = min(te, ee);
                }

            }

            cout << ans + (e - s) << endl;

        }

    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=0;i<n;i++)
#define fordebug int hoge;cin>>hoge;
int a[10000][2000];
int main(){
  while(1){
    int n,m;
    cin>>n>>m;
    if(n==0&&m==0) break;
    int r;
    cin>>r;
    lp(i,r){
      int t,nn,mm,s;
      cin>>t>>nn>>mm>>s;
      if(s==0) a[mm][t-1]=-1; 
      else a[mm][t]=s;
    }
    lp(i,n){
      for(int j=1;j<2000;j++){
	a[i][j]+=a[i][j-1];
      }
    }
    int q;
    cin>>q;
    lp(i,q){
      int x,y,z;
      cin>>x>>y>>z;
      int count=0;
      for(int j=x;j<=y;j++){
	if(a[z][j]!=0) count++;
      }
      cout<<count<<endl;
    }
    lp(i,m){
      lp(j,2000){
	a[i][j]=0;
      }
    }
  }
  return 0;
}