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>
#include<vector>
#include<cassert>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<28)

using namespace std;
typedef pair<int,int> ii;
int field[5010][5];
int top[5];
int n,d,p,q;
vector<ii> input;


void print()
{
  int st = 0;
  rep(i,5)st = max(st,top[i]);
  for(int y=st;y>=0;y--)
    {
      rep(x,5)
	{
	  cout << field[y][x] << " ";
	}
      cout << endl;
    }
}

bool checkX(int y)
{
  rep(x,5)if(!field[y][x])return false;
  return true;
}

void updateD(int x,int y)
{
  ii info = input[field[y][x]];
  if(info.first == 1)
    {//yoko
      while(true)
	{
	  bool check = true;
	  rep(i,5)
	    {
	      if(field[y][x] != field[y][i])continue;
	      if(y-1 >= 0 && field[y-1][i] != 0)
		{
		  check = false;
		  break;
		}
	    }
	  if(!check)return;
	  int v = field[y][x];
	  rep(i,5)
	    {
	      if(v != field[y][i])continue;
	      assert(field[y-1][i] == 0);
	      field[y-1][i] = field[y][i];
	      field[y][i] = 0;
	    }
	  y = y-1;
	  if(y <= 0)return;
	}
    }
  else
    {
      int v = field[y][x];
    
      int bot = 0;
      int py = y;
      while(py-1 >= 0 && field[py-1][x] == 0)py--;
      bot = y-py;

      if(bot == 0)return;

      while(v == field[y][x])
	{
	  assert(y-bot >= 0);
	  assert(field[y-bot][x] == 0);
	  field[y-bot][x] = field[y][x];
	  field[y][x] = 0;
	  y++;
	}
	
    }
}

int main()
{
  while(cin >> n,n)
    {
      input.clear();
      input.resize(1010,ii(-1,-1));
      rep(i,5010)rep(j,5)field[i][j] = 0;
      rep(i,5)top[i] = 0;
      REP(i,1,n+1)
	{
	  cin >> d >> p >> q;
	  if(d == 1 && p == 5)continue;
	  q--;
	  input[i] = ii(d,p);
	  int cyb,cyt;
	  if(d == 2)
	    {
	      cyb = top[q],cyt = top[q]+p;
	      REP(j,top[q],top[q]+p)field[j][q] = i;
	      top[q] += p;
	    }
	  else
	    {
	      int mexico = -inf;
	      REP(j,q,q+p)mexico = max(mexico,top[j]);
	      REP(j,q,q+p)field[mexico][j] = i;
	      cyb = cyt = mexico;
	      REP(j,q,q+p)top[j] = mexico+1;
	    }

	  //check, this is teroris so do no need to check again and again
	  vector<int> erasedY;
	  REP(y,cyb,cyt+1)
	    {
	      if(checkX(y))
		{
		  erasedY.push_back(y);
		  rep(x,5)field[y][x] = 0;
		}
	    }

	  //print();
	  //down
	  if(!erasedY.empty())
	    {


	      int yt = 0;
	      rep(j,5)yt = max(yt,top[j]);
	      rep(j,erasedY.size())
		{
		  REP(y,erasedY[j],yt)
		    {
		      int pre = -1;
		      rep(x,5)
			{
			  if(field[y][x] == pre)continue;
			  if(field[y][x] == 0 || y-1 < 0)continue;

			  pre = field[y][x];
			  updateD(x,y);
			}
		    }
		}


	    }
	  //update top

	  rep(j,5)
	    {
	      int y = top[j];
	      while(y >= 0 && field[y][j] == 0)y--;
	      top[j] = y+1;
	    }
	  /*
	  cout << "top = ";
	  rep(j,5)cout << top[j] << " ";
	  cout << endl;
	  print();

	  cout << "----------------" << endl;
	  */
	}
      int yt = 0;
      rep(i,5)yt = max(yt,top[i]);
      int ans = 0;
      rep(y,yt)
	rep(x,5)
       	  if(field[y][x] != 0)ans++;	
      cout << ans << endl;
    }
  return 0;
}

C++: // 2011/08/10 Tazoe

#include <iostream>
using namespace std;

int main()
{
	while(true){
		int n;
		cin >> n;
		if(n==0)
			break;

		int T[5000][5];
		for(int i=0; i<5000; i++)
			for(int j=0; j<5; j++)
				T[i][j] = 0;
		int I1 = 0;
		int I2 = 0;

		int N = 0;

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

			if(d==1){
				int i;
				bool flg = true;
				for(i=I1-1; i>=I2; i--){
					for(int j=0; j<p; j++){
						if(T[i][q-1+j]!=0){
							flg = false;
							break;
						}
					}
					if(!flg)
						break;
				}
				for(int j=0; j<p; j++)
					T[i+1][q-1+j] = 1;
				if(I1<i+2)
					I1 = i+2;
			}
			else if(d==2){
				int i;
				bool flg = true;
				for(i=I1-1; i>=I2; i--){
					for(int j=0; j<p; j++){
						if(T[i+j][q-1]!=0){
							flg = false;
							break;
						}
					}
					if(!flg)
						break;
				}
				for(int j=0; j<p; j++)
					T[i+1+j][q-1] = 1;
				if(I1<i+1+p)
					I1 = i+1+p;
			}

			N += p;

			for(int i=I2; i<I1; i++)
				if(T[i][0]==1&&T[i][1]==1&&T[i][2]==1&&T[i][3]==1&&T[i][4]==1){
					T[i][0] = 9;
				}


			for(int i=I2; i<I1; i++)
				if(T[i][0]==9){
					N -= 5;
					for(int j=i; j<I1-1; j++){
						T[j][0] = T[j+1][0];
						T[j][1] = T[j+1][1];
						T[j][2] = T[j+1][2];
						T[j][3] = T[j+1][3];
						T[j][4] = T[j+1][4];
					}
					I1--;
					i--;
				}

		}
/*
		for(int i=I2; i<I1; i++){
			for(int j=0; j<5; j++)
				cout << T[i][j];
			cout << endl;
		}				
*/
		cout << N << endl;
	}

	return 0;
}