My answer fails on 6th and 9th test cases. I can’t understand the reason. Can someone please help?
A link to the problem is given below.
http://www.usaco.org/index.php?page=viewproblem2&cpid=783
Here is my code.
import java.io.*;
import java.util.*;
class Rect{
int x1 = 0,y1 = 0,x2 = 0,y2 = 0;
int x3,y3,x4,y4;
int area(){
return Math.abs((y2-y1)*(x2-x1));
}
int calcWidth(){
return x2-x1;
}
int calcHeight(){
return y2-y1;
}
boolean containsPoint(int x, int y){
if( x >= this.x1 && x <= this.x2 && y >= this.y1 && y <= this.y2){
return true;
}
return false;
}
void setRemainingPoints(){
x3 = x2;
y3 = y1;
x4 = x1;
y4 = y2;
}
}
public class BlockedBillboard2{
public static void main(String[] args) throws IOException{
Kattio io = new Kattio("billboard");
Rect a = new Rect(), b = new Rect();
int area = 0;
b.x1 = io.nextInt() + 1000;
b.y1 = io.nextInt() + 1000;
b.x2 = io.nextInt() + 1000;
b.y2 = io.nextInt() + 1000;
b.setRemainingPoints();
a.x1 = io.nextInt() + 1000;
a.y1 = io.nextInt() + 1000;
a.x2 = io.nextInt() + 1000;
a.y2 = io.nextInt() + 1000;
if((!a.containsPoint(b.x1,b.y1) && !a.containsPoint(b.x2,b.y2) && !a.containsPoint(b.x3,b.y3) && !a.containsPoint(b.x4,b.y4)) || (!a.containsPoint(b.x1,b.y1) && !a.containsPoint(b.x2,b.y2) && a.containsPoint(b.x3,b.y3) && !a.containsPoint(b.x4,b.y4)) || (!a.containsPoint(b.x1,b.y1) && !a.containsPoint(b.x2,b.y2) && !a.containsPoint(b.x3,b.y3) && a.containsPoint(b.x4,b.y4)) || (a.containsPoint(b.x1,b.y1) && !a.containsPoint(b.x2,b.y2) && !a.containsPoint(b.x3,b.y3) && !a.containsPoint(b.x4,b.y4)) ){
area = b.area();
}else if((!a.containsPoint(b.x1,b.y1) && a.containsPoint(b.x2,b.y2) && !a.containsPoint(b.x3,b.y3) && a.containsPoint(b.x4,b.y4))){
area = Math.abs(b.x1 - b.x3) * Math.abs(b.y1-a.y1);
}else if((!a.containsPoint(b.x1,b.y1) && a.containsPoint(b.x2,b.y2) && a.containsPoint(b.x3,b.y3) && !a.containsPoint(b.x4,b.y4))){
area = Math.abs(b.x1 - a.x1)*Math.abs(b.y1 - b.y2);
}else if((a.containsPoint(b.x1,b.y1) && !a.containsPoint(b.x2,b.y2) && !a.containsPoint(b.x3,b.y3) && a.containsPoint(b.x4,b.y4))){
area = Math.abs(a.x3 - b.x3) * Math.abs(b.y1 - b.y2);
}else if((a.containsPoint(b.x1,b.y1) && !a.containsPoint(b.x2,b.y2) && a.containsPoint(b.x3,b.y3) && !a.containsPoint(b.x4,b.y4))){
area = Math.abs(b.x2 - b.x4) * Math.abs(b.y2 - a.y2);
}
if(b.area() == 0){
area = 0;
}
io.println(area);
io.close();
}
static int intersect(Rect p, Rect q) {
int xOverlap = Math.max(0, Math.min(p.x2, q.x2) - Math.max(p.x1, q.x1));
int yOverlap = Math.max(0, Math.min(p.y2, q.y2) - Math.max(p.y1, q.y1));
return xOverlap * yOverlap;
}
static class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// standard input
public Kattio() { this(System.in, System.out); }
public Kattio(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// USACO-style file input
public Kattio(String problemName) throws IOException {
super(new FileWriter(problemName + ".out"));
r = new BufferedReader(new FileReader(problemName + ".in"));
}
// returns null if no more input
public String next() {
try {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(r.readLine());
return st.nextToken();
} catch (Exception e) { }
return null;
}
public int nextInt() { return Integer.parseInt(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public long nextLong() { return Long.parseLong(next()); }
}
}