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 <cstdint>
#include <vector>
#include <algorithm>
#include <numeric>
#include <utility>
#include <tuple>

int const yoko = 1;
int const tate = 2;

void tetris(std::vector<std::vector<int>>& z) {
  size_t m = z[0].size();
  std::vector<std::vector<int>> y(5, std::vector<int>(m));

  size_t i = 0;
  for (size_t j = 0; j < m; ++j) {
    int cur = 0;
    for (size_t k = 0; k < 5; ++k) cur += z[k][j];
    if (cur == 5) continue;
    for (size_t k = 0; k < 5; ++k) y[k][i] = z[k][j];
    ++i;
  }
  z = std::move(y);
}

int testcase_ends() {
  size_t n;
  scanf("%zu", &n);
  if (n == 0) return 1;

  size_t m = 5*n+10;
  std::vector<std::vector<int>> z(5, std::vector<int>(m));
  for (size_t i = 0; i < n; ++i) {
    size_t d, p, q;
    scanf("%zu %zu %zu", &d, &p, &q);
    --q;

    size_t l = q;
    size_t r = q;
    if (d == yoko) r += p-1;

    size_t h = 0;
    for (size_t j = l; j <= r; ++j) {
      for (size_t k = m; k--;) {
        if (z[j][k]) {
          h = std::max(h, k+1);
          break;
        }
      }
    }

    if (d == yoko) {
      for (size_t i = l; i <= r; ++i) ++z[i][h];
    } else if (d == tate) {
      for (size_t i = 0; i < p; ++i) ++z[q][h+i];
    }

    tetris(z);
  }
  int res = 0;
  for (auto const& zi: z)
    res += std::accumulate(zi.begin(), zi.end(), 0);

  printf("%d\n", res);
  return 0;
}

int main() {
  while (!testcase_ends()) {}
}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int dat[5000][5];
int m[5];
int main(){
	int a;
	while(scanf("%d",&a),a){
		for(int i=0;i<5000;i++)
			for(int j=0;j<5;j++)dat[i][j]=0;
		for(int i=0;i<5;i++)m[i]=0;
		int count=0;
		for(int i=0;i<a;i++){
			int b,c,d;
			scanf("%d%d%d",&b,&c,&d);d--;
			count+=c;
			if(b==2){
				for(int i=0;i<c;i++){
					dat[m[d]+i][d]=1;
					bool ok=true;
					for(int j=0;j<5;j++)if(!dat[m[d]+i][j])ok=false;
					if(ok){
						for(int j=0;j<5;j++){
							dat[m[d]+i][j]=0;
							for(int k=m[d]+i;k<4999;k++)dat[k][j]=dat[k+1][j];
							m[j]--;
						}
						count-=5;
					}
				//	m[d]++;
				}
				m[d]+=c;
			}else{
				int h=0;
				for(int i=0;i<c;i++)h=max(h,m[i+d]);
				for(int i=0;i<c;i++)dat[h][i+d]=1;
				bool ok=true;
				for(int j=0;j<5;j++)if(!dat[h][j])ok=false;
				if(ok){
					for(int j=0;j<5;j++){
						dat[h][j]=0;
						for(int k=h;k<4999;k++)dat[k][j]=dat[k+1][j];
						m[j]--;
					}
					count-=5;
				}
				for(int i=0;i<c;i++)m[i+d]=h+1;
			}
		}
		printf("%d\n",count);
	}
}

C++: #include <cstdio>
#include <ctime>
using namespace std;

int main(){
	clock_t c1 = clock();
	int s = 0, n;
	while(scanf("%d", &n), n){
		s += n;
		scanf("%*d%*d%*d");
	}
	
	clock_t c2 = c1 + CLOCKS_PER_SEC * (s / 1000000.0);
	while(clock() < c2);
}

C++: #include <iostream>
#include <vector>
using namespace std;

class TETORIS {
public:
    int N;
    vector<vector<int> > bd;
    TETORIS() : N(0) {}
    void put(int len, int pos) {
        int k;
        for (k=bd.size()-1; k>=0; --k) {
            for (int i=0; i<len; ++i) {
                if (bd[k][pos+i]) goto brk;
            }
        } brk:;
        ++k;
        if ((int)bd.size() == k) bd.push_back(vector<int>(5, 0));
        for (int i=0; i<len; ++i) {
            bd[k][pos+i] = 1;
        }
        N += len;
    }
    void update() {
        for (int i=bd.size()-1; i>=0; --i) {
            if (bd[i][0] && bd[i][1] && bd[i][2] && bd[i][3] && bd[i][4]) {
                bd.erase(bd.begin()+i);
                N -= 5;
            }
        }
    }
};

int main() {
    int n;
    while (cin >> n, n) {
        TETORIS tet;
        int d, p, q;
        for (int i=0; i<n; ++i) {
            cin >> d >> p >> q;
            --q;
            if (d == 1) {
                tet.put(p, q);
                tet.update();
            } else {
                for (int j=0; j<p; ++j) tet.put(1, q);
                tet.update();
            }
        }
        cout << tet.N << endl;
    }
    return 0;
}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int dat[5000][5];
int m[5];
int main(){
	int a;
	while(scanf("%d",&a),a){
		for(int i=0;i<5000;i++)
			for(int j=0;j<5;j++)dat[i][j]=0;
		for(int i=0;i<5;i++)m[i]=0;
		int count=0;
		for(int p=0;p<a;p++){
			int b,c,d;
			scanf("%d%d%d",&b,&c,&d);d--;
			count+=c;
			if(b==2){
				for(int i=0;i<c;i++){
					dat[m[d]][d]=1;
					bool ok=true;
					for(int j=0;j<5;j++)if(!dat[m[d]][j])ok=false;
					if(ok){
						for(int j=0;j<5;j++){
							dat[m[d]][j]=0;
							for(int k=m[d];k<4999;k++)dat[k][j]=dat[k+1][j];
							for(int k=4999;k>=0;k--){
								if(dat[k][j]){
									m[j]=k+1;break;}
								if(k==0)m[j]=0;
							}
						}
						count-=5;
					}else m[d]++;
				//	m[d]++;
				}
				//m[d]+=c;
			}else{
				int h=0;
				for(int i=0;i<c;i++)h=max(h,m[i+d]);
				for(int i=0;i<c;i++)dat[h][i+d]=1;
				for(int i=0;i<c;i++)m[i+d]=h+1;
				bool ok=true;
				for(int j=0;j<5;j++)if(!dat[h][j])ok=false;
				if(ok){
					for(int j=0;j<5;j++){
						dat[h][j]=0;
						for(int k=h;k<4999;k++)dat[k][j]=dat[k+1][j];
						for(int k=4999;k>=0;k--){
							if(dat[k][j]){m[j]=k+1;break;}
							if(k==0)m[j]=0;
						}
					}
					count-=5;
				}
			}
	//		for(int i=6;i>=0;i--,printf("\n"))for(int j=0;j<5;j++)printf("%d",dat[i][j]);
	//		printf("\n");
		}
		printf("%d\n",count);
	}
}