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

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

Solution in JAVA (judge verdict: Accepted)

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

	Scanner sc = new Scanner(System.in);
	public void run() {
		while(sc.hasNext()){
			int n = sc.nextInt();
			int w = sc.nextInt();
			int d = sc.nextInt();
			if(n == 0 && w == 0 && d == 0)break;
			else calc(n, w, d);
			
		}
	}
	public void calc(int n, int w, int d){
		//最終的にnピースできる
		ArrayList<int[]> list = new ArrayList<int[]>();
		list.add(new int[]{w, d});
		for(int i = 0; i < n; i++){
			int p = sc.nextInt();
			int s = sc.nextInt();
			
			int[] peace = list.get(p-1);
			list.remove(p-1);
			
			int nw = 0;
			int nd = 0;
			
			while(true){
				if(s < peace[0]) {
					nw = s;
					break;
				}
				else s -= peace[0];
				
				if(s < peace[1]){
					nd = s;
					break;
				}
				else s -= peace[1];
			}
			
			if(nw == 0){
				int nd2 = peace[1] - nd;
				if(nd < nd2){
					list.add(new int[]{peace[0], nd});
					list.add(new int[]{peace[0], nd2});
				}
				else{
					list.add(new int[]{peace[0], nd2});
					list.add(new int[]{peace[0], nd});
				}
			}
			else{
				int nw2 = peace[0] - nw;
				if(nw < nw2){
					list.add(new int[]{nw, peace[1]});
					list.add(new int[]{nw2, peace[1]});
				}
				else{
					list.add(new int[]{nw2, peace[1]});
					list.add(new int[]{nw, peace[1]});
				}
			}
		}
		
		list = mergesort(list);
		for(int i = 0; i < list.size(); i++){
			if(i != 0) System.out.print(" ");
			System.out.print(list.get(i)[0] * list.get(i)[1]);
		}
		System.out.println();
	}

	public ArrayList<int[]> mergesort(ArrayList<int[]> list){
		if(list.size() > 1){
			int n = list.size() / 2;
			int m = list.size() - n;
			ArrayList<int[]> a1 = new ArrayList<int[]>();
			ArrayList<int[]> a2 = new ArrayList<int[]>();
			for(int i = 0; i < n; i++) a1.add(list.get(i));
			for(int i = 0; i < m; i++) a2.add(list.get(i + n));

			a1 = mergesort(a1);
			a2 = mergesort(a2);
			return merge(a1, a2);
			
		}
		else return list;
	}
	
	public ArrayList<int[]> merge(ArrayList<int[]> a1, ArrayList<int[]> a2){
		int i = 0;
		int j = 0;
		ArrayList<int[]> a = new ArrayList<int[]>();
		while(i < a1.size() || j < a2.size()){
			if(j >= a2.size() || (i < a1.size() && a1.get(i)[0] * a1.get(i)[1] < a2.get(j)[0] * a2.get(j)[1])){
				a.add(a1.get(i));
				i++;
			}
			else {
				a.add(a2.get(j));
				j++;
			}
		}
		return a;
	}
	
	public static void main(String[] args) {
		new Main().run();
	}
}