Problem:
Problem E: Water Tank

Mr. Denjiro is a science teacher.

Today he has just received a specially ordered water tank that
will certainly be useful for his innovative experiments on water
flow.

Figure 1: The water tank

The size of the tank is 100cm (Width) * 50cm (Height) * 30cm (Depth)
(see Figure 1).

For the experiments, he fits several partition boards inside of
the tank parallel to the sideboards.

The width of each board is equal to the depth of the tank, i.e. 30cm.
The height of each board is less than that of the tank, i.e. 50 cm,
and differs one another.

The boards are so thin that he can neglect the thickness in the experiments.

Figure 2: The front view of the tank

The front view of the tank is shown in Figure 2.

There are several faucets above the tank and he turns them on at
the beginning of his experiment. The tank is initially empty.
Your mission is to write a
computer program to simulate water flow in the tank.

Input

The input consists of multiple data sets.

D is the number of the data sets.

D

DataSet1

DataSet2

...

DataSetD

The format of each data set
(DataSetd , 1 <= d <= D)
is as follows.

N

B1 H1

B2 H2

...

BN HN

M

F1 A1

F2 A2

...

FM AM

L

P1 T1

P2 T2

...

PL TL

Each line in the data set contains one or two integers.

N is the number of the boards he sets in the tank .
Bi and Hi
are the x-position (cm) and the height (cm)
of the i-th board,
where 1 <= i <= N .

Hi s differ from one another. You may assume the
following.

0 < N < 10 ,

0 < B1 < B2 < ... < BN < 100 ,

0 < H1 < 50 ,
0 < H2 < 50 ,
..., 0 < HN < 50.

M is the number of the faucets above the tank .
Fj and Aj are
the x-position (cm) and the amount of water flow (cm3/second)
of the j-th faucet ,
where 1 <= j <= M .

There is no faucet just above any boards .
Namely, none of Fj is equal to Bi .

You may assume the following .

0 < M <10 ,

0 < F1 < F2 < ...
< FM < 100 ,

0 < A1 < 100,
0 < A2 < 100, ...
0 < AM < 100.

L is the number of observation time and location.
Pk is the x-position (cm) of the k-th
observation point.
Tk is the k-th observation time in seconds from the beginning.

None of Pk
is equal to Bi .

You may assume the following .

0 < L < 10 ,

0 < P1 < 100,
0 < P2 < 100, ...,
0 < PL < 100 ,

0 < T1 < 1000000,
0 < T2 < 1000000,
... ,
0 < TL < 1000000.

Output

For each data set,
your program should output L lines each containing
one real number which represents the height (cm) of the water level specified
by the x-position Pk
at the time Tk.

Each answer may not have an error greater than 0.001.
As long as this condition is satisfied,
you may output any number of digits after the decimal point.

After the water tank is filled to the brim,
the water level at any Pk is equal to the
height of the tank, that is, 50 cm.

Sample Input

2
5
15 40
35 20
50 45
70 30
80 10
3
20 3
60 2
65 2
6
40 4100
25 7500
10 18000
90 7000
25 15000
25 22000
5
15 40
35 20
50 45
70 30
80 10
2
60 4
75 1
3
60 6000
75 6000
85 6000

Output for the Sample Input

0.666667
21.4286
36.6667
11.1111
40.0
50.0
30.0
13.3333
13.3333

Solution in JAVA (judge verdict: Time Limit Exceeded)

import java.util.*;
public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		ArrayList<Box> box, boxClone;
		int[][] water;
		int d, n, m, l;
		int p, t;
		int x1, x2, y1, y2;
		d = sc.nextInt();
		for(int i = 0; i < d; i++){

			n = sc.nextInt();
			box = new ArrayList<Box>();
			x1 = 0;
			y1 = 50;
			for(int j = 0; j < n; j++){
				x2 = sc.nextInt();
				y2 = sc.nextInt();
				box.add(new Box(x1, y1, x2, y2));
				x1 = x2;
				y1 = y2;
			}
			x2 = 100;
			y2 = 50;
			box.add(new Box(x1, y1, x2, y2));

			m = sc.nextInt();
			water = new int[m][2];
			for(int j = 0; j < m; j++){
				water[i][0] = sc.nextInt();
				water[i][1] = sc.nextInt();
			}
			waterIn(box, water);

			l = sc.nextInt();
			for(int j = 0; j < l; j++){
				p = sc.nextInt();
				t = sc.nextInt();
				boxClone = deepCopy(box);
				setHeight(box, t);
				overFlow(box);
				level(box);
				System.out.println(search(box, p));
				box = boxClone;
			}
		}
	}
	static void waterIn(ArrayList<Box> box, int[][] water){
		for(int i = 0; i < box.size(); i++){
			for(int j = 0; j < water.length; j++){
				if(water[j][0] > box.get(i).x1 && water[j][0] < box.get(i).x2){
					box.get(i).flow += water[j][1];
				}
			}
		}	
	}
	static void setHeight(ArrayList<Box> box, int t){
		for(int i = 0; i < box.size(); i++)
			box.get(i).height = box.get(i).flow * t / (box.get(i).x2 - box.get(i).x1) / 30.0;
	}
	static void overFlow(ArrayList<Box> box){
		for(int i = 0; i < box.size(); i++){
			if(box.get(i).height > box.get(i).y1 || box.get(i).height > box.get(i).y2){
				if(box.get(i).y1 < box.get(i).y2){
					box.get(i-1).height += (box.get(i).height - box.get(i).y1) * (box.get(i).x2 - box.get(i).x1) / (box.get(i-1).x2 - box.get(i-1).x1);
					box.get(i).height = box.get(i).y1;
				}else if(box.get(i).y1 > box.get(i).y2){
					box.get(i+1).height += (box.get(i).height - box.get(i).y2) * (box.get(i).x2 - box.get(i).x1) / (box.get(i+1).x2 - box.get(i+1).x1);
					box.get(i).height = box.get(i).y2;
				}else{
					box.get(i-1).height += (box.get(i).height - box.get(i).y1) * (box.get(i).x2 - box.get(i).x1) / (box.get(i-1).x2 - box.get(i-1).x1) / 2;
					box.get(i+1).height += (box.get(i).height - box.get(i).y1) * (box.get(i).x2 - box.get(i).x1) / (box.get(i+1).x2 - box.get(i+1).x1) / 2;
					box.get(i).height = box.get(i).y1;
				}
			}
		}
	}
	static void level(ArrayList<Box> box){
		while(true){
			int count = 0;
			for(int i = 0; i < box.size()-1; i++){
				if(box.get(i).height >= box.get(i).y2 && box.get(i+1).height >= box.get(i).y2){
					box.get(i).full = 1;
					count++;
				}
			}
			if(count == 0)
				break;
			int left = 0, right = 0;
			int k = 0;
			double sum = 0;
			for(int i = 0; i < box.size()-1; i++){
				if(box.get(i).full == 1){
					if(k == 0){
						left = i;
						k = 1;
					}
					sum += box.get(i).height * (box.get(i).x2 - box.get(i).x1);
					continue;
				}
				if(k == 0)
					continue;
				right = i;
				box.get(left).x2 = box.get(right).x2;
				box.get(left).y2 = box.get(right).y2;
				box.get(left).height = sum / (box.get(right).x2 - box.get(left).x1);
				box.get(left).full = 0;
				for(int j = left+1; j <= right; j++)
					box.remove(j);
				k = 0;
				sum = 0;
				i = left;
			}
		}
	}
	static double search(ArrayList<Box> box, int x){
		double height = -1;
		for(int i = 0; i < box.size(); i++){
			if(x > box.get(i).x1 && x < box.get(i).x2){
				height = box.get(i).height;
				break;
			}
		}
		return (height <= 50 ? height : 50);
	}
	static ArrayList<Box> deepCopy(ArrayList<Box> box){
		ArrayList<Box> boxClone = new ArrayList<Box>();
		for(int i = 0; i < box.size(); i++)
			boxClone.add(box.get(i).clone());
		return boxClone;
	}
}

class Box implements Cloneable{
	int x1, x2;
	int y1, y2;
	int full = 0;
	int flow = 0;
	double height;
	Box(int p1, int q1, int p2, int q2){
		x1 = p1;
		y1 = q1;
		x2 = p2;
		y2 = q2;
	}
	public Box clone(){
		try{
			Box cloned = (Box)super.clone();
			return cloned;
		}catch(CloneNotSupportedException e){
			return null;
		}
	}
}