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>
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
#include<cstring>
#include<string>
#include <math.h>
#include<algorithm>
#include<functional>

#define ll long long
#define inf  1000000007
#define mod 1000000007
#define pa pair<int,int>
#define pal pair<ll,ll>
#define ppa pair<int,pa>
#define EPS (1e-10)
#define equals(a,b) (fabs((a)-(b))<EPS)

using namespace std;

class Point{
  public:
  double x,y;
  Point(double x=0,double y=0):x(x),y(y) {}
  Point operator + (Point p) {return Point(x+p.x,y+p.y);}
  Point operator - (Point p) {return Point(x-p.x,y-p.y);}
  Point operator * (double a) {return Point(x*a,y*a);}
  Point operator / (double a) {return Point(x/a,y/a);}
  double absv() {return sqrt(norm());}
  double norm() {return x*x+y*y;}
  bool operator < (const Point &p) const{
    return x != p.x ? x<p.x: y<p.y;
  }
  bool operator == (const Point &p) const{
    return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS;
  }
};
typedef Point Vector;

struct Segment{
Point p1,p2;
};

double hen(Vector a){
if(fabs(a.x)<EPS && a.y>0) return acos(0);
else if(fabs(a.x)<EPS && a.y<0) return 3*acos(0);
else if(fabs(a.y)<EPS && a.x<0) return 2*acos(0);
else if(fabs(a.y)<EPS && a.x>0) return 0.0;
else if(a.y>0) return acos(a.x/a.absv());
else return 2*acos(0)+acos(-a.x/a.absv());

}

double dot(Vector a,Vector b){
  return a.x*b.x+a.y*b.y;
}
double cross(Vector a,Vector b){
  return a.x*b.y-a.y*b.x;
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ru[11]={1,2,4,8,16,32,64,128,256,512,1024};

ll frac[100010];
ll inv[100010];

ll saikif(ll a,int n){
  if(n==1) return a%mod;
  else if(n%2==0){
    ll q=saikif(a,n/2);
    return (q*q)%mod;
  }
  else return (a*saikif(a,n-1))%mod;
}

void gyakugen(){
  frac[0]=1;
  for(int i=1;i<=100000;i++) frac[i]=(frac[i-1]*i)%mod;
  for(int i=0;i<=100000;i++) inv[i]=saikif(frac[i],mod-2);
}


//----------------kokomade temple------------

int a[10010][1270]={0};
//int b[10010][1270]={0};
int main(){
int n,m;

while(cin>>n>>m){
  int t,nn,mm,s;
fill(a[0],a[10010],0);
//fill(b[0],b[10010],0);
if(n==0 && m==0) break;
int r,q;
cin>>r;
for(int i=0;i<r;i++){
cin>>t>>nn>>mm>>s;
if(s==1){
  a[mm][t]++;
//  cout<<mm<<" in "<<t<<endl;
}
else{
  a[mm][t]--;
//  cout<<mm<<" out "<<t<<endl;
}
}

for(int i=1;i<=m;i++){
  for(int j=1;j<1265;j++){
    a[i][j] +=a[i][j-1];
  }

    for(int j=1;j<1265;j++){
      if(a[i][j] >0) a[i][j]=1;
    }
  for(int j=1;j<1265;j++){
    a[i][j] = a[i][j-1]+a[i][j];
  }
}

cin>>q;
int ts,te,mm1;
for(int i=0;i<q;i++){
cin>>ts>>te>>mm1;
cout<<a[mm1][te-1]-a[mm1][ts-1]<<endl;
}


}

return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back 
#define pf push_front 
#define mp make_pair
#define fr first
#define sc second
#define Rep(i,n) for(int i=0;i<(n);i++)
#define All(v) v.begin(),v.end()
typedef pair<int, int> Pii; typedef pair<int, Pii> Pip;
const int INF = 1107110711071107;

bool state[10001][1261];
int pc[10001];

main()
{
  int N, M;
  while( cin >> N >> M, !(N == 0 && M == 0) ) {
    int r, q;

    fill_n(*state, 10001*1261, false);

    cin >> r;
    Rep(i, r) {
      int t, n, m, s;
      cin >> t >> n >> m >> s;
      if( s ) pc[n] = t;
      else {
	for(int j=pc[n]; j<t; j++) {
	  state[m][j] = 1;
	}
      }
    }

    cin >> q;
    Rep(i, q) {
      int ts, te, m;
      cin >> ts >> te >> m;
      
      int sum = 0;
      for(int j=ts; j<te; j++) sum += state[m][j];
      
      cout << sum << endl;
    }
    
  }

}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int d[1000];
int e[1000];
int f[1000];
int g[1000];
int main(){
    int a,b;
    while(scanf("%d%d",&a,&b),a+b){
        int c;
        scanf("%d",&c);
        for(int i=0;i<c;i++){
            scanf("%d%d%d%d",d+i,e+i,f+i,g+i);
        }
        int p;
        scanf("%d",&p);
        for(int i=0;i<p;i++){
            int q,r,s;
            scanf("%d%d%d",&q,&r,&s);
            int last=0;
            int ret=0;
            int now=0;
            for(int j=0;j<c;j++){
                if(f[j]==s){
                    if(g[j]){
                        if(now==0)last=d[j];
                        now++;
                    }else{
                        now--;
                        if(now==0){
                            if(q<=d[j]&&d[j]<=r)ret+=d[j]-max(q,last);
                            if(d[j]>r&&last<=r)ret+=r-max(q,last);
                        }
                    }
                }
            }
            printf("%d\n",ret);
        }
    }
}