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 <algorithm>

using namespace std;

void solve()
{
	bool isUsed[5002][5];
	int n;
	while (cin >> n, n)
	{
		for (int i = 1; i < 5002; ++i)
		{
			for (int j = 0; j < 5; ++j)
			{
				isUsed[i][j] = false;
			}
		}
		for (int i = 0; i < 5; ++i)
		{
			isUsed[0][i] = true;
		}
		int highest = 1;
		for (int i = 0; i < n; ++i)
		{
			int d, p, q;
			cin >> d >> p >> q;
			--q;
			int level = 0;
			if (d == 1)
			{
				bool isOK = true;
				while (isOK)
				{
					for (int j = 0; j < p; ++j)
					{
						if (isUsed[highest - level][q + j])
						{
							isOK = false;
							--level;
							break;
						}
					}
					++level;
				}
				--level;
				for (int j = 0; j < p; ++j)
				{
					isUsed[highest - level][q + j] = true;
				}
				highest = max(highest, highest - level + 2);
			}
			else if (d == 2)
			{
				while (!isUsed[highest - level][q])
				{
					++level;
				}
				--level;
				for (int j = 0; j < p; ++j)
				{
					isUsed[highest - level + j][q] = true;
				}
				highest = max(highest, highest - level + p + 1);
			}
		}
		int ans = 0;
		for (int i = 1; i < highest; ++i)
		{
			int rowCount = 0;
			for (int j = 0; j < 5; ++j)
			{
				if (isUsed[i][j])
				{
					++ans;
					++rowCount;
				}
				if (rowCount == 5)
				{
					ans -= 5;
				}
			}
		}
		cout << ans << endl;
	}
}

int main()
{
	solve();
	return(0);
}

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 = 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>
#include <algorithm>
using namespace std;

int board[6000][6];

int main() {
	int n;
	while(cin >> n, n) {
		int top[8];

		for(int i = 0; i < 6000; i++)
			for(int j = 0; j < 6; j++)
				board[i][j] = 0;
		for(int i = 0; i < 8; i++)
			top[i] = 0;

		for(int i = 0; i < n; i++) {
			int d, p, q;
			cin >> d >> p >> q;

			if(d == 1) {
				int h_max = -1;
				for(int j = 0; j < p; j++) {
					h_max = max(top[q - 1 + j], h_max);
				}
				for(int j = 0; j < p; j++) {
					board[h_max][q - 1 + j] = 1;
					top[q - 1 + j] = h_max + 1;
				}

				bool flg = true;
				for(int j = 0; j < 5; j++)
					flg &= board[h_max][j];
				if(flg) {
					for(int j = 0; j < 5; j++) {
						board[h_max][j] = 0;
						for(int k = h_max + 1; k < top[j]; k++) {
							board[k - 1][j] = board[k][j];
						}
					}
				}
			}

			else {
				for(int j = 0; j < p; j++) {
					board[top[q - 1] + j][q - 1] = 1;
				}

				int h = top[q - 1];
				top[q - 1] += p;
				int del = 0;
				for(int j = 0; j < p; j++) {
					bool flg = true;
					for(int k = 0; k < 5; k++) {
						flg &= board[h + j][k];
					}
					if(flg) {
						for(int k = 0; k < 5; k++)
							board[h + j - del][k] = board[h + j + 1][k];
						del++;
					}
				}
				for(int j = 0; j < 5; j++) {
					for(int k = h + p - del; k < top[j]; k++) {
						board[k][j] = board[k + del][j];
					}
				}
				for(int j = 0; j < 5; j++)
					top[j] -= del;
			}
		}

		int ans = 0;
		for(int i = 0; i < 6000; i++) {
			for(int j = 0; j < 6; j++) {
				ans += board[i][j];
			}
		}

		cout << ans << endl;
	}
}