Problem:


Problem C: Cut the Cake

Today is the birthday of Mr. Bon Vivant, who is known as one of the greatest p&acirc;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

p1  s1

...

pn  sn

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.
pi 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.

si 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 pi, you can reach the starting point by traveling si 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



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


Sample output:
4 4 6 16
1 1 1 1
10

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

while True:
    N, W, D = map(int, input().split())
    if N == 0 and W == 0 and D == 0:
        break    
    cakes = [(W * D, W, D)]
    for i in range(N):
        p, s = map(int, input().split())
        _, w, d = cakes.pop(p - 1)
        s %= (2 * w + 2 * d)
        A = 0
        B = 0
        if s <= w:
            A = (s * d, s, d)
            B = ((w - s) * d, w - s, d)
        elif s <= w + d:
            y = s - w
            A = (w * y, w, y)
            B = (w * (d - y), w, (d - y))
        elif s <= 2 * w + d:
            x = 2 * w + d - s
            A = (x * d, x, d)
            B = ((w - x) * d, w - x, d)
        else:
            y = (2 * w + 2 * d) - s
            A = (w * y, w, y)
            B = (w * (d - y), w, (d - y))
        
        if A[0] > B[0]:
            A, B = B, A
        cakes.append(A)
        cakes.append(B)
    print(" ".join(map(lambda cake: str(cake[0]), sorted(cakes))))

Submission status: Accepted

Solution code:
N,W,D = map(int,input().split())

while W:
  cp = [[0,0,0,0] for _ in range(2*N+1)]	#始点x,始点y,長さx,長さy
  cp[0] = [0,0,W,D]
  
  for time in range(N):
    P,S = map(int,input().split())
    P -= 1
    ta = cp[P]
    S %= (ta[2]+ta[3])*2
    
    #カット処理
    #面積が小さいほうが番号が小さい
    
    #上辺
    if 0 <= S < ta[2]:
      if S < ta[2]/2:
        cp[time+1] = [ta[0],ta[1],S,ta[3]]
        cp[time+2] = [ta[0]+S,ta[1],ta[2]-S,ta[3]]
      else:
        cp[time+1] = [ta[0]+S,ta[1],ta[2]-S,ta[3]]
        cp[time+2] = [ta[0],ta[1],S,ta[3]]
        
    #右辺
    elif S < ta[2]+ta[3]:
      S -= ta[2]
      if S < ta[3]/2:
        cp[time+1] = [ta[0],ta[1],ta[2],S]
        cp[time+2] = [ta[0],ta[1]+S,ta[2],ta[3]-S]
      else:
        cp[time+1] = [ta[0],ta[1]+S,ta[2],ta[3]-S]
        cp[time+2] = [ta[0],ta[1],ta[2],S]
        
    #下辺
    elif S < ta[2]*2+ta[3]:
      S = ta[2]-(S-ta[2]-ta[3])
      if S < ta[2]/2:
        cp[time+1] = [ta[0],ta[1],S,ta[3]]
        cp[time+2] = [ta[0]+S,ta[1],ta[2]-S,ta[3]]
      else:
        cp[time+1] = [ta[0]+S,ta[1],ta[2]-S,ta[3]]
        cp[time+2] = [ta[0],ta[1],S,ta[3]]
        
    #左辺
    else:
      S = ta[3]-(S-ta[2]*2-ta[3])
      if S < ta[3]/2:
        cp[time+1] = [ta[0],ta[1],ta[2],S]
        cp[time+2] = [ta[0],ta[1]+S,ta[2],ta[3]-S]
      else:
        cp[time+1] = [ta[0],ta[1]+S,ta[2],ta[3]-S]
        cp[time+2] = [ta[0],ta[1],ta[2],S]
        
    #カットされたやつのデータを消す
    del cp[P]
    
    
  #データ出力
  ansli = []
  for el in cp:
    ansli.append(el[2]*el[3])
  ansli.sort()
  print(*ansli)
  
  N,W,D = map(int,input().split())

Submission status: Accepted

Solution code:
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)
    # print(ls)
    ans = sorted([hi * wi for wi, hi in ls])
    print(' '.join(map(str, ans)))

Submission status: Accepted

Solution code:
def cake(n, w, d):
  piece = [0] * (n + 1)
  piece[0] = [w, d, w * d]
  ps = [0] * n
  for i in range(n):#100
    #print(piece)
    p, s = map(int, input().split())
    now = piece[p - 1]#切るケーキ
    nw, nd = now[0], now[1]
    s = s % (nw + nd)
    if p != i + 1:
      for j in range(p, i + 2):
        piece[j - 1] = piece[j]
    if s < nw:
      a, b = s, nw - s
      if a > b:
        piece[i + 1] = [a, nd, a * nd]
        piece[i] = [b, nd, b * nd]
      else:
        piece[i] = [a, nd, a * nd]
        piece[i + 1] = [b, nd, b * nd]   
        
    elif s < nw + nd:
      a, b = s - nw, nw + nd - s
      if a > b:
        piece[i] = [nw, b, b * nw]
        piece[i + 1] = [nw, a, a * nw]
      else:
        piece[i + 1] = [nw, b, b * nw]
        piece[i] = [nw, a, a * nw]
        
    elif s < 2 * nw + nd:
      a, b = s - nw - nd, 2 * nw + nd - s
      if a > b:
        piece[i + 1] = [a, nd, a * nd]
        piece[i] = [b, nd, b * nd]
      else:
        piece[i] = [a, nd, a * nd]
        piece[i + 1] = [b, nd, b * nd]   
        
    else:
      a, b = s - nw, nw + nd - s
      if a > b:
        piece[i] = [nw, b, b * nw]
        piece[i + 1] = [nw, a, a * nw]
      else:
        piece[i + 1] = [nw, b, b * nw]
        piece[i] = [nw, a, a * nw]
  return piece

p = True
while p:
  n, w, d = map(int, input().split())
  if (n, w, d) == (0, 0, 0):
    p = False
    quit()
  else:
    ans = cake(n, w, d)
    ans = sorted(ans, key = lambda x:x[2])
    for i in range(n + 1):
      if i!= n:
        print(ans[i][2], end = " ")
      else:
        print(ans[i][2])

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)
while 1:
    N, W, D = mapint()
    if N==0 and W==0 and D==0:
        break
    cakes = [(W, D)]
    for i in range(N):
        p, s = mapint()
        w, h = cakes.pop(p-1)
        s = s%(w+h)
        if s<w:
            piece1, piece2 = (s, h), (w-s, h)
            if s>=w-s: piece1, piece2 = piece2, piece1
            cakes.append(piece1)
            cakes.append(piece2)
        else:
            s -= w
            piece1, piece2 = (w, s), (w, h-s)
            if s>=h-s: piece1, piece2 = piece2, piece1
            cakes.append(piece1)
            cakes.append(piece2)
    cakes = sorted(list(map(lambda x: x[0]*x[1], cakes)))
    cakes = list(map(str, cakes))
    print(' '.join(cakes))

Submission status: Accepted

Solution code:
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]))

Submission status: Accepted

Solution code:
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)]

            # id付け替え
            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()

Submission status: Accepted

Solution code:
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)))

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1149&lang=jpfrom bisect import bisect_left
# 対称性から s <= w + h の範囲のみを考えれば良い
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()

Submission status: Accepted

Solution code:
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()))

Submission status: Accepted

Solution code:
#!usr/bin/env python3
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())

Submission status: Accepted

Solution code:
#!usr/bin/env python3
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

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
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())

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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

    #         t ,  S , W , H
    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)))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# python template for atcoder1
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()

Submission status: Accepted

Solution code:
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)

        #あとはここでlinkedlistを使うようにする
        #だいたい問題を解くのに50分かかった。多分急いで４０分。
        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])

Submission status: Runtime Error

Solution code:
ans_list = []
def make_pieces(w, d, s):
    s %= (2 * w + 2 * d)
    while True:
        if w > s:
            return [s, d], [w - s, d]
        s -= w
        if d > s:
            return [w, s], [w, d - s]
        s -= d
        if w > s:
            return [w - s, d], [s, d]
        s -= w
        if d > s:
            return [w, s], [w, d - s]
        s -= d


while True:
    n, w, d = map(int, input().split())
    P = [[w, d]]
    if n == 0 and w == 0 and d == 0:
        break
    for i in range(n):
        p, s = map(int, input().split())
        new_pieces = make_pieces(P[p - 1][0], P[p - 1][1], s)
        #print(new_pieces)
        if new_pieces[0][0] * new_pieces[0][1] < new_pieces[1][0] * new_pieces[1][1]:
            P = P[:p - 1] + [new_pieces[0]] + [new_pieces[1]] + P[p:]
        else:
            P = P[:p - 1] + [new_pieces[1]] + [new_pieces[0]] + P[p:]
        #print(P)

    S_list = []
    for i in P:
        S_list.append(i[0] * i[1])

    S_list.sort()
    ans_list.append(S_list)

for i in range(len(ans_list)):
    I = ans_list[i]
    for j in range(len(I)):
        J = I[j]
        if i == len(ans_list) - 1 and j == len(I) - 1:
            print(J, end = "")
        else:
            print(J, end = " ")
    print()

Submission status: Runtime Error

Solution code:
def cut(cake,s):
    ret_cake = [(0,0),(0,0)]
    w = cake[2]
    h = cake[1]
    s %= 2*(w+h)
    if w > s:
        ret_cake = [(h,s),(h,w-s)]
    elif w+h > s:
        ret_cake = [(s-w,w),(w+h-s,w)]
    elif 2*w+h > s:
        ret_cake = [(s-w-h,w),(2*w+h-s,w)]
    elif 2*w+2*h > s:
        ret_cake = [(s-2*w-h,w),(2*w+2*h-s,w)]

    return [ (c[0]*c[1],c[0],c[1]) for c in ret_cake]

while True:
    n,w,d = map(int,input().split())
    if n == 0  and w == 0 and d == 0:
        break
    p = [0] * n
    s = [0] * n
    for i in range(n):
        p[i],s[i] = map(int,input().split())
    cake = [ (w*d,d,w) ]

    for i in range(n):
        cake = cake[:p[i]-1] + cake[p[i]:] + cut(cake[p[i]-1],s[i])
    cake.sort()
    print( " ".join(map(str,[c[0] for c in cake])))

Submission status: Runtime Error

Solution code:
def cut(d, w, s):
    s %= 2 * (d + w)
    sq = []
    if 0 < s < w:
        sq = [(d, s), (d, w - s)]
    elif w < s < w + d:
        s -= w
        sq = [(s, w), (d - s, w)]
    elif w + d < s < 2 * w + d:
        s -= w + d
        sq = [(d, s), (d, w - s)]
    elif 2 * w + d < s < 2 * (w + d):
        s -= 2 * w + d
        sq = [(s, w), (d - s, w)]
    else:
        assert(False)
    p1, p2 = sq
    if p1[0] * p1[1] > p2[0] * p2[1]:
        p1, p2 = p2, p1
    return p1, p2


while True:
    N, W, D = map(int, input().split())
    if not (N | W | D):
        break
    square = [(D, W)]
    for _ in range(N):
        p, s = map(int, input().split())
        square[p - 1], p1 = cut(*square[p - 1], s)
        square.append(p1)
    print(*sorted(d * w for d, w in square))

Submission status: Runtime Error

Solution code:
def cut(d, w, s):
    s %= 2 * (d + w)
    sq = []
    if 0 < s < w:
        sq = [(d, s), (d, w - s)]
    elif w < s < w + d:
        s -= w
        sq = [(s, w), (d - s, w)]
    elif w + d < s < 2 * w + d:
        s -= w + d
        sq = [(d, s), (d, w - s)]
    elif 2 * w + d < s < 2 * (w + d):
        s -= 2 * w + d
        sq = [(s, w), (d - s, w)]
    else:
        assert(False)
    p1, p2 = sq
    if p1[0] * p1[1] > p2[0] * p2[1]:
        p1, p2 = p2, p1
    return [p1, p2]


while True:
    N, W, D = map(int, input().split())
    if not (N | W | D):
        break
    square = [(D, W)]
    for _ in range(N):
        p, s = map(int, input().split())
        square.extend(cut(*square.pop(p - 1), s))
    print(*sorted(d * w for d, w in square))

Submission status: Runtime Error

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353
dd = [(0,-1),(1,0),(0,1),(-1,0)]
ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    while True:
        n,w,d = LI()
        if n == 0 and w == 0 and d == 0:
            break
        a = [LI() for _ in range(n)]
        r = [(w,d)]
        for p,s in a:
            ww,dd = r.pop(p-1)
            u = ww+dd
            s %= u
            if s < ww:
                r.append((s,dd))
                r.append((ww-s,dd))
            else:
                s -= ww
                r.append((ww,s))
                r.append((ww,dd-s))

        rr.append(' '.join(map(lambda x: str(x[0]*x[1]),sorted(r))))
    return '\n'.join(map(str,rr))


print(main())