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 <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <tuple>
#include <queue>
#include <set>
#include <cstdio>
#include <climits>
#include <cmath>
#include <array>
#include <functional>
#include <sstream>
#include <list>
#include <set>
const int MOD=1000000007;
const int INF=1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const double eps=1e-9;
const int inf=1e9;
typedef pair<int,int> P;
struct Point
{
  double x,y;
  Point(){x=0;y=0;}
  Point(double d_x,double d_y){x=d_x,y=d_y;}
  double operator*(Point obj){return obj.x*x+obj.y*y;}
  double operator%(Point obj){return obj.y*x-obj.x*y;}
  Point operator*(double b){Point tmp;tmp.x=x*b;tmp.y=y*b;return tmp;}
  Point operator/(double b){Point tmp;tmp.x=x/b;tmp.y=y/b;return tmp;}
  Point operator+(Point obj){Point tmp;tmp.x=x+obj.x;tmp.y=y+obj.y;return tmp;}
  Point operator-(){Point tmp;tmp.x=-x;tmp.y=-y;return tmp;}
  Point operator-(Point obj){Point tmp;tmp.x=x-obj.x;tmp.y=y-obj.y;return tmp;}
  Point operator-=(Point obj){x-=obj.x;y-=obj.y;return *this;}
  Point operator+=(Point obj){x+=obj.x;y+=obj.y;return *this;}
  Point operator/=(double b){x=x/b;y=y/b;return *this;}
  Point operator*=(double b){x=x*b;y=y*b;return *this;}
  double size(){return hypot(x,y);}
  Point unit(){return Point(x/size(),y/size());}
  Point normal(){return Point(y,-x);}
  double atan(){return atan2(y,x);}
};

bool operator<(Point a,Point b){return a.x!=b.x?a.x<b.x:a.y<b.y;}
bool operator>(Point a,Point b){return b<a;}
bool operator<=(Point a,Point b){return !(b<a);}
bool operator>=(Point a,Point b){return !(a<b);}
bool operator==(Point a,Point b){return (a-b).size()<eps;}
bool operator!=(Point a,Point b){return !(a==b);}

bool equal(double a,double b){return abs(a-b)<eps;}
double cross(Point a,Point b){return a%b;}
double dot(Point a,Point b){return a*b;}

int ccw(Point a,Point b,Point c)
{
  b=b-a;
  c=c-a;
  if(b%c>0) return +1;
  else if(b%c<0)return -1;
  else if(b*c<0) return +2;
  else if(b.size()<c.size()) return -2;
  else return 0;
}


class history
{
public:
  int pc,start,end;
  history(int p,int s,int e)
  {
    pc=p;start=s;end=e;
  }
};
int main(int argc,char const* argv[])
{
  vector<history> hist[10005];
  int N,M;
  int r,t,n,m,s,q,ts,te;
  int imos[1265];
  while(1)
    {
      cin >> N >> M;
      for(int i=0;i<10005;i++) hist[i].clear();
      if(!N) break;
      cin >> r;
      for(int i=0;i<r;i++)
	{
	  cin >> t >> n >> m >> s;
	  if(s==1)
	    {
	      hist[m].push_back(history(n,t,-1));
	    }
	  else
	    {
	      for(int j=0;j<hist[m].size();j++)
		{
		  if(hist[m][j].pc==n && hist[m][j].end==-1)
		    {
		      hist[m][j].end=t;
		      break;
		    }
		}
	    }
	}
      cin >> q;
      for(int i=0;i<q;i++)
	{
	  cin >> ts >> te >> m;
	  for(int j=0;j<1265;j++) imos[j]=0;
	  for(int j=0;j<hist[m].size();j++)
	    {
	      if(hist[m][j].start<te && hist[m][j].end > ts)
		{
		  imos[max(hist[m][j].start,ts)]+=1;
		  imos[min(hist[m][j].end,te)]-=1;
		}
	    }
	  int ans=0,cnt=0,start;
	  for(int j=0;j<1265;j++)
	    {
	      if(cnt==0 && imos[j] > 0)
		{
		  start=j;
		}
	      if(cnt>0 && cnt+imos[j]==0)
		{
		  ans+=(j-start);
		}
	      cnt+=imos[j];
	    }
	  cout << ans << endl;
	}
    }
  return 0;
}

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

using namespace std;

class student {
public:
    int st[50], ed[50];
    int num, cnt;
    student() { num = 0; cnt = 0; }
};

int main()
{
    int N; // PC数
    int M; // 学生数

    while (cin >> N >> M, N || M) {
        int r;  // 利用記録数
        int q;  // 質問数
        vector<student> std(M + 1);

        // 登録
        cin >> r;
        for (int i = 0; i < r; i++) {
            int t, n, m, s;  // s(1: ログイン, 0: ログアウト), 時刻tにPCnで学生mが
            cin >> t >> n >> m >> s;

            if (s == 1) { // ログイン
                if (std[m].cnt == 0) {  // まだログインしていない
                    std[m].st[std[m].num] = t;
                }
                std[m].cnt++;
            }
            else { // ログアウト
                if (std[m].cnt == 1) 
                    std[m].ed[std[m].num++] = t;
                std[m].cnt--;
            }
        }

        // 質問
        cin >> q;
        for (int i = 0; i < q; i++) {
            int ts, te, m;
            int ans = 0;

            cin >> ts >> te >> m;

            for (int j = 0; j < std[m].num; j++) {
                if (std[m].ed[j] <= ts)
                    continue;
                if (std[m].st[j] >= te)
                    break;
                
                if (std[m].st[j] <= ts && std[m].ed[j] >= te) {
                    ans += te - ts;
                    break;
                }
                if (std[m].st[j] <= ts && std[m].ed[j] < te)
                    ans += std[m].ed[j] - ts;
                else if (std[m].st[j] >= ts && std[m].ed[j] >= te) {
                    ans += te - std[m].st[j];
                    break;
                }
                else if (std[m].st[j] >= ts && std[m].ed[j] < te) 
                    ans += std[m].ed[j] - std[m].st[j];
            }
            cout << ans << endl;
        }
    }

    return 0;
}