Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)
var (
	dat = [5000][5]int{}
	m = [5]int{}
)

func main() {
	for scanner.Scan() {
		for i := range dat {
			for j := range dat[i] {
				dat[i][j] = 0
			}
		}
		for i := range m {
			m[i] = 0
		}
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		count := 0
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			d, _ := strconv.Atoi(buf[0])
			p, _ := strconv.Atoi(buf[1])
			q, _ := strconv.Atoi(buf[2])
			q--
			count += p
			if d == 2 {
				for j := 0; j < p; j++ {
					dat[m[q]][q] = 1
					ok := true
					for k := 0; k < 5; k++ {
						if dat[m[q]][k] == 0 {
							ok = false
						}
					}
					if ok {
						for k := 0; k < 5; k++ {
							dat[m[q]][k] = 0
							for l := m[q]; l < 4999; l++ {
								dat[l][k] = dat[l+1][k]
							}
							for l := 4999; l >= 0; l-- {
								if dat[l][k] != 0 {
									m[k] = l+1
									break
								}
								if l == 0 {
									m[k] = 0
								}
							}
						}
						count -= 5
					} else {
						m[q]++
					}
				}
			} else {
				h := 0
				for i := 0; i < p; i++ {
					h = max(h, m[i+q])
				}
				for i := 0; i < p; i++ {
					dat[h][i+q] = 1
				}
				for i := 0; i < p; i++ {
					m[i+q] = h+1
				}
				ok := true
				for j := 0; j < 5; j++ {
					if dat[h][j] == 0 {
						ok = false
					}
				}
				if ok {
					for j := 0; j < 5; j++ {
						dat[h][j] = 0
						for k := h; k < 4999; k++ {
							dat[k][j]=dat[k+1][j]
						}
						for k := 4999; k>= 0; k-- {
							if dat[k][j] != 0 {
								m[j]=k+1
								break
							}
							if k == 0 {
								m[j] = 0
							}
						}
					}
					count -= 5
				}
			}
		}
		fmt.Println(count)
	}
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define F first
#define S second
#define INF 1 << 30
#define UNDER 1000

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int t[UNDER+1][5], n;

int count(){
  int res = 0;
  rep(i, UNDER) rep(j, 5) if(t[i][j] == 1) res++;
  return res;
}

void deleteblock(){
  for(int y = UNDER-1; y >= 0; y--){
    int f = 0;
    rep(i, 5) if(t[y][i] == 0){ f = 1; break;}
    if(f) continue;
    rep(i, 5){
      for(int c = y; c > 0; c--){
	t[c][i] = t[c-1][i];
      }
    }
    t[0][0] = t[0][1] = t[0][2] = t[0][3] = t[0][4] = 0;
    y++;
  }
  return;
}

int main(){
  while(scanf("%d", &n) && n){
    int d, p, q;
    memset(t, 0, sizeof(t));
    rep(i, 5) t[1000][i] = 1;

    rep(i, n){
      scanf("%d%d%d", &d, &p, &q); q--;
      if(d == 1){
	rep(y, UNDER+1){
	  int f = 0;
	  REP(x, q, q+p) if(t[y][x] == 1) f = 1;
	  if(!f) continue;
	  REP(x, q, q+p) t[y-1][x] = 1;
	  break;
	}
      }else{
	rep(y, UNDER+1){
	  if(!t[y][q]) continue;
	  for(int c = y-1; c > y-p-1; c--) t[c][q] = 1;
	  break;
	}
      }
      deleteblock();
      /*      REP(i, 990, UNDER+1){
	rep(j, 5) printf("%d ", t[i][j]); puts("");
	}*/
    }

    printf("%d\n", count());
  }
  return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>


int main(void)
{
	int result[1000], suffix = 0;

	while (1) {

		int nblock, direct, len, posit, tetris[1000][6] = { 0 };

		scanf("%d", &nblock);
		if (nblock == 0) {
			break;
		}

		for (int n = 0; n < nblock; n++) {
			scanf("%d %d %d", &direct, &len, &posit);

			int success;
			if (direct == 1) {
				for (int i = 1; i < 1000; i++) {
					success = 0;
					for (int j = posit; j < posit + len; j++) {
						if (tetris[i][j] == 1) {
							break;
						}

						for (int k = i; k <= i + 7; k++) {
							if (tetris[k][j] == 1) {
								success = 2;
								break;
							}
						}
						if (success == 2) {
							break;
						}

						if (j == posit + len - 1) {
							for (int k = 0; k < len; k++) {
								tetris[i][k + posit] = 1;
							}
							success = 1;
							break;
						}
					}
					if (success == 1) {
						break;
					}
				}
			}
			else {
				for (int i = 1; i < 1000; i++) {
					success = 0;
					for (int j = i; j < len + i; j++) {
						if (tetris[j][posit] == 1) {
							break;
						}

						for (int k = j; k <= 7; k++) {
							if (tetris[k][posit] == 1) {
								success = 2;
								break;
							}
						}
						if (success == 2) {
							break;
						}

						if (j == len + i - 1) {
							for (int k = 0; k < len; k++) {
								tetris[k + i][posit] = 1;
							}
							success = 1;
							break;
						}
					}
					if (success == 1) {
						break;
					}
					else if (success == 2) {
						continue;
					}
				}
			}
		}

		int sum = 0;
		for (int i = 1; i <= 1000; i++) {
			int a = 0, b = 0;
			for (int j = 1; j <= 5; j++) {
				if (tetris[i][j] == 0) {
					a++;
				}
				else {
					sum++;
					b++;
				}
			}

			if (a == 5) {
				break;
			}
			else if (b == 5) {
				sum -= 5;
			}
		}

		result[suffix++] = sum;
	}

	for (int i = 0; i < suffix; i++) {
		printf("%d\n", result[i]);
	}

	return 0;
}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int dat[5000][5];
int m[5];
int main(){
	int a;
	while(scanf("%d",&a),a){
		for(int i=0;i<5000;i++)
			for(int j=0;j<5;j++)dat[i][j]=0;
		for(int i=0;i<5;i++)m[i]=0;
		int count=0;
		for(int i=0;i<a;i++){
			int b,c,d;
			scanf("%d%d%d",&b,&c,&d);d--;
			count+=c;
			if(b==2){
				for(int i=0;i<c;i++){
					dat[m[d]+i][d]=1;
					bool ok=true;
					for(int j=0;j<5;j++)if(!dat[m[d]+i][j])ok=false;
					if(ok){
						for(int j=0;j<5;j++){
							for(int k=m[d]+i;k<m[j];k++)dat[k][j]=dat[k+1][j];
							dat[m[j]][j]=0;
							m[j]--;
						}
						count-=5;
					}
				}
				m[d]+=c;
			}else{
				int h=0;
				for(int i=0;i<c;i++)h=max(h,m[i+d]);
				for(int i=0;i<c;i++)dat[h][i+d]=1;
				for(int i=0;i<c;i++)m[i+d]=h+1;
				bool ok=true;
				for(int j=0;j<5;j++)if(!dat[h][j])ok=false;
				if(ok){
					for(int j=0;j<5;j++){
						for(int k=h;k<m[j];k++)dat[k][j]=dat[k+1][j];
						dat[m[j]][j]=0;
						m[j]--;
					}
					count-=5;
				}
			}
		}
		printf("%d\n",count);
	}
}