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 <bits/stdc++.h>

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = std::int64_t;

std::list<int> board;

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    while(true){
        int N;
        std::cin >> N;

        if(N == 0){
            break;
        }

        board.clear();

        for(int i=0;i<N;++i){
            int D, P, Q;
            std::cin >> D >> P >> Q;

            Q -= 1;

            if(D == 1){
                if(P == 5){
                    continue;
                }

                int block = ((1 << P) - 1) << Q;

                auto it = board.end();
                while(it != board.begin() &&
                      (*std::prev(it) & block) == 0){
                    std::advance(it, -1);
                }

                if(it == board.end()){
                    board.push_back(block);
                }else{
                    *it |= block;

                    if(*it == 31){
                        board.erase(it);
                    }
                }
            }else{
                auto it = board.end();
                while(it != board.begin() &&
                      (*std::prev(it) >> Q & 1) == 0){
                    std::advance(it, -1);
                }

                for(int i=0;i<P;++i){
                    if(it == board.end()){
                        board.push_back(1 << Q);
                    }else{
                        *it |= 1 << Q;
                        if(*it == 31){
                            it = board.erase(it);
                        }else{
                            std::advance(it, 1);
                        }
                    }
                }
            }
        }

        int cnt = 0;
        
        for(int l : board){
            cnt += __builtin_popcount(l);
        }

        std::cout << cnt << std::endl;
    }
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)

const int N = 6000;
bool b[N][5];

void all_fall(int y){
  rep(j,5)b[y][j]=false;
  REP(i,y,N-1){
    rep(j,5)b[i][j]=b[i+1][j];
  }
  rep(j,5)b[N-1][j]=false;
}

int getmax(int l,int r){
  int ret=0;
  REP(j,l,r){    
    int tmp = N-1;
    for(int i=N-1;i>=0;i--){
      if (b[i][j])break;
      tmp=i;
    }
    ret=max(ret,tmp);
  }
  return ret;
}

void fall(){
  rep(i,N){
    if (b[i][0]&b[i][1]&b[i][2]&b[i][3]&b[i][4]){
      all_fall(i);
      i--;
    }
  }
}

main(){
  int n;
  while(cin>>n && n){
    rep(i,N)rep(j,5)b[i][j]=false;
    rep(i,n){
      int d,p,q;
      int wid=1,hei=1,pos;
      cin>>d>>p>>q;
      q--;
      if (d == 1){//yoko
	pos = getmax(q,q+p);	
	wid=p;
      }else if (d == 2){//tate
	pos = getmax(q,q+1);
	hei=p;
      }

      rep(i,hei){
	rep(j,wid){
	  b[pos+i][q+j]=true;
	}
      }
      fall();
    }

    cout << count(&b[0][0],&b[N][0],true) << endl;
  }
}

C++: ?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 #include <iostream> using namespace std;   int dir[5010];   void remove(){     int cnt = 0;     for(int i = 0 ; i < 5010 ; i++){         if( dir[i] == 31 ) dir[i] = 0,cnt++;     }     for(int k = 0 ; k <= cnt ; k++)     for(int i = 5009 ; i > 0 ; i--){         if( dir[i] && !dir[i-1] ){             swap(dir[i],dir[i-1]);         }     } }   void tate(int x,int l){     for(int i = 1 ; i <= l ; i++) dir[5010-i] |= (1<<x);           for(int _ = 0 ; _ < 10 ; _++)     for(int i = 5010-l+_ ; i > 0 ; i--){         if( (dir[i] >> x & 1) && (dir[i-1] >> x & 1) == 0 ){             dir[i] -= (1<<x);             dir[i-1] += (1<<x);         }else{             break;         }     }     remove(); }   void yoko(int x,int l){     int mask = ((1<<l)-1) << x;     dir[5009] = mask;     for(int i = 5009 ; i > 0 ; i--){         if( dir[i] && !(dir[i-1] & mask) ){             dir[i] -= mask;             dir[i-1] += mask;         }else{             break;         }     }     remove(); } int main(){     int n;     while(cin >> n && n){         for(int i = 0 ; i <= 5009 ; i++)             dir[i] = 0;                   for(int i = 0 ; i < n ; i++){             int d,l,x;             cin >> d >> l >> x;             x--;             if(d==1){                 yoko(x,l);             }else{                 tate(x,l);             }             int f = 0;             if( i == 10 ) continue;             for(    int j = 5009 ; j >= 0 ; j--){                 if(f||dir[j]){                     //cout << j << " " << (dir[j]&1) << (dir[j]&2) << (dir[j]&4) << (dir[j]&8) << (dir[j]&16) << endl; ;                     f=true;                 }             }         //  cout << endl;         }         int ans = 0;         for(int i = 0 ; i < 5010 ; i++)             ans += __builtin_popcount(dir[i]);         cout << ans << endl;     } }

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int main() {
	int n;
	while (scanf("%d", &n), n) {
		deque<int>E[5];
		rep(i, n) {
			int d, p, q; scanf("%d%d%d", &d, &p, &q); q--;
			if (d == 1) {
				int Max = 0;
				rep(i, p)Max = max(Max, (int)E[q + i].size());
				rep(i, p) {
					int k = E[q + i].size();
					rep(j, Max - k)E[q + i].push_back(0);
					E[q + i].push_back(1);
				}
			}
			else {
				rep(i, p)E[q].push_back(1);
			}
			bool update = true;
			while (update) {
				update = false;
				int Min = INT_MAX;
				rep(i, 5)Min = min(Min, (int)E[i].size());
				rep(i, Min) {
					bool ok = true;
					rep(j, 5)ok &= E[j][i];
					if (ok) {
						update = true;
						rep(j, 5)E[j][i] = -1;
					}
				}
				rep(i, 5)E[i].erase(remove(E[i].begin(), E[i].end(), -1), E[i].end());
			}
		}
		int sum = 0;
		rep(i, 5)rep(j, E[i].size()) {
			if (E[i][j])sum++;
		}
		printf("%d\n", sum);
	}
}