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 <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <deque>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
#define println(X) cout<<X<<endl;
 
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;


int main(){
	int N, M, r, q;
	while(cin>>N>>M){
		if(N==0 && M==0) break;
		cin>>r;
		vi t(r), n(r), m(r), s(r);
		rep(i, r){
			cin>>t[i]>>n[i]>>m[i]>>s[i];
		}
		cin>>q;
		vi ts(q), te(q), qm(q);
		rep(i, q){
			cin>>ts[i]>>te[i]>>qm[i];
		}

		vector<pair<pair<int,int>, int > >p[1010];
		for(int i = 0; i < r; i++){
			if(s[i]==0){
				p[n[i]].back().first.second=t[i];
			}
			else{
				p[n[i]].push_back(pair<pii, int>(pii(t[i], -1), m[i]));
			}
		}

		vector<pii> v[10010];
		for(int i = 0; i < 1010; i++){
			for(int j = 0; j < p[i].size(); j++){
				v[p[i][j].se].pb(p[i][j].fi);
			}
		}
		for(int i = 0; i < 10010; i++){
			sort(all(v[i]));
			vector<pii> tmp;
			if(v[i].size()>0){
				tmp.pb(v[i][0]);
				for(int j = 1; j < v[i].size(); j++){
					if(tmp.back().se >= v[i][j].fi){
						tmp.back().se = max(tmp.back().se, v[i][j].se);
					}
					else{
						tmp.pb(v[i][j]);
					}
				}
				v[i]= tmp;
			}
		}

		for(int i = 0; i <q; i++){
			int ans = 0;
			for(int j = 0; j < v[qm[i]].size(); j++){
				if(v[qm[i]][j].se >= ts[i] && v[qm[i]][j].fi <= te[i]){
					ans += min(v[qm[i]][j].se, te[i])-max(v[qm[i]][j].fi, ts[i]);
				}
			}
			cout<<ans<<endl;
		}
	}
}

C++: #include<cstdio>
#include<set>
#include<iostream>
using namespace std;

struct st_student{
	bool t[721];//540 to 1260
};

struct st_pc{
	set<int> t;//540 to 1260
};

int n,m,r,q,t,s,ts,te;
st_student *student;
st_pc **pc;

void Or(){
	int ts,te;
	set<int>::iterator itr;
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			for(itr=pc[j][i].t.begin();itr!=pc[j][i].t.end();itr++){
				ts=*itr;
				++itr;
				te=*itr;
				for(int k=ts;k<te;k++) student[i].t[k]|=1;
			}
		}
	}
}

int cal(st_student st,int ts,int te){
	int cnt=0;
	for(int i=ts;i<te;i++) if(st.t[i]) cnt++;
	return cnt;
}

int main(){
	int nn,mm,mmm;
	while(cin>>n>>m){
		if(n==0 && m==0) break;
		student=new st_student[m];
		pc=new st_pc*[n];
		for(int i=0;i<n;i++) pc[i]=new st_pc[m];
		for(int i=0;i<m;i++){
			for(int j=0;j<721;j++) student[i].t[j]=0;
		}
		cin>>r;
		for(int i=0;i<r;i++){
			cin>>t>>nn>>mm>>s;
			pc[nn-1][mm-1].t.insert(t-540);
		}
		Or();
		cin>>q;
		for(int i=0;i<q;i++){
			cin>>ts>>te>>mmm;
			cout<<cal(student[mmm-1],ts-540,te-540)<<endl;
		}
		delete []student;
		for(int i=0;i<n;i++) delete []pc[i];
		delete []pc;
	}
	return 0;
}

C++: 
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <set>
#include <map>
#include <cstring>
#include <cstdio>
#include <stack>
#define INF 1000000000
#define rep(i,n) for(int i =0;i<n;i++)
using namespace std;
typedef long long int ll;

int main(){
    for(;;){
        int N,M;
        cin >> N>>M;
        if(!N and !M)break;
        int r;
        cin >> r;
        vector<int> g(10001*721,0);
        map<ll, int> hoge;
        for (int i=0; i<r; i++) {
            int t,n,m,s;
            cin >>t>>n>>m>>s;
            t-=540;
            if (s==1) {
                hoge[1000*m+n]=t;
            }else{
                for (int j=hoge[1000*m+n];j<t; j++) {
                    g[m*721 + j] = 1;
                }
            }
        }
        int q;
        cin >>q;
        for (int qq = 0; qq < q; qq++) {
            int ts,te,m,ans = 0;
            cin >> ts >> te >>m;
            for (int i= ts-540; i<te-540; i++) {
                if (g[m*721+i] == 1) {
                    ans++;
                }
            }
            cout << ans <<endl;
        }
    }
}

C++: 
#include<iostream>
using namespace std;

int main()
{
loop:
  int min = 1 << 30, max = 0, sum = 0, n;
  cin >> n;
  if(!s)
    return 0;
  for(int i = 0; i < n; i++)
  {
    int tmp;
    cin >> tmp;
    max = max < tmp ? tmp : max;
    min = min > tmp ? tmp : min;
    sum += tmp;
  }
  cout << (sum - max - min) / (n - 2) << endl;
  goto loop;
}