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 <vector>
#include <iostream>

using namespace std;

int n, d, p, q;

int main()
{
    while(true)
    {
        cin >> n;
        
        if(n == 0) { break; }
        
        vector<vector<bool> > c(5000, vector<bool>(5, false));
        
        for(int i = 0; i < n; i++)
        {
            cin >> d >> p >> q;
            
            if(d == 1)
            {
                int maxh = -1;
                
                for(int j = p - 1; j < p + q - 1; j++)
                {
                    int r = 5000;
                    
                    while(r--)
                    {
                        if(c[r][j] == true)
                        {
                            break;
                        }
                    }
                    
                    maxh = max(maxh, r + 1);
                }
                
                for(int j = p - 1; j < p + q - 1; j++)
                {
                    c[maxh][j] = true;
                }
            }
            else
            {
                int r = 5000;
            
                while(r--)
                {
                    if(c[r][p] == true)
                    {
                        break;
                    }
                }   
                
                for(int j = r + 1; j < r + q + 1; j++)
                {
                    c[j][p] = true;
                }
            }   
            
            for(int j = 0; j < 5000; i++)
            {
                if(c[j][0] == c[j][1] && c[j][1] == c[j][2] && c[j][2] == c[j][3] && c[j][3] == c[j][4])
                {
                    for(int k = j + 1; k < 5000; k++)
                    {
                        for(int l = 0; l < 5; l++)
                        {
                            c[k - 1][l] = c[k][l];
                        }
                    }
                }
            }
        }    
    
        int sum = 0;
    
        for(int i = 0; i < 5000; i++)
        {
            for(int j = 0; j < 5; j++)
            {
                sum += (c[j][p] ? 1 : 0);
            }
        }
        
        cout << sum << endl;
    }
    
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define for_(i,a,b) for(int i=(a);i<(b);++i)

int n;
bool block[5050][5];

void down(int py) {
	for_(y,py,5010) {
		for_(x,0,5) {
			block[y][x] = block[y + 1][x];
			block[y + 1][x] = false;
		}
	}
}

void vanish() {
	for_(y,0,5050) {
		bool van = true;
		
		for_(x,0,5) van &= block[y][x];
		
		if (van) {
			down(y);
			--y;
		}
	}
}

void fall(int dir, int len, int lft) {
	int by = 5010, bx = lft;
	
	while (by > 0) {
		bool end = false;
		
		if (dir) {
			end |= block[by - 1][bx];
		} else {
			for_(x,bx,bx+len) end |= block[by - 1][x];
		}
		
		if (end) break;
		
		--by;
	}
	
	if (dir) {
		for_(y,by,by+len) block[y][bx] = true;
	} else {
		for_(x,bx,bx+len) block[by][x] = true;
	}
}

void play() {
	for_(i,0,n) {
		int a, b, c;
		cin >> a >> b >> c;
		fall(--a, b, --c);
		vanish();
	}
	
	int ans = 0;
	for_(y,0,5050) for_(x,0,5) ans += block[y][x];
	cout << ans << endl;
}

int main() {
	while (cin >> n, n) {
		memset(block, false, sizeof(block));
		play();
	}
}

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=1; 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] = k;
				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] = k;
				if(I1<i+1+p)
					I1 = i+1+p;
			}

			N += p;

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


			for(int i=I2; i<I1; i++)
				if(T[i][0]!=0&&T[i][1]!=0&&T[i][2]!=0&&T[i][3]!=0&&T[i][4]!=0){
					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--;
/*
					for(int j=I2+1; j<I1; j++){
						int j1, j2;
						for(j1=0; j1<5; j1++){
							for(j2=j1; j2<5; j2++)
								if(T[j][j2]!=T[j][j1])
									break;
							j2--;
							int flg2 = true;
							for(int j3=j1; j3<=j2; j3++)
								if(T[j-1][j3]!=0){
									flg2 = false;
									break;
								}
							if(flg2)
								for(int j3=j1; j3<=j2; j3++){
									T[j-1][j3] = T[j][j3];
									T[j][j3] = 0;
								}
							j1 = j2;
						}
					}

					for(int j=I1-1; j>=I2; j--)
						if(T[j][0]==0&&T[j][1]==0&&T[j][2]==0&&T[j][3]==0&&T[j][4]==0)
							I1--;
						else
							break;
*/
				}

		}
/*
		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;
}