Problem:
Problem C: Cut the Cake Today is the birthday of Mr. Bon Vivant, who is known as one of the greatest pâtissiers in the world.
Those who are invited to his birthday party are gourmets from around the world.
They are eager to see and eat his extremely creative cakes.
Now a large box-shaped cake is being carried into the party.
It is not beautifully decorated and looks rather simple, but it must be delicious beyond anyone's imagination.
Let us cut it into pieces with a knife and serve them to the guests attending the party. The cake looks rectangular, viewing from above (Figure C-1).
As exemplified in Figure C-2, the cake will iteratively be cut into pieces, where on each cut exactly a single piece is cut into two smaller pieces.
Each cut surface must be orthogonal to the bottom face and must be orthogonal or parallel to a side face.
So, every piece shall be rectangular looking from above and every side face vertical.   Figure C-1: The top view of the cake     Figure C-2: Cutting the cake into pieces  Piece sizes in Figure C-2 vary significantly and it may look unfair, but you don't have to worry.
Those guests who would like to eat as many sorts of cakes as possible often prefer smaller pieces.
Of course, some prefer larger ones. Your mission of this problem is to write a computer program that simulates the cutting process of the cake and reports the size of each piece. Input The input is a sequence of datasets, each of which is of the following format.  n  w  d  p 1  s 1 ...  p n  s n  The first line starts with an integer n that is between 0 and 100 inclusive.
It is the number of cuts to be performed.
The following w and d in the same line are integers between 1 and 100 inclusive.
They denote the width and depth of the cake, respectively.
Assume in the sequel that the cake is placed so that w and d are the lengths in the east-west and north-south directions, respectively. Each of the following n lines specifies a single cut, cutting one and only one piece into two. p i is an integer between 1 and i inclusive and is the identification number of the piece that is the target of the i -th cut.
Note that, just before the i -th cut, there exist exactly i pieces.
Each piece in this stage has a unique identification number that is one of 1, 2, ..., i and is defined as follows:  The earlier a piece was born, the smaller its identification number is. Of the two pieces born at a time by the same cut, the piece with the smaller area (looking from above) has the smaller identification number.
If their areas are the same, you may define as you like the order between them, since your choice in this case has no influence on the final answer. Note that identification numbers are adjusted after each cut.  s i is an integer between 1 and 1000 inclusive and specifies the starting point of the i -th cut.
From the northwest corner of the piece whose identification number is p i , you can reach the starting point by traveling s i in the clockwise direction around the piece.  
You may assume that the starting point determined in this way cannot be any one of the four corners of the piece.
The i -th cut surface is orthogonal to the side face on which the starting point exists.   The end of the input is indicated by a line with three zeros.   Output   For each dataset, print in a line the areas looking from above of all the pieces that exist upon completion of the n cuts specified in the dataset.
They should be in ascending order and separated by a space.
When multiple pieces have the same area, print it as many times as the number of the pieces.   Sample Input  3 5 6
1 18
2 19
1 2
3 4 1
1 1
2 1
3 1
0 2 5
0 0 0  Output for the Sample Input  4 4 6 16
1 1 1 1
10

Solution (Python):
while(1):
    n, w, h = map(int, input().split())
    if not n+w+h:
        break
    t = [[w, h]]
    for i in range(n):
        p, s = map(int, input().split())
        cur = t.pop(p-1)
        W = cur[0]
        H = cur[1]
        s %= 2*W+2*H
        if s < W:
            a = min(s, W-s)
            t.extend([[a, H], [W-a, H]])
        elif s < W+H:
            a = min(s-W, W+H-s)
            t.extend([[W, a], [W, H-a]])
        elif s < 2*W+H:
            a = min(s-W-H, 2*W+H-s)
            t.extend([[a, H], [W-a, H]])
        else:
            a = min(s-2*W-H, 2*W+2*H-s)
            t.extend([[W, a], [W, H-a]])
    ans = []
    for i in t:
        ans.append(i[0]*i[1])
    ans.sort()
    print(' '.join([str(i) for i in ans]))

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    while True:
        n, w, d = LI()
        if n==0 and w==0 and d==0:
            break

        cake = []
        cake.append([w, d])
        for _ in range(n):
            ps = LI()
            p = ps[0]-1
            s = ps[1]
            
            cake_w = cake[p][0]
            cake_d = cake[p][1]
            s %= (cake_w+cake_d)*2
            if s<cake_w:
                new_cake_s = [min(s, cake_w-s), cake_d]
                new_cake_l = [max(s, cake_w-s), cake_d]
            elif cake_w<s<cake_w+cake_d:
                new_cake_s = [cake_w, min(s-cake_w, cake_w+cake_d-s)]
                new_cake_l = [cake_w, max(s-cake_w, cake_w+cake_d-s)]
            elif cake_w+cake_d<s<cake_w+cake_d+cake_w:
                new_cake_s = [min(s-(cake_w+cake_d), cake_w+cake_d+cake_w-s), cake_d]
                new_cake_l = [max(s-(cake_w+cake_d), cake_w+cake_d+cake_w-s), cake_d]
            else:
                new_cake_s = [cake_w, min(s-(cake_w+cake_d+cake_w), cake_w+cake_d+cake_w+cake_d-s)]
                new_cake_l = [cake_w, max(s-(cake_w+cake_d+cake_w), cake_w+cake_d+cake_w+cake_d-s)]

            
            del cake[p]
            
            cake.append(new_cake_s)
            cake.append(new_cake_l)

        print(*sorted([i[0]*i[1] for i in cake]))

if __name__ == '__main__':
    resolve()

Solution (Python):
while True:
    n,w,h = map(int,input().split())
    if w == 0:
        break
    ls = [[w,h]]
    for i in range(n):
        p,s = map(int,input().split())
        p -= 1
        pw,ph = ls[p]
        if s%(pw+ph) < pw:
            s %= pw+ph
            ls.extend([[min(s,pw-s),ph],[max(s,pw-s),ph]])
        else:
            s = (s-pw)%(ph+pw)
            ls.extend([[pw,min(s,ph-s)],[pw,max(s,ph-s)]])
        ls.pop(p)
    ans = [hi*wi for wi,hi in ls]
    ans.sort()
    print(' '.join(map(str,ans)))

Solution (Python):


import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

class Cake:
    def __init__(self, h, w):
        self.height = h
        self.width = w
    
    def circumference(self):
        return 2 * (self.height + self.width)
    
    def area(self):
        return self.height * self.width


def main():
    ans = []

    while True:
        n,w,d = map(int, readline().split())
        if (n, w, d) == (0, 0, 0):
            break

        piece = [Cake(d, w)]
        for _ in range(n):
            p, s = map(int, readline().split())
            p -= 1
            
            c = piece.pop(p)
            s %= c.circumference() // 2
            if s < c.width:
                h1, h2 = c.height, c.height
                w1, w2 = min(s, c.width - s), max(s, c.width - s)
            else:
                h = s - c.width
                h1, h2 = min(h, c.height - h), max(h, c.height - h)
                w1, w2 = c.width, c.width
            
            """
            if h == c.height:
                h1, h2 = h, h
                w1, w2 = min(w, c.width - w), max(w, c.width - w)
            else:
                h1, h2 = min(h, c.height - h), max(h, c.height - h)
                w1, w2 = w, w
            """

            piece.append(Cake(h1, w1))
            piece.append(Cake(h2, w2))

        ans.append(sorted([p.area() for p in piece]))
    
    for a in ans:
        print(*a)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
def input():
	return sys.stdin.buffer.readline()[:-1]

while True:
	n, w, d = map(int, input().split())
	if n == w == d == 0:
		break
	cakes = dict()
	cakes[1] = [w, d]
	for i in range(1, n+1):
		p, s = map(int, input().split())
		w, d = cakes[p]
		for j in range(p, i):
			cakes[j] = cakes[j+1]
		s %= w+d
		if s < w:
			cakes[i] = [min(s, w-s), d]
			cakes[i+1] = [max(s, w-s), d]
		else:
			cakes[i] = [w, min(s-w, w+d-s)]
			cakes[i+1] = [w, max(s-w, w+d-s)]
	print(*sorted(v[0]*v[1] for v in cakes.values()))

Solution (Python):
while True:
    n,w,h = map(int,input().split())
    if w == 0:
        break
    ls = [[w,h]]
    for i in range(n):
        p,s = map(int,input().split())
        p -= 1
        pw,ph = ls[p]
        if s%(pw+ph) < pw:
            s %= pw+ph
            ls.extend([[min(s,pw-s),ph],[max(s,pw-s),ph]])
        else:
            s = (s-pw)%(ph+pw)
            ls.extend([[pw,min(s,ph-s)],[pw,max(s,ph-s)]])
        ls.pop(p)
    ans = [hi*wi for wi,hi in ls]
    ans.sort()
    print(' '.join(map(str,ans)))

Solution (Python):

import sys
import math
import string
import collections
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

n, w, d = map(int, input().split())
while not n == w == d == 0:
    if n == 0:
        print(w * d)
    else:
        num_list = [[0, 0, w, d]]
        for _ in range(n):
            p, s = map(int, input().split())
            vert = width = -1
            x1, y1, x2, y2 = num_list.pop(p - 1)
            temp_vert = y2 - y1
            temp_width = x2 - x1
            s %= (temp_width + temp_vert) * 2
            for k in range(10 ** 9):
                if k % 4 == 0:
                    if s <= temp_width:
                        width = s
                        break
                    else:
                        s -= temp_width
                elif k % 4 == 1:
                    if s <= temp_vert:
                        vert = s
                        break
                    else:
                        s -= temp_vert
                elif k % 4 == 2:
                    if s <= temp_width:
                        width = temp_width - s
                        break
                    else:
                        s -= temp_width
                else:
                    if s <= temp_vert:
                        vert = temp_vert - s
                        break
                    else:
                        s -= temp_vert
            if width != -1:
                grid1 = [x1, y1, x1 + width, y2]
                area1 = (width - x1 + x1) * (y2 - y1)
                grid2 = [x1 + width, y1, x2, y2]
                area2 = (x2 - width - x1) * (y2 - y1)
            else:
                grid1 = [x1, y1, x2, y1 + vert]
                area1 = (x2 - x1) * (vert + y1 - y1)
                grid2 = [x1, y1 + vert, x2, y2]
                area2 = (x2 - x1) * (y2 - vert - y1)
            num_list.append(grid1 if area1 <= area2 else grid2)
            num_list.append(grid2 if area1 <= area2 else grid1)
        print(" ".join(list(map(str, sorted([abs((i[2] - i[0]) * (i[3] - i[1])) for i in num_list])))))
    n, w, d = map(int, input().split())

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def size(p):
        return (p[2]-p[0])*(p[3]-p[1])

    def cut(p,s):
        u,l,d,r = p
        x = r-l
        y = d-u
        s %= 2*(x+y)
        if s < x:
            a,b = [u,l,d,l+s], [u,l+s,d,r]
        elif s < x+y:
            s -= x
            a,b = [u,l,u+s,r], [u+s,l,d,r]
        elif s < 2*x+y:
            s -= x+y
            s = x-s
            a,b = [u,l,d,l+s],[u,l+s,d,r]
        else:
            s -= 2*x+y
            s = y-s
            a,b = [u,l,u+s,r], [u+s,l,d,r]
        if size(b) < size(a):
            a,b = b,a
        return a,b

    while 1:
        n,w,h = LI()
        if n == w == h == 0:
            break
        q = [[0,0,h,w]]
        for _ in range(n):
            i,s = LI()
            i -= 1
            p = q.pop(i)
            a,b = cut(p,s)
            q.append(a)
            q.append(b)
        s = [size(p) for p in q]
        s.sort()
        print(*s)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
import sys,queue,math,copy
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
LI = lambda : [int(x) for x in input().split()]
_LI = lambda : [int(x)-1 for x in input().split()]

while True:
    N,W,D = LI()
    if N==0 and W==0 and D==0: break

    q = [(W*D,W,D)]
    for _ in range(N):
        p,s = LI()
        r = q.pop(p-1)
        w = r[1]
        d = r[2]
        s = s % (2*(w+d))
        if s < w:
            np1 = (s*d,s,d)
            np2 = ((w-s)*d,w-s,d)
        elif s < w + d:
            s -= w
            np1 = (w*s,w,s)
            np2 = (w*(d-s),w,d-s)
        elif s < w*2 + d:
            s -= w+d
            np1 = (s*d,s,d)
            np2 = ((w-s)*d,w-s,d)
        else:
            s -= w*2 + d
            np1 = (w*s,w,s)
            np2 = (w*(d-s),w,d-s)
        if np1 > np2: np1,np2 = np2,np1
        q.append(np1)
        q.append(np2)

    ans = ''
    q.sort()
    for s,w,d in q:
        ans += str(s) + ' '
    print (ans.rstrip())

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    while True:
        n, w, d = map(int, input().split())
        if w == 0:
            break
        cuts = [list(map(int, input().split())) for i in range(n)]
        cakes = [(w,d,w*d)]
        for cut in cuts:
            p, s = cut
            w, d, wd = cakes.pop(p-1)
            s = s % (w*2+d*2)
            if s > w+d:
                s -= w+d
            if 0 < s and s < w:
                w1 = min(s, w-s)
                w2 = w - w1
                cakes.append((w1, d, w1*d))
                cakes.append((w2, d, w2*d))
            else:
                d1 = min(w+d-s, s-w)
                d2 = d - d1
                cakes.append((w, d1, w*d1))
                cakes.append((w, d2, w*d2))
        cakes = sorted(cakes, key=lambda x:x[2])
        len_ = len(cakes)
        for i in range(len_):
            _,__,area = cakes[i]
            print(area, end="")
            if i != len_ - 1:
                print(" ",end="")
            else:
                print()

if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

def cut(W,H,s):
    s %= (W+H)
    if 0 <= s <= W:
        return [s,H],[W-s,H]
    elif W <= s <= W+H:
        return [W,s-W],[W,H+W-s]

while True:
    N,W,H = inpl()
    if N == 0 and W== 0:
        break

    
    cakes = [[INF,INF,INF,INF] for _ in range(N+5)]
    cakes[0] = [0,W*H,W,H]
    for t in range(N):
        p,s = inpl()
        tp,sp,wp,hp = cakes[p-1]
        c1,c2 = cut(wp,hp,s)
        cakes[p-1] = [INF,INF,INF,INF]
        cakes[N+4] = [t,c1[0]*c1[1],c1[0],c1[1]]
        cakes[N+3] = [t,c2[0]*c2[1],c2[0],c2[1]]
        cakes.sort()

    ans = []
    for t,s,w,h in cakes:
        if t == INF:
            break
        ans.append(s)
    ans.sort()
    print(' '.join(map(str,ans)))

Solution (Python):


import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve():
    N, W, D = map(int, input().split())
    if N == 0 and W == 0 and D == 0:
        exit()
    L = []
    L.append((W, D))

    for _ in range(N):
        op, le = map(int, input().split())
        cut_cake_W, cut_cake_D = L.pop(op-1)
        le = le % (cut_cake_W+cut_cake_D)
        if le < cut_cake_W:
            new_W1 = min(le, cut_cake_W-le)
            new_W2 = max(le, cut_cake_W-le)
            new_D1 = cut_cake_D
            new_D2 = cut_cake_D
        else:
            le = le-cut_cake_W
            new_W1 = cut_cake_W
            new_W2 = cut_cake_W
            new_D1 = min(le, cut_cake_D-le)
            new_D2 = max(le, cut_cake_D-le)
        L.append((new_W1, new_D1))
        L.append((new_W2, new_D2))
    L = sorted(L, key=lambda x: x[0]*x[1])
    print(" ".join(map(str, [a*b for a, b in L])))


while True:
    solve()

Solution (Python):
def cut2(cut_place, width, height):
    part1 = [width, cut_place]
    part2 = [width, height - cut_place]
    if height - cut_place >= cut_place:
        output = [part1, part2]
    else:
        output = [part2, part1]
    return output

def cut1(cut_place, width, height):
    part1 = [cut_place, height]
    part2 = [width - cut_place, height]
    if width - cut_place >= cut_place:
        output = [part1, part2]
    else:
        output = [part2, part1]
    return output


while True:

    headder = input().split(" ")
    n = int(headder[0])
    w = int(headder[1])
    h = int(headder[2])


    if n == 0 and h == 0 and w == 0:
        break

    slices = [[w,h]]

    for i in range(n):
        how = input().split(" ")
        part_id = int(how[0])
        cut_place = int(how[1])

        piece_obj = slices[part_id-1]
        width = piece_obj[0]
        height = piece_obj[1]

        seiki_cut_place = cut_place%(2*(width+height))

        if seiki_cut_place < width:
            result = cut1(seiki_cut_place,width,height)
        elif seiki_cut_place < width + height and seiki_cut_place > width:
            cp = seiki_cut_place - width
            result = cut2(cp, width, height)
        elif seiki_cut_place < 2*width + height and seiki_cut_place > width + height:
            cp = seiki_cut_place - width - height
            result = cut1(cp, width, height)
        elif seiki_cut_place < 2*width + 2*height and seiki_cut_place > 2*width + height:
            cp = seiki_cut_place - 2*width - height
            result = cut2(cp, width, height)

        
        
        slices = slices[0:part_id-1] + slices[part_id:] + result

    answers = []
    for ele in slices:
        answers.append(ele[0]*ele[1])

    sub = ""
    answers.sort()
    for ele in answers:
        sub += str(ele)
        sub += " "

    print(sub[0:-1])