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 <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

typedef pair<int,int> P;
char s[5010][5];

// 初期化 (y == 0 のところを番兵でブロックにしておく)
void ini(){
	for(int y=0 ; y < 5010 ; y++ ){
		for(int x=0 ; x < 5 ; x++ ){
			s[y][x] = y? '.' : '#' ;
		}
	}
}

void debug(int ay){
	cout << "[debug]" << endl;
	for(int y=ay ; y > 0 ; y-- ){
		for(int x=0 ; x < 5 ; x++ ){
			cout << s[y][x];
		}
		cout << endl;
	}
	cout << endl;
}

// ブロックの初期位置を返す.
vector<P> block(int d, int p, int q, int y){
	vector<P> v;
	if( d == 1 ){ // 横向きのブロック
		for(int i=0 ; i < p ; i++ ){
			int mx = q + i;
			int my = y;
			v.push_back( P(mx,my) );
		}
	}else{ // 縦向きのブロック
		for(int i=0 ; i < p ; i++ ){
			int mx = q;
			int my = y + i;
			v.push_back( P(mx,my) );
		}
	}
	return v;
}

// ブロックを落とす, ay := ブロックの一番高い位置 + 10
void fall(vector<P> v, int& ay){
	// flag := ブロックが1マス落下できるかどうか
	bool flag = true;
	for(int i=0 ; i < v.size() ; i++ ){
		int x = v[i].first;
		int y = v[i].second;
		if( s[y-1][x] == '#' ){
			flag = false;
		}
	}
	
	if( flag ){ // 1マス落下させるとき
		for(int i=0 ; i < v.size() ; i++ ){
			v[i].second--;
		}
		fall( v , ay );
	}else{ // ブロックの落下位置が決まる
		int my = v[0].second;
		for(int i=0 ; i < v.size() ; i++ ){
			int x = v[i].first;
			int y = v[i].second;
			s[y][x] = '#';
			my = max( my , y );
		}
		ay = max( ay , my + 10);
	}
}

// 高さ y が揃ったかどうか
bool is_line(int y){
	return (s[y][0] == '#' && s[y][1] == '#' && s[y][2] == '#' && s[y][3] == '#' && s[y][4] == '#');
}

// 高さ l の列を消す.
void remove(int l, int ay){
	for(int y=l ; y < ay ; y++ ){
		s[y][0] = s[y+1][0];
		s[y][1] = s[y+1][1];
		s[y][2] = s[y+1][2];
		s[y][3] = s[y+1][3];
		s[y][4] = s[y+1][4];
	}
}

// 揃った列があるか調べて消す.
void check(int ay){
	for(int y=1 ; y < ay ; y++ ){
		if( is_line(y) ){
			remove(y,ay);
			y--;
		}
	}
}

int block_count(int ay){
	int ans = 0;
	for(int y=1 ; y < ay ; y++ ){
		for(int x=0 ; x < 5 ; x++ ){
			if( s[y][x] == '#' ) ans++;
		}
	}
	return ans;
}

int main(){
	int n;
	while( cin >> n , n ){
		ini();
		int y=10;
		for(int i=0 ; i < n ; i++ ){
			int d, p, q;
			cin >> d >> p >> q;
			vector<P> v = block(d,p,q-1,y);
			fall(v,y);
			check(y);
			// debug(y);
		}
		int ans = block_count(y);
		cout << ans << endl;
	}
}

C++: 
#include<iostream>
using namespace std;
#define W 5
#define H 5100
#define BLANK 0
#define BLOCK 1
int main()
{
  int n,d,p,q;
  int field[H+2][W];
  int i,j,k;
  bool flag;
  int count;
  while(cin>>n,n){
    for(i=0;i<H+1;i++)for(j=0;j<W;j++)field[i][j]=BLANK;
    for(j=0;j<W;j++)field[i][j]=BLOCK;
    while(n-->0){
      cin>>d>>p>>q;
      // ¡u«
      if(d==1){
	j=q-1;
	flag=false;
	for(i=0;i<H+2;i++){
	  for(k=0;k<p;k++)if(field[i][j+k]==BLOCK)flag=true;
	  if(flag){
	    for(k=0;k<p;k++)field[i-1][j+k]=BLOCK;
	    break;
	  }
	}
      }
      // cu«
      if(d==2){
	j=q-1;
	for(i=0;i<H+2;i++){
	  if(field[i][j]==BLOCK){
	    for(k=1;k<=p;k++)field[i-k][j]=BLOCK;
	    break;
	  }
	}
      }
      // Á
      for(i=H;i>=0;i--){
	flag=true;
	for(j=0;j<W;j++)if(field[i][j]==BLANK)flag=false;
	if(flag){
	  for(j=0;j<W;j++)field[i][j]=BLANK;
	  for(k=i;k>0;k--)for(j=0;j<W;j++)field[k][j]=field[k-1][j];
	  i++;
	}
      }
    }
    // \¦
    /*
    for(i=H-8;i<H+2;i++){
      for(j=0;j<W;j++){
	cout<<field[i][j]<<" ";
      }
      cout<<endl;
    }
    cout<<endl;
    */
    count=0;
    for(i=H-8;i<H+1;i++)for(j=0;j<W;j++)if(field[i][j]==BLOCK)count++;
    cout<<count<<endl;
  }
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\b.txt"

int fall(vector<vector<int>>&field, vector<vector<int>>&pino,int l) {
	{

	}
	int y = 0;
	for ( y = 0; y < field.size() - pino.size(); ++y) {
		bool ok = true;
		for (int i = 0; i < pino.size(); ++i) {
			for (int j = 0; j < pino[i].size(); ++j) {
				if (pino[i][j]) {
					if (field[y + i+1][l + j])ok = false;
				}
			}
		}
		if (!ok) {
			break;
		}
	}
	for (int i = 0; i < pino.size(); ++i) {
		for (int j = 0; j < pino[i].size(); ++j) {
			if (pino[i][j]) {
				assert(!field[y + i][l + j]);
				field[y + i][l + j] = true;
			}
		}
	}
	int delline = 0;
	for (int i = y + pino.size() - 1; i >= y; --i) {
		while (all_of(field[i].begin(), field[i].end(), [](const int a) {return a == 1; })) {
			transform(field[i].begin(), field[i].end(), field[i].begin(), [](const int a) {return false; });
			for (int j = i- 1; j >= 0; --j) {
				swap(field[j], field[j + 1]);
			}
			delline += 1;
		}
	}
	return delline;
}

int main() {
	while (1) {
		int N; cin >> N;
		if (!N)break;
		int sum = 0;
		vector<vector<int>>field(10, vector<int>(5));
		for (int i = 0; i < N; ++i) {
			int d, p, q; cin >> d >> p >> q; q--;
			int w = d == 2 ? 1 : p;
			int h = d == 1 ? 1 : p;
			sum += p;
			vector<vector<int>>pino(h, vector<int>(w, 1));
			sum -= fall(field, pino, q) * 5;
		}
		cout << sum << endl;
	}
	
	return 0;
}