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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }



/* Header */
enum Color{ CLEAR, BLACK };
struct Point;
class Block;
class Board;

struct Point
{
	int x, y;
	
	Point() : x(0), y(0) {}
	Point(int v) : x(v), y(v) {}
	Point(int x, int y) : x(x), y(y) {}
};

Point operator + (const Point& left, const Point& right)
{	return Point(left.x + right.x, left.y + right.y);	}

class Block
{
public:

	Color color;
	
	// points[0]ÌÝâÎÀWA»êÈOÍpoints[0]©çÌÎÀW
	vector<Point> points;
	
	Block(vector<Point> points, Color color)
		: points(points), color(color) {}
};

class Board
{
	int width, height;
	vector<vector<Color> > board;
	
public:
	
	Board(int width, int height);
	
	bool BoardRange(Point point);
		
	bool PutBlock(const Block& block, bool twice = false);
	
	bool RemoveBlock(const Block& block);
	
	bool DownBlock(Block *block);
	
	int DeleteLine();
	
	int CountBlock();
};



/* Implement */
Board::Board(int width, int height)
	: width(width), height(height)
{
	board = vector<vector<Color> >(height, vector<Color>(width, CLEAR));
}

bool Board::BoardRange(Point point)
{
	return 0 <= point.x && point.x < width && 0 <= point.y && point.y < height;
}

bool Board::PutBlock(const Block& block, bool twice)
{
	for(int i = 0; i < block.points.size(); i++)
	{
		Point p = (i ? block.points[0] : Point(0, 0)) + block.points[i];
		
		if(twice) board[p.y][p.x] = block.color;
		else if(!BoardRange(p) || board[p.y][p.x] != CLEAR) return false;
	}
	
	if(!twice) PutBlock(block, true);
	
	return true;
}

bool Board::RemoveBlock(const Block& block)
{
	for(int i = 0; i < block.points.size(); i++)
	{
		Point p = (i ? block.points[0] : Point(0, 0)) + block.points[i];
		
		board[p.y][p.x] = CLEAR;
	}
}

bool Board::DownBlock(Block *block)
{
	RemoveBlock(*block);
	
	block->points[0].y--;
	
	if(PutBlock(*block))
	{
		return true;
	}
	else
	{
		block->points[0].y++;
		PutBlock(*block);
		
		return false;
	}
}

int Board::DeleteLine()
{
	int ret = 0;
	
	for(int i = height - 1; 0 <= i; i--)
	{
		if(count(board[i].begin(), board[i].end(), CLEAR) == 0)
		{
			board[i] = vector<Color>(width, CLEAR);
			
			for(int j = i; j < height - 1; j++)
			{
				board[j] = board[j + 1];
			}
			
			ret++;
		}
	}
	
	return ret;
}

int Board::CountBlock()
{
	int ret = 0;
	
	for(int y = 0; y < height; y++)
	{
		for(int x = 0; x < width; x++)
		{
			ret += board[y][x] != CLEAR;
		}
	}
	
	return ret;
}



/* Entry */
int main()
{
    int n;
	while(cin >> n && n)
	{		
		Board board(5, 5048);
		
		rep(i, n)
		{
			int d, p, q;
			cin >> d >> p >> q;
			
			vector<Point> points; points.push_back(Point(q - 1, 5000));
			REP(j, 1, p)  points.push_back(Point((d == 1 ? j : 0), (d == 2 ? j : 0)));
			
			Block block(points, BLACK);
			while(board.DownBlock(&block));
			board.DeleteLine();
		}
		
		cout << board.CountBlock() << endl;
	}
}

C++: #include <iostream>
#include <math.h>

using namespace std;

int main() {
	int i,j,n,a,b,c,d,e,f,g,m[5002];
	while(cin >> n) {
		if (n==0) break;
		for (j=0;j<5002;j++) m[j]=0; d=0;
		for (i=0;i<n;i++) {
			cin >> a >> b >> c;
			f=a >1 ? 1 << (c-1) : ((1 << b)-1)  << (c-1);  
			for (j=d;j>=0;j--) if ( m[j]+f != (m[j] | f)) break;
			e=g=j+1;
			if (a==1) { m[e]+=f; d++;}  else for (j=0;j<b;j++) { m[e+j]+=f; d+=b;}
			for (j=g; j<g+d+1; j++) if (m[j]<31) m[e++]=m[j];
		}
		f=0;
		for (i=0;i<5000;i++) for (j=0;j<5;j++) f+=(m[i] & ( 1 << j))>0;
		cout << f << endl;
	}
	return 0;
}