Problem:
Convex Cut

As shown in the figure above, cut a convex polygon g by a line p1p2 and print the area of the cut polygon which is on the left-hand side of the line.

g is represented by a sequence of points p1, p2,..., pn where line segments connecting pi and pi+1 (1 ≤ i ≤ n−1) are sides of the convex polygon. The line segment connecting pn and p1 is also a side of the polygon.

Input

The input is given in the following format:

g (the sequence of the points of the polygon)
q (the number of queries = the number of target lines)
1st query
2nd query
:
qth query

g is given as a sequence of points p1,..., pn in the following format:

n
x1 y1
x2 y2
:
xn yn

The first integer n is the number of points. The coordinate of the i-th point pi is given by two integers xi and yi. The coordinates of points are given in the order of counter-clockwise visit of them. Note that all interior angles of given convex polygons are less than or equal to 180.

For each query, a line represented by two points p1 and p2 is given. The coordinates of the points are given by four integers p1x, p1y, p2x and p2y.

Output

For each query, print the area of the cut polygon. The output values should be in a decimal fraction with an error less than 0.00001.

Constraints

3 ≤ n ≤ 100

1 ≤ q ≤ 100

-10000 ≤ xi, yi ≤ 10000

-10000 ≤ p1x,p1y,p2x,p2y ≤ 10000

No point in g will occur more than once.

p1 ≠ p2

Sample Input

4
1 1
4 1
4 3
1 3
2
2 0 2 4
2 4 2 0

Sample Output

2.00000000
4.00000000

Solution in JAVA (judge verdict: Wrong Answer)

import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.NoSuchElementException;

public class Main {
	private static FastScanner sc = new FastScanner();
	static final double ALLOWABLE_ERROR = 0.0000000001;

	public static void main(String[] args) {
		int n = sc.nextInt();
		Point[] p = new Point[n];
		for(int i=0; i<n; i++) {
			p[i] = new Point(sc.nextDouble(), sc.nextDouble());
		}
		
		int q = sc.nextInt();
		StringBuilder ans = new StringBuilder();
		DecimalFormat df = new DecimalFormat("#######0.0000000000");
		for(int i=0; i<q; i++) {
			double p1x = sc.nextDouble();
			double p1y = sc.nextDouble();
			double p2x = sc.nextDouble();
			double p2y = sc.nextDouble();
			
			int l_idx = -1;
			int r_idx = -1;
			boolean mode = true;
			for(int j=0; j<n; j++) {
				if(mode) {
					if((p2x - p1x)*(p[j].y - p1y)-(p[j].x - p1x)*(p2y - p1y) + ALLOWABLE_ERROR < 0) {
						r_idx = j;
						mode = false;
					}
				} else {
					if((p2x - p1x)*(p[j].y - p1y)-(p[j].x - p1x)*(p2y - p1y) >= ALLOWABLE_ERROR) {
						l_idx = j;
						mode = true;
					}
				}
			}
			
			System.out.println("i=" + i);
			System.out.println(l_idx + " " + r_idx);
			
			ArrayList<Point> pa = new ArrayList<>();
			if(l_idx == -1 && r_idx == 0) {
				ans.append(0);
				ans.append("\n");
				continue;
			} else if(l_idx == -1 && r_idx == -1) {
				l_idx = 0;
				r_idx = 0;
				for(int j=0; j<n; j++) {
					pa.add(p[j]);
				}
			} else if(l_idx == -1) {
				l_idx = 0;
				for(int j=l_idx; j%n != r_idx; j++) {
					pa.add(p[j%n]);
				}
				Point p1 = new Point(p1x, p1y);
				Point p2 = new Point(p2x, p2y);

				pa.add(0, crossPoint(p1, p2, p[(l_idx-1+n)%n], p[l_idx]));
				pa.add(crossPoint(p1, p2, p[(r_idx-1+n)%n], p[r_idx]));
			} else {
				for(int j=l_idx; j%n != r_idx; j++) {
					pa.add(p[j%n]);
				}
				Point p1 = new Point(p1x, p1y);
				Point p2 = new Point(p2x, p2y);

				pa.add(0, crossPoint(p1, p2, p[(l_idx-1+n)%n], p[l_idx]));
				pa.add(crossPoint(p1, p2, p[(r_idx-1+n)%n], p[r_idx]));
			}
			
			
			
			
			
			double sum_area = 0;
			for(int j=0; j<pa.size(); j++) {
				Point origin = new Point(0, 0);
				if(checkCounterClockwise(pa.get(j), pa.get((j+1)%pa.size()), origin)) {
					sum_area += area(pa.get(j), pa.get((j+1)%pa.size()), origin);
				} else {
					sum_area -= area(pa.get(j), pa.get((j+1)%pa.size()), origin);
				}
			}
			
			if(Double.isNaN(sum_area)) {
				ans.append(0);
				ans.append("\n");
				continue;
			}
			
			ans.append(df.format(sum_area));
			ans.append("\n");
		}
		
		System.out.print(ans);
	}
	
	static Point crossPoint(Point p0, Point p1, Point p2, Point p3) {
		double a1 = (p1.y - p0.y) / (p1.x - p0.x);
		double a2 = (p3.y - p2.y) / (p3.x - p2.x);
		double b1 = p0.y - a1 * p0.x;
		double b2 = p2.y - a2 * p2.x;
		
		double x;
		double y;
		if(p1.x == p0.x) {
			x = p0.x;
			y = a2 * x + b2;
		} else if(p3.x == p2.x) {
			x = p2.x;
			y = a1 * x + b1;
		} else {
			x = (b1 - b2)/(a2 - a1);
			y = a1 * x + b1;
		}
		
		return new Point(x, y);
	}
	
	static boolean checkCounterClockwise(Point p1, Point p2, Point p3) {
		double a = (p2.y - p1.y) / (p2.x - p1.x);
		double b = p2.y - a * p2.x;
		if(p1.x == p2.x) {
			if((p1.y < p2.y && p3.x < p1.x) || (p1.y > p2.y && p3.x > p1.x)) {
				return true;
			} else {
				return false;
			}	
			
		} else {
			if((p1.x < p2.x && p3.y - (a * p3.x + b) > 0) || (p1.x > p2.x && p3.y - (a * p3.x + b) < 0)) {
				return true;
			} else {
				return false;
			}				
		}
	}
	
	static double area(Point p1, Point p2, Point p3) {
		double a = distance(p1, p2);
		double b = distance(p2, p3);
		double c = distance(p3, p1);
		double s = (a + b + c) / 2;
		if(s < Math.max(a, Math.max(b, c)) + ALLOWABLE_ERROR && s + ALLOWABLE_ERROR > Math.max(a, Math.max(b, c))) {
			return 0;
		}
		return Math.sqrt(s * (s - a) * (s - b) * (s - c));
	}
	
	static double distance(Point p1, Point p2) {
		return Math.sqrt((p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)*(p2.y-p1.y));
	}

	static class Point {
		double x;
		double y;
		double ang;
		Point(double x, double y) {
			this.x = x;
			this.y = y;
		}
	}
	
	static class FastScanner {
        private final InputStream in = System.in;
        private final byte[] buffer = new byte[1024];
        private int ptr = 0;
        private int buflen = 0;
        private boolean hasNextByte() {
            if(ptr < buflen) {
                return true;
            } else {
                ptr = 0;
                try {
                    buflen = in.read(buffer);
                } catch(IOException e) {
                    e.printStackTrace();
                }
                if(buflen <= 0) {
                    return false;
                }
            }
            return true;
        }
        private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
        private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
        private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
        public boolean hasNext() { skipUnprintable(); return hasNextByte();}
        public String next() {
            if (!hasNext()) throw new NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while(isPrintableChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }
        public long nextLong() {
            return Long.parseLong(next());
        }
        public int nextInt(){
            return Integer.parseInt(next());
        }
        public double nextDouble(){
            return Double.parseDouble(next());
        }
    }
}