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 <stack>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#include <sstream>
#include <iomanip>
#include <complex>
using namespace std;

#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back 
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int grid[5][5000];
int n,d,p,q;
ll int ans = 0;

int GetBottom(int x){
	REP(i,5000){
		if(grid[x][i]==-1) return i;
	}
	return -1;
}

bool isallclear(int x){
	REP(i,5){
		if(grid[i][x]!=-1) return false;
	}
	return true;
}

bool isallblock(int x){
	REP(i,5){
		if(grid[i][x]!=1) return false;
	}
	return true;
}

int main(){
	while(1){
		cin >> n;
		REP(i,5) REP(j,100000) grid[i][j] = -1;
		REP(i,n){
			cin >> d >> p >> q;
			if(d==2){
				int tmp = GetBottom(q-1);
				FOR(j,tmp,tmp+p) grid[q-1][j] = 1;
			}else{
				int tmp = -1;
				FOR(j,q-1,q-1+p){
					tmp = max(tmp,GetBottom(j));
				}
				FOR(j,q-1,q-1+p) {
					grid[j][tmp] = 1;
					REP(k,tmp) if(grid[j][k]==-1) grid[j][k] = 0;
				}
			}
			int nowline = 0;
			while(1){
				if(isallclear(nowline)) break;
				if(isallblock(nowline)){
					REP(j,5) grid[j][nowline] = 0;
				}
				nowline++;

			}
		}
		ans = 0;
		int line = 0;
		while(1){
			if(isallclear(line)) break;
			REP(i,5){
				if(grid[i][line]==1) ans++;
			}
			line++;
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <vector>
using namespace std;
#define UI unsigned int

string getM(char mask){
  string s;
  for( int i=4;i>=0;i-- ){
    if( mask & 1<<i ) s+="1";
    else s+="0";
  }
  return s;
}
void pr(vector<char> v){
  cout <<" ***** " << v.size() << endl;
  for( int i=v.size()-1;i>=0;i-- )
    cout << getM(v[i]) << endl;
}

int main(){
  int n, d,p,q, index;
  char ms[5], ful=0, mask;

  for( int i=0;i<5;i++ )
    ful += (ms[i]=1<<i);

  while( cin>>n && n ){
    vector<char> v;
    for( int lp=0;lp<n;lp++ ){
      //      cout << "lp="<<lp<<endl;

      cin >> d >> p >> q; // 向き・長さ・位置
      q--;

      if( d==1 ){ // 横向き
	mask=0;
	for( int i=0;i<p;i++ )	  mask |= ms[q+i];

	index=v.size()-1;
	for( ;index>=0;index-- )
	  if( v[index] & mask ) break;
	index++;
	if( index>=(int)v.size() ) v.push_back(mask);
	else v[index] |= mask;
      }else{ // 縦向き
	mask = ms[q];
	index=v.size()-1;
	for( ;index>=0;index-- )
	  if( v[index] & mask ) break;
	index++;
	for( int i=0;i<p;i++,index++ ){
	  if( index>=(int)v.size() )
	    v.push_back(mask);
	  else
	    v[index] |= mask;
	}
      }
      //      pr( v );

      // 消去チェック
      for( UI i=0;i<v.size();i++ ){
	if( v[i]==ful ){
	  v.erase( v.begin()+i );
	  i--;
	  //	  cout << "  ERASE" << endl;
	  //	  pr( v );
	}
      }
    }
    int ans=0;
    for( UI i=0;i<v.size();i++ ){
      for( int j=0;j<5;j++ )if( v[i] & 1<<j ) ans++;
    }
    cout << ans << endl;

    //    break;
  }

  return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
using namespace std;

int ad,muki,pos,leng,hight[6],map1[6][6000];

int main(){
  while(true){
    for(int raw=1;raw<6;raw++){
      for(int high=0;high<6000;high++){
	map1[raw][high]=0;
      }
    }
    int blo=0; 
    int x=0;
    cin>>ad;
    if(ad==0){break;}
    for(int i=0;i<ad;i++) {
      for(int j=1;j<6;j++){
	hight[j]=0;}

      cin>>muki;
      cin>>leng;
      cin>>pos;
      blo+=leng;
      for(int raw=1;raw<6;raw++){
	for(int high=0;high<6000;high++){
	  if(map1[raw][high]==1){
	    hight[raw]=max(hight[raw],high);
	  }
	}
	if(map1[raw][hight[raw]]==1){
	  hight[raw]++;
	}
	///cout<<hight[raw]<<" ";
      }
      ///cout<<endl;
      if(muki==2){
	for(int hhhh=0;hhhh<leng;hhhh++){
	  map1[pos][hight[pos]+hhhh]=1;
	}
      }
      if(muki==1){
	int hamu=0;
	for(int raw=pos;raw<pos+leng;raw++){
	  hamu=max(hight[raw],hamu);
	}
	for(int raw=pos;raw<pos+leng;raw++){
	  map1[raw][hamu]=1;
	}
      }/*
      for(int high=0;high<10;high++){
	for(int raw=1;raw<6;raw++){
	  cout<<map1[raw][high]<<" ";
	}
	cout<<endl;
      }
      cout<<"-----------------------"<<endl;
       *///kesi
      for(int high=0;high<6000;high++){
	int hantei=0;
	for(int raw=1;raw<6;raw++){
	  if(map1[raw][high]==0){break;}
	  else{hantei++;}
	}
	if(hantei==5){
	  x++;
	  for(int j=1;j<6;j++){
	    hight[j]--;}
	  for(int a=1;a<6000-high;a++){
	    for(int raw=1;raw<6;raw++){
	      map1[raw][high+a-1]=map1[raw][high+a];
	    }
	  }
	  high--;
	}
      }
    }
    int xxx=0;
    xxx=blo-(5*x);
    cout<<xxx<<endl;
  }
}

C++: #include<stdio.h>
#include<string.h>
#define max(a,b) (a>b?a:b)
char B[15005][5];
int N,d,p,q,res;


int cnt(int y,int x,int c)
{
	int t=0;
	for(int i=0;i<c;i++)
		t+=B[y][x+i];
	return t;
}

void dbg()
{
	for(int i=0;;i++,puts(""))
	{
		int f=cnt(i,0,5);
		if(!f)break;
		for(int j=0;j<5;j++)
			putchar(B[i][j]?'x':'o');
	}
	puts("");
}


int sch(int h,int w,int p,int y)
{
	int i;
	for(i=y;i>=0;)
	{
		int l=cnt(i,p,w);
		if(!l)
		{
			if(i&&cnt(i-1,p,w)==0)
				i--;
			else
				return i;
		}
		else
		{
			if(cnt(i+1,p,w)==0)
				i++;
			else
				return i;
		}
	}
	return i;
}

void Tset(int y,int x,int h,int w)
{
	for(int i=0;i<h;i++)
		for(int j=0;j<w;j++)
			B[y+i][x+j]=1;
}

void flsh()
{
	for(int i=0;;)
	{
		int j,f=cnt(i,0,5);
		if(!f)break;
		if(f==5)
		{
			int tmp=sizeof(char)*(6000*5-i*(5+1));
			memmove(B[i],B[i+1],sizeof(char)*25000);
		}
		else
			i++;
	}
}

int r()
{
	int res=0;
	for(int i=0;;i++)
	{
		int t=cnt(i,0,5);
		if(t==0)break;
		else if(t==5)continue;
		else res+=t;
	}
	return res;
}

int main()
{
	for(;scanf("%d",&N),N;)
	{
		int h=0,t;
		res=0;
		memset(B,0,sizeof(B));
		for(int i=0;i<N;i++)
		{
			scanf("%d%d%d",&d,&p,&q);
			res+=p;
			q--;
			t=sch(d==1?1:p,d==1?p:1,q,h);
			Tset(t,q,d==1?1:p,d==1?p:1);
			if(d==2)t+=p;
			else t++;
			h=max(t,h);
			flsh();
		}
		printf("%d\n",r());
	}
}