Problem:
テトリス
テトリスは、落ちてくるブロックを盤面上に並べて消すゲームです。ここでは、それを少しアレンジしたゲームを考えましょう。
このゲームの盤面の大きさは横 5 コマで、出現するブロックがすべて入るだけの高さがあります。落ちてくるブロックは直線状で、横向き、縦向きの 2 種類があり、長さは 1 から 5 までの 5 種類です。
以下に例を示します。Step(イ) からStep(ホ)までの図はブロックが落ちて消えていく様子を表したものです。
Step(イ)から順にStep(ロ)、Step(ハ)と順に進んでいきます。
ブロックを落とすときに、Step(イ)のようにブロックのどこかが下に積んであるブロックに引っかかったときには、Step(ロ)のように落ちたブロックはその場所で止まります。また、ブロックを落とした結果、盤面の横一行の全てのコマにブロックが詰まった場合には、Step(ニ)で示されるように、その行のブロックが消えます。この後、消えた行の上にあるブロックが、そのままの形で1行下にしずみます(Step(ホ))。
1 ゲームは 1000 個以内のブロックが順に落ちてくることとします。例えば、落ちるブロックの長さが順番に横向き 4 コマ, 横向き 3 コマ, 縦向き 2 コマ, 縦向き 3 コマで、落ちる場所が左端から 1 コマ目、1 コマ目、4 コマ目、5 コマ目であった場合には、下図のStep(イ)~(ト)のように落ち、最後に残るブロックは 2 コマになります。
順番に落ちてくるブロックの情報を入力とし、全てのブロックが落ちた時に残るコマ数を出力するプログラムを作成してください。
Input
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。各データセットは以下の形式で与えられます。
n
d
1
p
1
q
1
d
2
p
2
q
2
:
d
n
p
n
q
n
１行目にブロックの数
n
(1 ≤
n
≤ 1000) が与えられます。続く
n
行に
i
個目のブロックの向き
d
i
(1 または2)、ブロックの長さ
p
i
(1 ≤
p
i
≤ 5)、ブロックの位置
q
i
が与えられます。ブロックの向き
d
i
は 1 が横向きを、2 が縦向きを表します。ブロックの位置
q
i
は盤面上の左端から1 ~ 5 までの整数とし、横向きのブロックの場合は左端のコマの落ちる位置とします。
データセットの数は 20 を超えません。
Output
データセット毎に最後に残るブロックの占めるコマ数を１行に出力します。
Sample Input
4
1 4 1
1 3 1
2 2 4
2 3 5
1
1 5 1
7
2 2 2
1 4 1
2 1 3
1 4 1
1 1 1
2 5 5
1 4 2
0
Output for the Sample Input
2
0
6

Submitted solutions:

code_snippet: #line 1 "/opt/library/template.hpp"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'001'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
#define inf infty<ll>

using pi = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

#define rep1(a) for (ll _ = 0; _ < (ll)(a); ++_)
#define rep2(i, a) for (ll i = 0; i < (ll)(a); ++i)
#define rep3(i, a, b) for (ll i = a; i < (ll)(b); ++i)
#define rep4(i, a, b, c) for (ll i = a; i < (ll)(b); i += (c))
#define rrep1(a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep2(i, a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep3(i, a, b) for (ll i = (b)-1; i >= (ll)(a); --i)
#define rrep4(i, a, b, c) for (ll i = (b)-1; i >= (ll)(a); i -= (c))
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)

#define all(x) (x).begin(),(x).end()
#define len(x) (ll)(x.size())
#define elif else if
#define bit(x, i) (((x)>>(i))&1)

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll
#define abs llabs

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
  sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

ll popcnt(ll x) { return __builtin_popcountll(x); }
ll popcnt(u64 x) { return __builtin_popcountll(x); }
ll popcnt_mod_2(ll x) { return __builtin_parityll(x); }
ll popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
ll topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
ll lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template<typename T> T SUM(const vector<T> &A) {
  T s = 0; for (auto &&a: A) s += a;
  return s;
}

template <typename T>
T POP(queue<T> &que) {
  T a = que.front();
  que.pop();
  return a;
}
template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <typename T>
T POP(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(pqg<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    (check(x) ? ok : ng) = x;
  }
  return ok;
}
template <typename F>
f128 binary_search_real(F check, f128 ok, f128 ng, ll iter = 100) {
  rep(iter) {
    f128 x = (ok + ng) / 2;
    (check(x) ? ok : ng) = x;
  }
  return (ok + ng) / 2;
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  rep(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vc<T> cumsum(vc<U> &A, ll off = 1) {
  ll N = A.size();
  vc<T> B(N + 1);
  rep(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vi argsort(const vector<T> &A) {
  vi ids(len(A));
  iota(all(ids), 0);
  sort(all(ids),
       [&](ll i, ll j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vi &I) {
  vc<T> B(len(I));
  rep(i, len(I)) B[i] = A[I[i]];
  return B;
}

template<typename T> inline bool chmax(T &a, T b) {return ((a<b)?(a=b,true):(false));}
template<typename T> inline bool chmin(T &a, T b) {return ((a>b)?(a=b,true):(false));}

inline void wt(const char c) { cout << c; }
inline void wt(const string s) { cout << s; }
inline void wt(const char *s) { cout << s; }

template <typename T>
void wt_integer(T x) {
  cout << (x);
}
template <typename T>
void wt_real(T x) {
  cout << fixed << setprecision(15) << (long double)(x);
}
template <typename T>
void wt_integer128(T x) {
  char buf[64];
  char *d = end(buf);
  d--; *d = '\0';
  __uint128_t tmp = ((x < 0)? -x : x);
  do {
    d--; *d = char(tmp%10 + '0'); tmp /= 10;
  } while (tmp);
  if (x < 0) {
    d--; *d = '-';
  }
  cout << d;
}

inline void wt(int x) { wt_integer(x); }
inline void wt(ll x) { wt_integer(x); }
inline void wt(i128 x) { wt_integer128(x); }
inline void wt(u32 x) { wt_integer(x); }
inline void wt(u64 x) { wt_integer(x); }
inline void wt(u128 x) { wt_integer128(x); }
inline void wt(double x) { wt_real(x); }
inline void wt(long double x) { wt_real(x); }
inline void wt(f128 x) { wt_real(x); }

template <class T, class U>
void wt(const pair<T, U> val) {
  wt(val.first); wt(' '); wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) { wt(' '); }
    const auto x = std::get<N>(t);
    wt(x);
    wt_tuple<N + 1>(t);
  }
}
template <class... T>
void wt(tuple<T...> tpl) {
  wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}
template <class T>
void wt(const vector<T> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}

void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  wt(head);
  if (sizeof...(Tail)) wt(' ');
  print(forward<Tail>(tail)...);
}

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
void onez(bool t = 1) { print(t ? 1 : 0); }
#define endl '\n'
#define dump(x) {cerr << #x " = " << x << '\n';}
#line 2 "main.cpp"

int solve();
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  while (!solve()) {}
  return 0;
}

int solve() {
  ll n;
  cin >> n;
  if (n == 0) return 1;
  vi p(n), q(n), d(n);
  rep(i, n) {
    cin >> d[i] >> p[i] >> q[i];
    q[i]--;
  }
  vv(ll, b, n*5+2, 5, 0);
  rep(j, 5) b[0][j] = 1;
  vc<bool> removed(len(b), false);
  rep(k, n) {
    ll prev = len(b)-1;
    bool found = false;
    rrep(i, len(b)-1) {
      if (removed[i]) continue;
      if (d[k] == 1) {
        rep(j, q[k], q[k]+p[k]) {
          if (b[i][j] == 1) {
            rep(l, q[k], q[k]+p[k]) {
              b[prev][l] = 1;
            }
            found = true;
            break;
          }
        }
      } else {
        if (b[i][q[k]] == 1) {
          ll cnt = 0;
          rep(l, prev, len(b)) {
            if (removed[l]) continue;
            b[l][q[k]] = 1;
            if (++cnt >= p[k]) break;
          }
          found = true;
          break;
        }
      }
      if (found) break;
      prev = i;
    }
    ll cnt = 0;
    rep(l, prev, len(b)) {
      if (removed[l]) continue;
      if (SUM(b[l]) == 5) {
        removed[l] = true;
      }
      if (++cnt >= p[k]) break;
    }
  }
  ll ans = 0;
  rep(i, 1, len(b)) {
    if (removed[i]) continue;
    ans += SUM(b[i]);
  }
  print(ans);
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3764
final_rank: 11

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    while (cin >> n, n) {
        deque<char> a[5];
        int sum = 0;
        for (int i = 0; i < n; ++i) {
            int d, p, q;
            cin >> d >> p >> q;
            sum += p, q -= 1;
            if (d == 2) {
                for (int j = 0; j < p; ++j) {
                    a[q].push_back('#');
                }
            } else {
                int mx = 0;
                for (int j = 0; j < p; ++j) {
                    mx = max(mx, (int)a[q + j].size());
                }
                for (int j = 0; j < p; ++j) {
                    while (a[q + j].size() < mx) {
                        a[q + j].push_back('.');
                    }
                    a[q + j].push_back('#');
                }
            }
            //            for (int j = 0; j < 5; ++j) {
            //                for (int k = 0; k < a[j].size(); ++k) {
            //                    cout << a[j][k];
            //                }
            //                cout << endl;
            //            }
            deque<char> na[5];
            while (true) {
                bool emp = true;
                bool ers = true;
                for (int j = 0; j < 5; ++j) {
                    if (a[j].size() == 0) {
                        ers = false;
                        continue;
                    }
                    emp = false;
                    na[j].push_back(a[j][0]);
                    if (a[j][0] != '#') ers = false;
                    a[j].pop_front();
                }
                if (emp) break;
                if (ers) {
                    sum -= 5;
                    for (int j = 0; j < 5; ++j) {
                        na[j].pop_back();
                    }
                }
            }
            swap(a, na);
            for (int j = 0; j < 5; ++j) {
                while (a[j].size() and a[j].back() == '.') {
                    a[j].pop_back();
                }
            }
            //            for (int j = 0; j < 5; ++j) {
            //                for (int k = 0; k < a[j].size(); ++k) {
            //                    cout << a[j][k];
            //                }
            //                cout << endl;
            //            }
            //            cout << endl;
        }
        cout << sum << "\n";
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3476
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    int n;
    while(cin >> n, n){
        int d, p, q;
        vector<array<int,5>> A;
        array<int,5> E{};
        while(n--){
            cin >> d >> p >> q;
            q--;
            for(int i = 0; i < 5; i++)A.push_back(E);
            int h = A.size() - 1;
            while(h >= 0){
                if(d == 2){
                    if(A[h][q])break;
                }else{
                    bool flag = false;
                    for(int j = 0; j < p; j++){
                        if(A[h][j + q])flag = true;
                    }
                    if(flag)break;
                }
                h--;
            }
            h++;
            if(d == 2){
                for(int j = 0; j < p; j++){
                    A[h + j][q] = 1;
                }
            }else{
                for(int j = 0; j < p; j++){
                    A[h][q + j] = 1;
                }
            }
            vector<array<int,5>> B;
            for(int i = 0; i < A.size(); i++){
                int cnt = count(A[i].begin(), A[i].end(), 0);
                if(cnt == 0 || cnt == 5)continue;
                B.push_back(A[i]);
            }
            swap(A, B);
        }
        int ans = 0;
        for(int i = 0; i < A.size(); i++){
            ans += count(A[i].begin(), A[i].end(), 1);
        }
        cout << ans << '\n';
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3572
final_rank: 9

code_snippet: #define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<iomanip>
#include<regex>
#include<sstream>
#include<random>
using namespace std;
#define int long long
#define uint unsigned long long
const int mod=1e9+7;
const int inf=mod*mod;
signed main(){
	int n;
	while(cin>>n,n){
		int d,p,q;
		bool mp[5000][5]={};
		for(int i=0;i<n;i++){
			cin>>d>>p>>q;
			q--;
			for(int j=4999;j>=0;j--){
				bool ok=0;
				for(int k=q;k<q+(d==1?p:1);k++)
					if(j==0||mp[j-1][k])
						ok=1;
				if(ok){
					for(int k=j+(d==2?p:1)-1;k>=j;k--){
						for(int l=q;l<q+(d==1?p:1);l++)
							mp[k][l]=1;
						bool ok=1;
						for(int l=0;l<5;l++)
							if(!mp[k][l])
								ok=0;
						if(ok){
							for(int l=k;l<4999;l++)
								for(int m=0;m<5;m++)
									mp[l][m]=mp[l+1][m];
							for(int l=0;l<5;l++)
								mp[4999][l]=0;
						}
					}
					break;
				}
			}
		}
		int ans=0;
		for(int i=0;i<5000;i++)
			for(int j=0;j<5;j++)
				if(mp[i][j])
					ans++;
		cout<<ans<<endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3380
final_rank: 4

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

void solve(int n){
    vector<vector<int>> grid(5, vector<int>(5*n+1));
    for (int i = 0; i < 5; ++i) grid[i].back() = n+1;
    vector<int> alive(5*n+1);
    vector<int> cnt(5*n+1);
    iota(alive.begin(),alive.end(), 0);
    stack<int> X;
    for (int _ = 0; _ < n; ++_) {
        int d, p, q;
        cin >> d >> p >> q; q--;
        if(d == 1){
            for (int i = 1; i < grid[0].size(); ++i) {
                int ok = 1;
                for (int j = q; j < q+p; ++j) {
                    if(grid[j][alive[i]]) ok = 0;
                }
                if(!ok){
                    for (int j = q; j < q+p; ++j) {
                        int a = alive[i-1];
                        grid[j][a] = _+1;
                        cnt[a]++;
                        if(cnt[a] == 5) X.emplace(a);
                    }
                    break;
                }
            }
        }else {
            for (int i = 1; i+p-1 < grid[0].size(); ++i) {
                int ok = 1;
                for (int j = i; j < i+p; ++j) {
                    if(grid[q][alive[j]]) ok = 0;
                }
                if(!ok){
                    for (int j = i; j < i+p; ++j) {
                        int a = alive[j-1];
                        grid[q][a] = _+1;
                        cnt[a]++;
                        if(cnt[a] == 5) X.emplace(a);
                    }
                    break;
                }
            }
        }
        while(!X.empty()){
            alive.erase(lower_bound(alive.begin(),alive.end(), X.top())); X.pop();
        }
    }
    int ans = 0;
    for (auto &&j : alive) {
        for (int i = 0; i < 5; ++i) {
            if(grid[i][j] && grid[i][j] <= n) ans++;
        }
    }
    cout << ans << "\n";
}

int main() {
    int n;
    while(cin >> n, n){
        solve(n);
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3196
final_rank: 2

code_snippet: #include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

int MapData[5001][6];
int N;


void solveHori(int len, int pos) {
	bool findFirstSquare;

	for (int row = 5000; row > -1; --row) {
		findFirstSquare = false;
		for (int j = 0; j < len; ++j) {
			if (MapData[row][pos+j] == 1) {
				findFirstSquare = true;
				break;
			}
		}
		if (!findFirstSquare) {
			continue;
		}
		for (int j = 0; j < len; ++j) {
			MapData[row+1][pos + j] = 1;
		}
		return;
	}
	for (int j = 0; j < len; ++j) {
		MapData[0][pos + j] = 1;
	}
}

void solveVert(int len, int pos) {
	bool findFirstSquare;

	for (int row = 5000; row > -1; --row) {
		findFirstSquare = false;
		if (MapData[row][pos] == 1) {
			findFirstSquare = true;
		}
		if (!findFirstSquare) {
			continue;
		}
		for (int j = 0; j < len; ++j) {
			MapData[row + 1 + j][pos] = 1;
		}
		return;
	}
	for (int j = 0; j < len; ++j) {
		MapData[j][pos] = 1;
	}
}

void modifyMapData(int row) {

	for (int i = row; i < 5000; i++) {
		for (int j = 1; j < 6; j++) {
			MapData[i][j] = MapData[i + 1][j];
		}
	}
	
}

void earseMapdata() {
	int cntZero, cntOne;
	
	for (int row = 0; row < 5001; ++row) {
		cntZero = cntOne = 0;
		for (int col = 1; col < 6; ++col) {
			if (MapData[row][col] == 1) {
				++cntOne;
			}
			else {
				++cntZero;
			}
		}
		if (cntZero == 5) {
			return;
		}
		if (cntOne == 5) {
			modifyMapData(row);
			--row;
		}
	}
}

int main() {
	int d, p, q, cnt;

	while (cin >> N, N) {
		memset(MapData, 0, sizeof(MapData));
		for (int i = 0; i < N; ++i) {
			cin >> d >> p >> q;

			if (d == 1) {
				//横向き
				solveHori(p, q);
			}
			else if (d == 2) {
				//縦向き
				solveVert(p, q);
			}
			earseMapdata();
		}
		cnt = 0;
		for (int row = 0; row < 5000; ++row) {
			for (int col = 1; col < 6; ++col) {
				if (MapData[row][col] == 1) {
					++cnt;
				}
			}
		}
		cout<<cnt<<endl;
	}

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3188
final_rank: 5

code_snippet: #include<iostream>
using namespace std;
int A[5000][5];
int x[5],y[5];
main()
{
	int N;
	while(cin>>N,N)
	{
		for(int i=0;i<5000;i++)for(int j=0;j<5;j++)A[i][j]=0;
		for(int i=0;i<N;i++)
		{
			int d,p,q;cin>>d>>p>>q;
			if(d==1)
			{
				for(int j=0;j<p;j++)
				{
					x[j]=4999;
					y[j]=q+j-1;
				}
			}
			else
			{
				for(int j=0;j<p;j++)
				{
					x[j]=4999-j;
					y[j]=q-1;
				}
			}
			while(true)
			{
				for(int j=0;j<p;j++)x[j]--;
				bool flag=false;
				for(int j=0;j<p;j++)
				{
					if(x[j]<0||A[x[j]][y[j]])flag=true;
				}
				if(flag)break;
			}
			for(int j=0;j<p;j++)A[++x[j]][y[j]]=1;
			for(int j=0;j<p;j++)
			{
				int X=x[j];
				bool flag=true;
				for(int k=0;k<5;k++)flag&=A[X][k];
				if(flag)
				{
					for(int k=X+1;k<5000;k++)for(int l=0;l<5;l++)A[k-1][l]=A[k][l];
				}
			}
		}
		int ans=0;
		for(int i=0;i<5000;i++)for(int j=0;j<5;j++)ans+=A[i][j];
		cout<<ans<<endl;
	}
}
accuracy: 1
time_ms: 40
memory_kb: 3176
final_rank: 13

code_snippet: #include<iostream>
#include<cstring>
using namespace std;

int di[2] = {0, 1};
int dj[2] = {1, 0};
int mat[5010][5];

int main(){
    int n;
    while(cin >> n, n){
        memset(mat, 0, sizeof(mat));
        int ma = 1;
        for(int i = 0; i < n; i++){
            int d, p, q;
            cin >> d >> p >> q;
            d--, q--;
            int put;
            for(put = 5000; put >= 0; put--){
                bool ok = true;
                for(int j = 0; j < p; j++){
                    ok &= mat[put+j*di[d]][q+j*dj[d]] == 0;
                }
                if(!ok) break;
            }
            int h = put+1;
            for(int j = 0; j < p; j++){
                mat[h+j*di[d]][q+j*dj[d]] = 1;
            }
            int acc = 0;
            for(int hh = h; hh < 5000; hh++){
                int sum = 0;
                for(int j = 0; j < 5; j++)  sum += mat[hh][j];
                if(sum == 5){
                    for(int j = 0; j < 5; j++)  mat[hh][j] = 0;
                    acc++;
                }else{
                    for(int j = 0; j < 5; j++)  mat[hh-acc][j] = mat[hh][j];
                }
            }
        }
        int ans = 0;
        for(int i = 0; i < 5000; i++){
            for(int j = 0; j < 5; j++){
                ans += mat[i][j];
            }
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3164
final_rank: 20

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

int n;
int h[7];
bool m[6010][7];
int b[1010][5];

int main()
{
    while (true)
    {
        cin >> n;
        if (n == 0)
            break;
        for (int i = 0; i < 5500; i++)
        {
            for (int j = 0; j < 7; j++)
                m[i][j] = false;
        }
        for (int i = 0; i <= 6; i++)
        {
            h[i] = 0;
        }
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                cin >> b[i][j];
                if (j == 1)
                {
                    ans += b[i][j];
                }
            }
        }
        for(int i = 0;i < n;i++){
            if(b[i][0] == 1){
                int hmax = h[b[i][2]];
                for(int j = 0;j < b[i][1];j++){
                    hmax = max(hmax,h[b[i][2]+j]);
                }
                hmax++;
                for(int j = 0;j < b[i][1];j++){
                    h[b[i][2]+j] = hmax;
                    m[hmax][b[i][2]+j] = true;
                }
            }else{
                int hmax = h[b[i][2]];
                for(int j = 1;j <= b[i][1];j++){
                    m[hmax+j][b[i][2]] = true;
                }
                h[b[i][2]] = hmax + b[i][1];
            }
            int hmax = h[1];
            for(int j = 1;j <= 5;j++) hmax = max(hmax,h[j]);
            set<int> s;
            for(int j = 1;j <= hmax;j++){
                int count = 0;
                for(int k = 1;k <= 5;k++)if(m[j][k] == true)count++;
                if(count == 5){
                    ans -= 5;
                    s.insert(j);
                }
            }
            int nj = 1;
            for(int j = 1;j <= hmax;j++){
                if( s.find(j) != s.end() )continue;
                for(int k = 1;k <= 5;k++){
                    m[nj][k] = m[j][k];
                }
                nj++;
            }
            for(int j = nj;j < 5050;j++){
                for(int k = 1;k <= 5;k++)m[j][k] = false;
            }
            for(int j = 1;j <= 5;j++){
                h[j] = 0;
                for(int k = hmax;k >= 0;k--){
                    if(m[k][j] == true){
                        h[j] = k;
                        break;
                    }
                }
            }
        }
        int hmax = h[1];
        for(int j = 1;j <= 5;j++) hmax = max(hmax,h[j]);
        for(int j = hmax;j >= 1;j--){
        }
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 30
memory_kb: 3160
final_rank: 6

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

int n;
int h[7];
bool m[6010][7];
int b[1010][5];

int main()
{
    while (true)
    {
        cin >> n;
        if (n == 0)
            break;
        for (int i = 0; i < 5500; i++)
        {
            for (int j = 0; j < 7; j++)
                m[i][j] = false;
        }
        for (int i = 0; i <= 6; i++)
        {
            h[i] = 0;
        }
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                cin >> b[i][j];
                if (j == 1)
                {
                    ans += b[i][j];
                }
            }
        }
        for(int i = 0;i < n;i++){
            if(b[i][0] == 1){
                int hmax = h[b[i][2]];
                for(int j = 0;j < b[i][1];j++){
                    hmax = max(hmax,h[b[i][2]+j]);
                }
                hmax++;
                for(int j = 0;j < b[i][1];j++){
                    h[b[i][2]+j] = hmax;
                    m[hmax][b[i][2]+j] = true;
                }
            }else{
                int hmax = h[b[i][2]];
                for(int j = 1;j <= b[i][1];j++){
                    m[hmax+j][b[i][2]] = true;
                }
                h[b[i][2]] = hmax + b[i][1];
            }
            int hmax = h[1];
            for(int j = 1;j <= 5;j++) hmax = max(hmax,h[j]);
            set<int> s;
            for(int j = 1;j <= hmax;j++){
                int count = 0;
                for(int k = 1;k <= 5;k++)if(m[j][k] == true)count++;
                if(count == 5){
                    ans -= 5;
                    s.insert(j);
                }
            }
            int nj = 1;
            for(int j = 1;j <= hmax;j++){
                if( s.find(j) != s.end() )continue;
                for(int k = 1;k <= 5;k++){
                    m[nj][k] = m[j][k];
                }
                nj++;
            }
            for(int j = nj;j < 5050;j++){
                for(int k = 1;k <= 5;k++)m[j][k] = false;
            }
            for(int j = 1;j <= 5;j++){
                h[j] = 0;
                for(int k = hmax;k >= 0;k--){
                    if(m[k][j] == true){
                        h[j] = k;
                        break;
                    }
                }
            }
        }
        int hmax = h[1];
        for(int j = 1;j <= 5;j++) hmax = max(hmax,h[j]);
        for(int j = hmax;j >= 1;j--){
        }

 
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 30
memory_kb: 3164
final_rank: 8

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

int n;
int h[7];
bool m[6010][7];
int b[1010][5];

int main()
{
    while (true)
    {
        cin >> n;
        if (n == 0)
            break;
        for (int i = 0; i < 5500; i++)
        {
            for (int j = 0; j < 7; j++)
                m[i][j] = false;
        }
        for (int i = 0; i <= 6; i++)
        {
            h[i] = 0;
        }
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                cin >> b[i][j];
                if (j == 1)
                {
                    ans += b[i][j];
                }
            }
        }
        for(int i = 0;i < n;i++){
            if(b[i][0] == 1){
                int hmax = h[b[i][2]];
                for(int j = 0;j < b[i][1];j++){
                    hmax = max(hmax,h[b[i][2]+j]);
                }
                hmax++;
                for(int j = 0;j < b[i][1];j++){
                    h[b[i][2]+j] = hmax;
                    m[hmax][b[i][2]+j] = true;
                }
            }else{
                int hmax = h[b[i][2]];
                for(int j = 1;j <= b[i][1];j++){
                    m[hmax+j][b[i][2]] = true;
                }
                h[b[i][2]] = hmax + b[i][1];
            }
            int hmax = h[1];
            for(int j = 1;j <= 5;j++) hmax = max(hmax,h[j]);
            set<int> s;
            for(int j = 1;j <= hmax;j++){
                int count = 0;
                for(int k = 1;k <= 5;k++)if(m[j][k] == true)count++;
                if(count == 5){
                    ans -= 5;
                    s.insert(j);
                }
            }
            int nj = 1;
            for(int j = 1;j <= hmax;j++){
                if( s.find(j) != s.end() )continue;
                for(int k = 1;k <= 5;k++){
                    m[nj][k] = m[j][k];
                }
                nj++;
            }
            for(int j = nj;j < 5050;j++){
                for(int k = 1;k <= 5;k++)m[j][k] = false;
            }
            for(int j = 1;j <= 5;j++){
                h[j] = 0;
                for(int k = hmax;k >= 0;k--){
                    if(m[k][j] == true){
                        h[j] = k;
                        break;
                    }
                }
            }
            for(int j = hmax;j >= 1;j--){
                for(int k = 1;k <= 5;k++){
            //        cout << m[j][k];
                }
            //    cout << endl;
            }
            for(int j = 1;j <= 5;j++){
            //    cout << h[j];
            }
        //    cout << endl << "*******" << endl;
        }
        int hmax = h[1];
        for(int j = 1;j <= 5;j++) hmax = max(hmax,h[j]);
        for(int j = hmax;j >= 1;j--){
        //    for(int k = 1;k <= 5;k++)cout << m[j][k];
        //    cout << endl;
        }
        /* 
        int hmax = 0;
        for(int i = 1;i <= 5;i++)hmax = max(hmax,h[i]);
        for(int i = 1;i <= hmax;i++){
            int count = 0;
            for(int j = 1;j <= 5;j++){
      //          if(m[hmax-i+1][j] == true)cout << 1 ;
       //         else cout << 0;
                if(m[i][j] == true)count++;
            }
       //     cout << endl;
            if(count == 5){
                ans -= 5;
            }
        }
        */
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 30
memory_kb: 3160
final_rank: 6

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <utility>
#include <tuple>
#include <math.h>
#include <iomanip>

using namespace std;

class Sover {
};

int main()
{
	while (true) {
		int n;
		cin >> n;
		if (n == 0)break;

		int length = n * 5 + 10;
		vector<vector<bool>> grid(length, vector<bool>(5, false));

		for (int i = 0; i < n; i++) {
			int d, p, q;
			cin >> d >> p >> q;
			q--;
			int bottom = length - 1;
			if (d == 1) {
				for (; bottom >= 0; bottom--) {
					bool interrupt = false;
					for (int j = 0; j < p; j++) {
						if (grid[bottom][q+j])interrupt = true;
					}
					if (interrupt)break;
				}

				for (int j = 0; j < p; j++)grid[bottom + 1][q + j] = true;
			}
			else {
				while (bottom >= 0 && !grid[bottom][q])bottom--;

				for (int j = 0; j < p; j++)grid[bottom + 1 + j][q] = true;
			}

			int shift_cnt = 0;
			for (int j = bottom + 1; j < length; j++) {
				bool full = true;
				for (int k = 0; k < 5; k++) {
					if (!grid[j][k]) {
						full = false;
						break;
					}
				}
				if (full) {
					shift_cnt++;
					for (int k = 0; k < 5; k++) {
						grid[j][k] = false;
					}
				}
				else {
					for (int k = 0; k < 5; k++) {
						grid[j - shift_cnt][k] = grid[j][k];
						if (shift_cnt > 0)grid[j][k] = false;
					}
				}
			}
		}

		int res = 0;
		for (int i = 0; i < length; i++) {
			for (int j = 0; j < 5; j++)if (grid[i][j])res++;
		}
		cout << res << endl;
	}
}
accuracy: 1
time_ms: 60
memory_kb: 3136
final_rank: 19

code_snippet: #include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;
const int MOD = 1000000007;

int di[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dj[] = {0, 1, 0, -1, 1, 1, -1, -1};
    
int g[5100][5];

signed main(){
    
    while(1){
    memset(g, 0, sizeof(g));
    int n; cin >> n;
    if(!n) break;
    int sum = 0;
    for(int j = 0; j < 5; j++){
        g[0][j] = 1;
    }


    for(int i = 0; i < n; i++){

        int a, p, q; cin >> a >> p >> q;
        swap(p, q);
        p--;
        sum += q;
        if(a == 1){


            // 落とす
            int y;
            for(int i = 5000; i >= 1; i--){
                bool flag = false;
                for(int j = p; j < p + q; j++){
                    if(g[i - 1][j] == 1){
                        flag = true;
                        y = i;        
                        break;
                    }
                }
                if(flag) break;
            }

            for(int j = p; j < p + q; j++){
                g[y][j] = 1;
            }

            // 消えるかどうか
            bool erase = true;
            for(int j = 0; j < 5; j++){
                if(!g[y][j]) erase = false;
            }

            if(erase){
                sum -= 5;
                for(int i = y + 1; i <= 5000; i++){
                    for(int j = 0; j < 5; j++){
                        g[i - 1][j] = g[i][j];
                        g[i][j] = 0;
                    }
                }
            }

        }else{
            

            // 落とす
            int y = 0;
            for(int i = 5000; i >= 0; i--){
                if(g[i - q][p]){
                    y = i;
                    break;
                }
            }

            for(int i = y - q + 1; i <= y; i++) g[i][p] = 1;

            // 消えるかどうか
            y = y - q + 1;
            for(int itr = 0; itr < 5; itr++){
                bool erase = true;
                for(int j = 0; j < 5; j++){
                    if(!g[y][j]) erase = false;
                }

                if(erase){
                    sum -= 5;
                    for(int i = y + 1; i <= 5000; i++){
                        for(int j = 0; j < 5; j++){
                            g[i - 1][j] = g[i][j];
                            g[i][j] = 0;
                        }
                    }
                }else{
                    y++;
                }
            }
        }

        /*
        // デバック
        for(int i = 10; i >= 0; i--){
            for(int j = 0; j < 5; j++){
                cout << g[i][j] << " ";
            }
            cout << endl;
        }
        */
    }


    cout << sum << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3232
final_rank: 3

code_snippet: #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()) {}
}
accuracy: 1
time_ms: 60
memory_kb: 2780
final_rank: 11

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

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define F first
#define S second
#define INF 1 << 30
#define UNDER 10000

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int t[UNDER + 10][5], n;

int count() {
	int res = 0;
	rep(i, UNDER)
		rep(j, 5)
			if (t[i][j] == 1)
				res++;
	return res;
}

void deleteblock() {
	for (int y = UNDER - 1; y >= 0; y--) {
		int f = 0;
		rep(i, 5)
			if (t[y][i] == 0) {
				f = 1;
				break;
			}
		if (f)
			continue;
		rep(i, 5)
		{
			for (int c = y; c > 0; c--) {
				t[c][i] = t[c - 1][i];
			}
		}
		t[0][0] = t[0][1] = t[0][2] = t[0][3] = t[0][4] = 0;
		y++;
	}
	return;
}

int main() {
	while (scanf("%d", &n) && n) {
		int d, p, q;
		memset(t, 0, sizeof(t));
		rep(i, 5)
			t[UNDER][i] = 1;

		rep(i, n)
		{
			scanf("%d%d%d", &d, &p, &q);
			q--;
			if (d == 1) {
				REP(y, 1, UNDER+1)
				{
					int f = 0;
					REP(x, q, q+p)
						if (t[y][x] == 1)
							f = 1;
					if (!f)
						continue;
					REP(x, q, q+p)
						t[y - 1][x] = 1;
					break;
				}
			} else {
				rep(y, UNDER+1)
				{
					if (!t[y][q])
						continue;
					for (int c = y - 1; c > y - p - 1; c--)
						t[c][q] = 1;
					break;
				}
			}
			deleteblock();
			/*      REP(i, 990, UNDER+1){
			 rep(j, 5) printf("%d ", t[i][j]); puts("");
			 }*/
		}

		printf("%d\n", count());
	}
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3380
final_rank: 15

code_snippet: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#ifdef _WIN32
#define __builtin_popcount __popcnt
#endif
int n, d, p, q;
int main() {
	while (cin >> n, n) {
		vector<int> bit;
		vector<int> c(5);
		while (n--) {
			cin >> d >> p >> q; q--;
			if (d == 1) {
				int r = *max_element(c.begin() + q, c.begin() + q + p);
				int b = (1 << (q + p)) - (1 << q);
				if (bit.size() == r) bit.push_back(b);
				else bit[r] |= b;
			}
			else {
				for (int i = c[q]; i < c[q] + p; i++) {
					if (bit.size() == i) bit.push_back(1 << q);
					else bit[i] |= 1 << q;
				}
			}
			vector<int> bit2;
			for (int i : bit) {
				if (i != 31) bit2.push_back(i);
			}
			bit = bit2;
			for (int i = 0; i < 5; i++) {
				c[i] = 0;
				for (int j = bit2.size() - 1; j >= 0; j--) {
					if (bit2[j] & (1 << i)) {
						c[i] = j + 1; break;
					}
				}
			}
		}
		int ret = 0;
		for (int i : bit) ret += __builtin_popcount(i);
		cout << ret << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3112
final_rank: 1

code_snippet: #include "bits/stdc++.h"
using namespace std;

int fall(vector<vector<int>>&fi, vector<vector<int>>&pi,int fall_l) {
	int fall_u = 0;
	for (; fall_u < fi.size() - pi.size(); ++fall_u) {
		bool ok = true;
		for (int i = 0; i < pi.size(); ++i) {
			for (int j = 0; j < pi[i].size(); ++j) {
				if (pi[i][j]) {
					if (fi[fall_u + i+1][fall_l + j])ok = false;
				}
			}
		}
		if (!ok) {
			break;
		}
	}
	for (int i = 0; i < pi.size(); ++i) {
		for (int j = 0; j < pi[i].size(); ++j) {
			if (pi[i][j]) {
				fi[fall_u + i][fall_l + j] = true;
			}
		}
	}
	int delline = 0;
	for (int i = fall_u + pi.size() - 1; i >= fall_u; --i) {
		while (all_of(fi[i].begin(), fi[i].end(), [](const int a) {return a == 1; })) {
			transform(fi[i].begin(), fi[i].end(), fi[i].begin(), [](const int a) {return false; });
			rotate(fi.begin(), fi.begin() + i, fi.begin() + i+1); 
			delline += 1;
		}
	}
	return delline;
}

int main() {
	int N;
	while (cin>>N,N) {
		int sum = 0;
		vector<vector<int>>field(5*N, vector<int>(5));
		while(N--){
			int d, p, q; cin >> d >> p >> q; q--;
			sum += p;
			vector<vector<int>>pino(d == 1 ? 1 : p, vector<int>(d == 2 ? 1 : p, 1));
			sum -= fall(field, pino, q) * 5;
		}
		cout << sum << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3208
final_rank: 17

code_snippet: #include "bits/stdc++.h"
using namespace std;

int fall(vector<vector<int>>&field, vector<vector<int>>&pino,int l) {
	int y = 0;
	for ( y = 0; y < field.size() - pino.size(); ++y) {
		bool ok = true;
		for (int i = 0; i < pino.size(); ++i) {
			for (int j = 0; j < pino[i].size(); ++j) {
				if (pino[i][j]) {
					if (field[y + i+1][l + j])ok = false;
				}
			}
		}
		if (!ok) {
			break;
		}
	}
	for (int i = 0; i < pino.size(); ++i) {
		for (int j = 0; j < pino[i].size(); ++j) {
			if (pino[i][j]) {
				assert(!field[y + i][l + j]);
				field[y + i][l + j] = true;
			}
		}
	}
	int delline = 0;
	for (int i = y + pino.size() - 1; i >= y; --i) {
		while (all_of(field[i].begin(), field[i].end(), [](const int a) {return a == 1; })) {
			transform(field[i].begin(), field[i].end(), field[i].begin(), [](const int a) {return false; });
			for (int j = i- 1; j >= 0; --j) {
				swap(field[j], field[j + 1]);
			}
			delline += 1;
		}
	}
	return delline;
}

int main() {
	int N;
	while (cin>>N,N) {
		int sum = 0;
		vector<vector<int>>field(5*N, vector<int>(5));
		while(N--){
			int d, p, q; cin >> d >> p >> q; q--;
			sum += p;
			vector<vector<int>>pino(d == 1 ? 1 : p, vector<int>(d == 2 ? 1 : p, 1));
			sum -= fall(field, pino, q) * 5;
		}
		cout << sum << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3192
final_rank: 16

code_snippet: #include "bits/stdc++.h"
using namespace std;

int fall(vector<vector<int>>&field, vector<vector<int>>&pino,int l) {
	int y = 0;
	for ( y = 0; y < field.size() - pino.size(); ++y) {
		bool ok = true;
		for (int i = 0; i < pino.size(); ++i) {
			for (int j = 0; j < pino[i].size(); ++j) {
				if (pino[i][j]) {
					if (field[y + i+1][l + j])ok = false;
				}
			}
		}
		if (!ok) {
			break;
		}
	}
	for (int i = 0; i < pino.size(); ++i) {
		for (int j = 0; j < pino[i].size(); ++j) {
			if (pino[i][j]) {
				assert(!field[y + i][l + j]);
				field[y + i][l + j] = true;
			}
		}
	}
	int delline = 0;
	for (int i = y + pino.size() - 1; i >= y; --i) {
		while (all_of(field[i].begin(), field[i].end(), [](const int a) {return a == 1; })) {
			transform(field[i].begin(), field[i].end(), field[i].begin(), [](const int a) {return false; });
			for (int j = i- 1; j >= 0; --j) {
				swap(field[j], field[j + 1]);
			}
			delline += 1;
		}
	}
	return delline;
}

int main() {
	int N;
	while (cin>>N,N) {
		int sum = 0;
		vector<vector<int>>field(5*N+1, vector<int>(5));
		while(N--){
			int d, p, q; cin >> d >> p >> q; q--;
			sum += p;
			vector<vector<int>>pino(d == 1 ? 1 : p, vector<int>(d == 2 ? 1 : p, 1));
			sum -= fall(field, pino, q) * 5;
		}
		cout << sum << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3136
final_rank: 14

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\b.txt"

int fall(vector<vector<int>>&field, vector<vector<int>>&pino,int l) {
	{

	}
	int y = 0;
	for ( y = 0; y < field.size() - pino.size(); ++y) {
		bool ok = true;
		for (int i = 0; i < pino.size(); ++i) {
			for (int j = 0; j < pino[i].size(); ++j) {
				if (pino[i][j]) {
					if (field[y + i+1][l + j])ok = false;
				}
			}
		}
		if (!ok) {
			break;
		}
	}
	for (int i = 0; i < pino.size(); ++i) {
		for (int j = 0; j < pino[i].size(); ++j) {
			if (pino[i][j]) {
				assert(!field[y + i][l + j]);
				field[y + i][l + j] = true;
			}
		}
	}
	int delline = 0;
	for (int i = y + pino.size() - 1; i >= y; --i) {
		while (all_of(field[i].begin(), field[i].end(), [](const int a) {return a == 1; })) {
			transform(field[i].begin(), field[i].end(), field[i].begin(), [](const int a) {return false; });
			for (int j = i- 1; j >= 0; --j) {
				swap(field[j], field[j + 1]);
			}
			delline += 1;
		}
	}
	return delline;
}

int main() {
	while (1) {
		int N; cin >> N;
		if (!N)break;
		int sum = 0;
		vector<vector<int>>field(5*N+10, vector<int>(5));
		for (int i = 0; i < N; ++i) {
			int d, p, q; cin >> d >> p >> q; q--;
			int w = d == 2 ? 1 : p;
			int h = d == 1 ? 1 : p;
			sum += p;
			vector<vector<int>>pino(h, vector<int>(w, 1));
			sum -= fall(field, pino, q) * 5;
		}
		cout << sum << endl;
	}
	
	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3244
final_rank: 18