1.

/**
* ACM Training 2009
* ACM Problem 11722 - Joining with Friend
*
* @author Felix Dietrich
* @version 1.0, 1/7/2009
*
* Methode: Geometry
* Status : Accepted
* Runtime: 0.340
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=117&problem=2769&mosmsg=Submission+received+with+ID+7583460
*/

import java.io.IOException;
import java.util.Scanner;

public class Main
{

public static void main(String... as) throws IOException
{
Scanner sc;
sc = new Scanner(System.in);

int cases = sc.nextInt();
int t1, t2, s1, s2, w;
int totalarea;
double partarea = 0;

int x_bottom = 0;
int x_top = 0;
int x_bottomtop = 0;
int x_topbottom = 0;
double A,B;

for (int i = 0; i < cases; i++)
{
t1 = sc.nextInt();
t2 = sc.nextInt();
s1 = sc.nextInt();
s2 = sc.nextInt();
w = sc.nextInt();

totalarea = (s2 - s1) * (t2 - t1);

// rechteck unter dem schlauch bzw rechteck über dem schlauch
if (t2 <= s1 - w || t1 >= s2 + w)
partarea = 0;
else if (t2 <= s1 + w && t1 >= s2 - w)
partarea = totalarea;
else
{
// calculate x values between s1 and s2 of lower line
x_bottom = Math.max(Math.min(s2, t1+w), s1);
x_bottomtop = Math.max(Math.min(s2, t2+w), s1);

// calculate x values between s1 and s2 of upper line
x_top = Math.min(Math.max(s1, t2-w), s2);
x_topbottom = Math.min(Math.max(s1, t1-w), s2);

// integrate upper line
A = 1.0/2*x_top*x_top + w * x_top - t1*x_top - (1.0/2*x_topbottom*x_topbottom + w * x_topbottom - t1*x_topbottom);
// add rect
A += Math.max(0, (s2-x_top)*(t2-t1));

// integrate lower line
B = 1.0/2*x_bottomtop*x_bottomtop - w * x_bottomtop - t1*x_bottomtop - (1.0/2*x_bottom*x_bottom - w * x_bottom - t1*x_bottom);
// add rect
B += Math.max(0, (s2-x_bottomtop)*(t2-t1));

partarea = A-B;
}

System.out.println(String.format("Case #%d: %.08f", i+1, (double) (partarea) / (double) (totalarea)));
}
}
}


2.

/**
* ACM Training 2009
* ACM Problem 11722 - Joining with Friend
*
* @author Felix Dietrich
* @author Dennis Wilfert
* @version 1.0, 11/26/2009
*
* Methode: Probabilities, Geometry
* Status : Accepted --> WA
* Runtime: 0.284
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=117&problem=2769&mosmsg=Submission+received+with+ID+7583460
*/

import java.io.IOException;
import java.util.Scanner;

public class Main
{

public static void main(String... as) throws IOException
{
Scanner sc;
sc = new Scanner(System.in);

int cases = sc.nextInt();
int t1, t2, s1, s2, w;
int totalarea;
int partarea = 0;

int x_bottom = 0;
int x_top = 0;
int y_left = 0;
int y_right = 0;
int cutArea = 0;
int cutArea2 = 0;

for (int i = 0; i < cases; i++)
{
t1 = sc.nextInt();
t2 = sc.nextInt();
s1 = sc.nextInt();
s2 = sc.nextInt();
w = sc.nextInt();

totalarea = (s2 - s1) * (t2 - t1);
cutArea = 0;
cutArea2 = 0;

// rechteck unter dem schlauch bzw rechteck über dem schlauch
if (t2 <= s1 - w || t1 >= s2 + w)
partarea = 0;
else if (t2 <= s1 + w && t1 >= s2 - w)
partarea = totalarea;
else
{
// x bottom ist der x wert des dreiecks rechts unten
if (t1 + w <= s2 && t1 + w >= s1) // ecken des rechtecks ausserhalb
x_bottom = t1 + w;
else if (s2 - w <= t1) // rechte untere ecke im schlauch
x_bottom = s2;
else // untere schlauchgerade über der diagonalen -> schnittdreieck entsteht!
{
x_bottom = t1 + w;
cutArea = (s1 - x_bottom) * (s1 - x_bottom) / 2;
}

// x top ist der x wert des dreiecks links oben
if (t2 <= s2 + w && t2 >= s1 + w)
x_top = t2 - w;
else if (s1 + w >= t2)
x_top = s1;
else
{
x_top = t2 - w;
cutArea2 = (s2 - x_top) * (s2 - x_top) / 2;
}

// y_left ist der y wert des dreiecks links oben
if (s1 + w <= t2 && s1 + w >= t1)
y_left = s1 + w;
else if (s1 + w >= t2)
y_left = t2;
else
y_left = s1 + w;

// y_right ist der y wert des dreiecks rechts unten
if (s2 - w <= t2 && s2 - w >= t1)
y_right = s2 - w;
else if (s2 - w <= t1)
y_right = t1;
else
y_right = s2 - w;

//partarea = ganze fläche - dreiecke links oben / rechts unten - überstehende dreiecke oben und unten

partarea = totalarea - (s2 - x_bottom) * (y_right - t1) / 2 - (t2 - y_left) * (x_top - s1) / 2 - cutArea - cutArea2;
}

System.out.println(String.format("%.07f", (double) (partarea) / (double) (totalarea)));
}
}
}


3.

/**
* ACM Training, SS09
* ACM Problem #11722 Joining With Friend
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2769

* @author Doina Logofatu
* @version 1.0, 11/20/2009
*
* Method: Math, Probability
* Status : Accepted --> WA
* Runtime: 0.008
*/

#include <stdio.h>

int main(){

int t1, t2, s1, s2, w, n, i, x0;
float A, B;

scanf("%d", &n);
for(i=0; i<n; ){
scanf("%d%d%d%d%d", &t1, &t2, &s1, &s2, &w);
printf("Case #%d: ", ++i);

x0 = t2 - w;
if(x0<=s1)A = A=t2*(s2-s1);
else if(x0<s2)A = (x0*x0)/2+w*x0-s1*s1/2-w*s1+t2*(s2-x0);
else A = s2*s2/2+w*s2-s1*s1/2-w*s1;

x0=t1+w;
if(x0>s2) B = t1*(s2-s1);
else if (x0>s1) B = t1*(x0-s1)+(s2*s2/2-w*s2-x0*x0/2+w*x0);
else B = s2*s2/2-w*s2-s1*s1/2+w*s1;

printf("%.8f\n", (A-B)/((s2-s1)*(t2-t1)));
}
return 0;
}