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()
{
    cin >> n;
    
    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;
            }
        }
    }
    
    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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

bool map[5000][6];
int N;

void func(){

	int max_height = 5*N-1;

	for(int row = 0; row <= max_height; row++){
		for(int col = 1; col <= 5; col++){
			map[row][col] = false;
		}
	}

	int top_pos[6] = {0,0,0,0,0,0};

	int dir,len,loc,tmp_max;
	stack<int> delete_row;
	bool FLG;

	for(int loop = 0; loop < N; loop++){
		scanf("%d %d %d",&dir,&len,&loc);

		if(dir == 1){
			tmp_max = 0;
			for(int col = loc; col <= loc+len-1; col++){
				tmp_max = max(tmp_max,top_pos[col]);
			}

			for(int col = loc; col <= loc+len-1; col++){
				map[tmp_max][col] = true;
				top_pos[col] = tmp_max+1;
			}
		}else{
			for(int i = 0; i < len; i++){
				map[top_pos[loc]+i][loc] = true;
			}
			top_pos[loc] += len;
		}

		tmp_max = 0;
		for(int col = 1; col <= 5; col++){
			tmp_max = max(tmp_max,top_pos[col]);
		}

		for(int row = 0; row <= tmp_max;row++){
			FLG = true;
			for(int col = 1; col <= 5; col++){
				if(map[row][col] == false){
					FLG = false;
					break;
				}

			}
			if(FLG){
				for(int col = 1; col <= 5; col++){
					map[row][col] = false;
				}
				delete_row.push(row);
			}
		}

		while(!delete_row.empty()){

			for(int col = 1; col <= 5; col++){
				for(int row = delete_row.top()+1; row <= top_pos[col]; row++){
					if(map[row][col]){
						map[row][col] = false;
						map[row-1][col] = true;
					}
				}
				for(int row = top_pos[col]; row >= 0; row--){
					if(row == 0){
						if(map[row][col]){
							top_pos[col] = 1;
						}else{
							top_pos[col] = 0;
						}
						break;
					}else{
						if(map[row][col]){
							top_pos[col] = row+1;
							break;
						}
					}
				}
			}

			tmp_max = 0;
			for(int col = 1; col <= 5; col++){
				tmp_max = max(tmp_max,top_pos[col]);
			}

			for(int row = 0; row <= tmp_max;row++){
				FLG = true;
				for(int col = 1; col <= 5; col++){
					if(map[row][col] == false){
						FLG = false;
						break;
					}

				}
				if(FLG){
					for(int col = 1; col <= 5; col++){
						map[row][col] = false;
					}
					delete_row.push(row);
				}
			}
			delete_row.pop();
		}
	}

	max_height = 0;
	for(int col = 1; col <= 5; col++){
		max_height = max(max_height,top_pos[col]);
	}

	int ans = 0;
	for(int row = 0; row <= max_height; row++){
		for(int col = 1; col <= 5; col++){
			if(map[row][col]){
				ans++;
			}
		}
	}

	printf("%d\n",ans);
}

int main(){

	while(true){
		scanf("%d",&N);
		if(N == 0)break;

		func();
	}
	return 0;
}

C++: 
#include<iostream>
using namespace std;
#define W 5
#define H 5000
#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;
      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;
	  }
	}
      }
      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++;
	}
      }
    }
    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;
  }
}