QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#234277 | #7693. Convex Hull Extension | Qingyu | AC ✓ | 242ms | 55080kb | Java11 | 34.1kb | 2023-11-01 15:30:27 | 2023-11-01 15:30:28 |
Judging History
answer
// Liam Keliher, 2023
// Solution for NENA 2023 problem "Convex Hull Extension" (convexhullextension)
//
// Very case-based.
// Flip/rotate each side (along with the point before and the point after) so that either:
// (i) the side is horizontal on top of hull (CASE_TOP), or
// (ii) the side has negative slope and is on top-right of hull (CASE_TOP_RIGHT)
// If the side under consideration is labelled S(p1,p2), then the preceding and following
// points are labelled p0 and p3.
// Determine whether the rays R(p0,p1) and R(p3,p2):
// - diverge (in which case there are infinitely many extension points)
// - are parallel (in which case there are either zero or infinitely many extension points)
// - converge (in which case there are finitely many extension points, so count them)
//
// Terminology used in code comments below:
// In CASE_TOP_RIGHT, there are two ways S(p0,p1) can have negative slope (similarly for S(p2,p3)):
// - negative-slope-lower: p0 is lower than p1
// - negative-slope-upper: p0 is higher than p1
import java.io.*;
public class ConvexKeliher {
static final int CASE_TOP = 1;
static final int CASE_TOP_RIGHT = 2;
static final String INFINITELY_MANY = "infinitely many";
//--------------------------------------------------------------------
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
if (n == 3) {
System.out.println(INFINITELY_MANY);
return;
} // if
int[][] points = new int[n][2];
for (int i = 0; i < n; i++) {
String[] tokens = br.readLine().split(" ");
int x = Integer.parseInt(tokens[0]);
int y = Integer.parseInt(tokens[1]);
points[i][0] = x;
points[i][1] = y;
} // for i
// For each side S(p1,p2) of the convex hull, store the 4 points (p0,p1,p2,p3)
// (counterclockwise order), and transform these points if necessary so that
// the transformed side (p1,p2) is either on the top of the hull (flat) or
// is on the top-right of the convex hull (==> negative slope). Record which
// of these two cases applies in the cases[] array.
int[][][] transformed = new int[n][4][2];
int[] cases = new int[n];
for (int i = 0; i < n; i++) {
int[][] quad = new int[4][2];
quad[0][0] = points[i][0];
quad[0][1] = points[i][1];
quad[1][0] = points[(i+1)%n][0];
quad[1][1] = points[(i+1)%n][1];
quad[2][0] = points[(i+2)%n][0];
quad[2][1] = points[(i+2)%n][1];
quad[3][0] = points[(i+3)%n][0];
quad[3][1] = points[(i+3)%n][1];
cases[i] = transformIfNecessary(quad);
for (int j = 0; j < 4; j++) {
transformed[i][j][0] = quad[j][0];
transformed[i][j][1] = quad[j][1];
} // for j
} // for i
// Check for divergence
for (int i = 0; i < n; i++) {
if (divergent(transformed[i], cases[i])) {
System.out.println(INFINITELY_MANY);
return;
} // if
} // for i
// Check for parallel lines that enclose infinitely many points
for (int i = 0; i < n; i++) {
if (parallelAndEncloseInfinitelyManyPoints(transformed[i], cases[i])) {
System.out.println(INFINITELY_MANY);
return;
} // if
} // for i
// Everything is convergent, or parallel but enclosing zero points, so the answer is finite
long answer = 0;
for (int i = 0; i < n; i++) {
// At this point in the program, parallel means enclosing zero points, so skip
if (parallel(transformed[i])) {
continue;
} // if
int currCase = cases[i];
if (currCase == CASE_TOP) {
answer += countExtensionPointsTop(transformed[i]);
} // if
else { // currCase == CASE_TOP_RIGHT
answer += countExtensionPointsTopRight(transformed[i]);
} // else
} // for i
System.out.println(answer);
} // main(String[])
//--------------------------------------------------------------------
// Rotate and/or reflect p0, p1, p2, p3 (if necessary) so that S(p1,p2)
// is on top of hull or top-right of hull, and return CASE_TOP or
// CASE_TOP_RIGHT as appropriate. When finished, points in quad[][]
// (possibly transformed) are still in counterclockwise order.
static int transformIfNecessary(int[][] quad) {
int x0 = quad[0][0];
int y0 = quad[0][1];
int x1 = quad[1][0];
int y1 = quad[1][1];
int x2 = quad[2][0];
int y2 = quad[2][1];
int x3 = quad[3][0];
int y3 = quad[3][1]; // never used
// Case 1: line segment S(p1,p2) is initially horizontal or vertical
// Translate as necessary so that:
// - S(p1,p2) is horizontal
// - p0, p3 are below p1, p2
// - if only one of S(p0,p1), S(p2,p3) is vertical, then it is S(p0,p1)
// Subcase 1-A: S(p1,p2) is initially horizontal at top of hull
if (y1 == y2 && y0 < y1) {
if (x0 != x1 && x2 == x3) {
swapLeftToRight(quad);
} // if
return CASE_TOP;
} // if
// Subcase 1-B: S(p1,p2) is initially horizontal at bottom of hull
else if (y1 == y2 && y0 > y1) {
swapBottomToTop(quad);
if (x0 != x1 && x2 == x3) {
swapLeftToRight(quad);
} // if
return CASE_TOP;
} // else if
// Subcase 1-C: S(p1,p2) is initially vertical on right side of hull
else if (x1 == x2 && x0 < x1) {
rotateCounterclockwise90Degrees(quad);
if (x0 != x1 && x2 == x3) {
swapLeftToRight(quad);
} // if
return CASE_TOP;
} // else if
// Subcase 1-D: S(p1,p2) is initially vertical on left side of hull
else if (x1 == x2 && x0 > x1) {
rotateClockwise90Degrees(quad);
if (x0 != x1 && x2 == x3) {
swapLeftToRight(quad);
} // if
return CASE_TOP;
} // else if
// Case 2: line segment S(p1,p2) is *not* initially horizontal or vertical
// Translate as necessary so that:
// - S(p1,p2) is has negative slope and is positioned as if in top-right part of hull
// - this means that p0 and p3 both lie strictly to the left of line L(p1,p2)
// Subcase 2-A: S(p1,p2) intially in top-left of hull
else if (x1 > x2 && y1 > y2) {
rotateClockwise90Degrees(quad);
return CASE_TOP_RIGHT;
} // else if
// Subcase 2-B: S(p1,p2) intially in bottom-left of hull
else if (x1 < x2 && y1 > y2) {
rotateClockwise90Degrees(quad);
rotateClockwise90Degrees(quad);
return CASE_TOP_RIGHT;
} // else if
// Subcase 2-C: S(p1,p2) intially in bottom-right of hull
else if (x1 < x2 && y1 < y2) {
rotateCounterclockwise90Degrees(quad);
return CASE_TOP_RIGHT;
} // else if
// Subcase 2-D: S(p1,p2) initially in top-right of hull
// (all is right with the world)
return CASE_TOP_RIGHT;
} // transformIfNecessary(int[][])
//--------------------------------------------------------------------
// Assumption: incoming points are in counterclockwise order
// - after transformation, they are still in counterclockwise order
static void swapLeftToRight(int[][] quad) {
// negate the x-values
quad[0][0] = -quad[0][0];
quad[1][0] = -quad[1][0];
quad[2][0] = -quad[2][0];
quad[3][0] = -quad[3][0];
// swap p0,p3 and p1,p2
int[] temp = quad[0];
quad[0] = quad[3];
quad[3] = temp;
temp = quad[1];
quad[1] = quad[2];
quad[2] = temp;
} // swapLeftToRight(int[][])
//--------------------------------------------------------------------
// Assumption: incoming points are in counterclockwise order
// - after transformation, they are still in counterclockwise order
// - could also be accomplished with two 90-degree rotations
static void swapBottomToTop(int[][] quad) {
// negate the y-values
quad[0][1] = -quad[0][1];
quad[1][1] = -quad[1][1];
quad[2][1] = -quad[2][1];
quad[3][1] = -quad[3][1];
// negate the x-values
quad[0][0] = -quad[0][0];
quad[1][0] = -quad[1][0];
quad[2][0] = -quad[2][0];
quad[3][0] = -quad[3][0];
} // swapBottomToTop(int[][])
//--------------------------------------------------------------------
// (x,y) -> (-y,x)
static void rotateCounterclockwise90Degrees(int[][] quad) {
for (int i = 0; i < 4; i++) {
int x = quad[i][0];
int y = quad[i][1];
quad[i][0] = -y;
quad[i][1] = x;
} // for i
} // rotateCounterclockwise90Degrees(int[][])
//--------------------------------------------------------------------
// (x,y) -> (y,-x)
static void rotateClockwise90Degrees(int[][] quad) {
for (int i = 0; i < 4; i++) {
int x = quad[i][0];
int y = quad[i][1];
quad[i][0] = y;
quad[i][1] = -x;
} // for i
} // rotateClockwise90Degrees(int[][])
//--------------------------------------------------------------------
// Returns true if rays R(p0,p1) and R(p3,p2) are divergent, and false otherwise
// - parallel rays are handled separately (by parallelAndEncloseInfinitelyManyPoints())
static boolean divergent(int[][] quad, int currCase) {
int x0 = quad[0][0];
int y0 = quad[0][1];
int x1 = quad[1][0];
int y1 = quad[1][1];
int x2 = quad[2][0];
int y2 = quad[2][1];
int x3 = quad[3][0];
int y3 = quad[3][1];
long[] slope01 = getSlope(x0, y0, x1, y1);
long rise01 = slope01[0];
long run01 = slope01[1];
long[] slope23 = getSlope(x2, y2, x3, y3);
long rise23 = slope23[0];
long run23 = slope23[1];
// NOTE: Slope is negative iff rise < 0. On the other hand, slope >= 0 requires
// rise >= 0 *and* run != 0 (since a vertical slope is indicated by [1,0])
if (currCase == CASE_TOP) {
// Subcase A: S(p0,p1) has negative slope
if (rise01 < 0) {
// Sub-subcase: S(p2,p3) has negative slope
if (rise23 < 0 && rise01*run23 < rise23*run01) { // slope01 < slope23 (but larger in abs value)
return true;
} // else if
} // if
// Subcase B: S(p0,p1) is vertical
else if (run01 == 0) {
// Sub-subcase: S(p2,p3) has negative slope
if (rise23 < 0) {
return true;
} // if
} // if
// Subcase C: S(p0,p1) has positive slope
else if (run01 != 0 && rise01 > 0) {
// Sub-subcase(s): S(p2,p3) is vertical or has negative slope
if (run23 == 0 || rise23 < 0) {
return true;
} // if
// Sub-subcase: S(p2,p3) has positive slope
else if (run23 != 0 && rise23 > 0 && rise01*run23 < rise23*run01) { // slope01 < slope23
return true;
} // else if
} // else if
} // if
if (currCase == CASE_TOP_RIGHT) {
// Subcase A: S(p0,p1) is negative-slope-lower
if (rise01 < 0 && y0 < y1) {
// Sub-subcase: S(p2,p3) is negative-slope-lower
if (rise23 < 0 && y3 < y2 && rise01*run23 < rise23*run01) { // slope01 < slope23 (but larger in abs value)
return true;
} // if
} // if
// Subcase B: S(p0,p1) is vertical
else if (run01 == 0) {
// Sub-subcase: S(p2,p3) is negative-slope-lower
if (rise23 < 0 && y3 < y2) {
return true;
} // if
} // else if
// Subcase C: S(p0,p1) has slope >= 0
else if (run01 != 0 && rise01 >= 0) {
// Sub-subcase(s): S(p2,p3) is vertical or negative-slope-lower
if (run23 == 0 || (rise23 < 0 && y3 < y2)) {
return true;
} // if
// Sub-subcase: S(p2,p3) has positive slope (not 0)
else if (rise23 > 0 && rise01*run23 < rise23*run01) { // slope01 < slope23
return true;
} // else if
} // else if
// Subcase D: S(p0,p1) is negative-slope-upper
else if (rise01 < 0 && y0 > y1) {
// Sub-subcase(s): S(p2,p3) is vertical or negative-slope-lower or has slope >= 0
if (run23 == 0 || (rise23 < 0 && y3 < y2) || (run23 != 0 && rise23 >= 0)) {
return true;
} // if
// Sub-subcase: S(p2,p3) is negative-slope-upper
if (rise23 < 0 && y3 > y2 && rise01*run23 < rise23*run01) { // slope01 < slope23 (but larger in abs value)
return true;
} // if
} // else if
} // if
return false;
} // divergent(int[][],int)
//--------------------------------------------------------------------
// Returns true if rays R(p0,p1) and R(p3,p2) are parallel, and false otherwise
static boolean parallel(int[][] quad) {
int x0 = quad[0][0];
int y0 = quad[0][1];
int x1 = quad[1][0];
int y1 = quad[1][1];
int x2 = quad[2][0];
int y2 = quad[2][1];
int x3 = quad[3][0];
int y3 = quad[3][1];
long[] slope01 = getSlope(x0, y0, x1, y1);
long[] slope23 = getSlope(x2, y2, x3, y3);
long rise01 = slope01[0];
long run01 = slope01[1];
long rise23 = slope23[0];
long run23 = slope23[1];
return rise01 == rise23 && run01 == run23;
} // parallel(int[][])
//--------------------------------------------------------------------
// Returns true if rays R(p0,p1) and R(p3,p2) are parallel and enclose
// infinitely many points, and false otherwise
// - if parallel and don't enclose infinitely many points, then enclose zero points
static boolean parallelAndEncloseInfinitelyManyPoints(int[][] quad, int currCase) {
int x0 = quad[0][0];
int y0 = quad[0][1];
int x1 = quad[1][0];
int y1 = quad[1][1];
int x2 = quad[2][0];
int y2 = quad[2][1];
int x3 = quad[3][0];
int y3 = quad[3][1];
long[] slope01 = getSlope(x0, y0, x1, y1);
long[] slope23 = getSlope(x2, y2, x3, y3);
long rise01 = slope01[0];
long run01 = slope01[1];
long rise23 = slope23[0];
long run23 = slope23[1];
// Return false if rays are not parallel
if (rise01 != rise23 || run01 != run23) {
return false;
} // if
if (currCase == CASE_TOP) {
// Subcase A: rays are distance >= 2 apart horizontally
if (x1 - x2 >= 2) {
return true;
} // if
// Subcase B: rays are distance 1 apart (horizontally)
// The only way for the rays *not* to enclose infinitely many points is if:
// (a) slope = +/- 1/m, where m is a positive integer
// or
// (b) the rays are vertical, in which case rise = 1
// (since we use [rise, run] = [1, 0] for a vertical slope)
else {
return !(rise01 == 1 || rise01 == -1);
} // else
} // if
else { // currCase == CASE_TOP_RIGHT
// Subcase A: rays are vertical
// Rays enclose infinitely many points iff they are distance >= 2 apart horizontally
if (run01 == 0) {
return x1 - x2 >= 2;
} // if
// Subcase B: rays are horizontal
// Rays enclose infinitely many points iff they are distance >= 2 apart vertically
else if (rise01 == 0) {
return y2 - y1 >= 2;
} // else if
// Subcase C: rays are neither vertical nor horizontal
else {
// Find x-coord of point p where R(p0,p1) has y value = y2
long pNumer = run01*(y2 - y1) + rise01*x1;
long pDenom = rise01;
// If negative-slope-lower or negative-slope-upper, then pDenom = rise01 < 0
if (pDenom < 0) {
pNumer = -pNumer;
pDenom = -pDenom; // now pDenom > 0
} // if
// Doesn't hurt to reduce pNumer/pDenom
long g = gcd(Math.abs(pNumer), pDenom);
pNumer /= g;
pDenom /= g;
long gapNumer = pNumer - x2*pDenom;
long gapDenom = pDenom;
// If negative-slope-upper, then gapNumer < 0
if (rise01 < 0 && y0 > y1) {
gapNumer = -gapNumer;
} // if
g = gcd(gapNumer, gapDenom);
gapNumer /= g;
gapDenom /= g;
return processParallelRays(gapNumer, gapDenom, rise01, run01);
} // else
} // else
} // parallelAndEncloseInfinitelyManyPoints(int[][],int)
//--------------------------------------------------------------------
// Returns true if the two parallel rays specified by the input enclose
// infinitely many points, and false otherwise
// - one ray is anchored at (0, 0), the other at (gapNumer/gapDenom, 0)
// - gapNumer/gapDenom > 0 (with gapNume > 0 and gapDenom > 0)
// - slope of both rays is rise/run
// - incoming slope is positive or negative (never 0 or infinite), so run > 0
// - WLOG, if the slope is negative, we make it positive
static boolean processParallelRays(long gapNumer, long gapDenom, long rise, long run) {
if (gapNumer <= 0 || gapDenom <= 0 || rise == 0 || run <= 0) {
throw new IllegalArgumentException("Inside processParallelRays(): at least one argument is <= 0");
} // if
// First, ensure slope is positive (WLOG)
rise = Math.abs(rise);
// Case 1: gapNumer/gapDenom > 1
// - rays enclose infinitely many points
if (gapNumer > gapDenom) {
return true;
} // if
// Case 2: gapNumer/gapDenom = 1
// - rays enclose infinitely many points iff slope is NOT equal to +/- 1/m, where m is a positive integer
else if (gapNumer == gapDenom) {
return !(rise == 1 || rise == -1);
} // else if
// Case 3: gapNumer/gapDenom < 1
// - follow left ray and right ray upward until left ray is again at a point with integer coords
// - at each integer y value, check whether an integer x value lies between the rays
// - if this ever happens, then the rays enclose infinitely many points; otherwise they don't
else { // gapNumer < gapDenom
int yUpper = (int)Math.abs(rise);
for (int y = 1; y < yUpper; y++) {
long xLeftNumer = run*y;
long xLeftDenom = rise;
// If xLeftNumer/xLeftDenom is an integer, add 1; if not, round up, so also add 1
long xLeftBumpUp = xLeftNumer/xLeftDenom + 1;
long xRightNumer = gapNumer*rise + gapDenom*run*y;
long xRightDenom = gapDenom*rise;
long xRightBumpDown = xRightNumer/xRightDenom;
if (xRightNumer % xRightDenom == 0) { // xRightNumer/xRightDenom is an integer
xRightBumpDown--;
} // if
if (xLeftBumpUp <= xRightBumpDown) {
return true;
} // if
} // for y
return false;
} // else
} // processParallelRays(long,long,long,long)
//--------------------------------------------------------------------
// Count the number of extension points when S(p1,p2) is horizontal
// - only called when rays R(p0,p1) and R(p3,p2) are convergent
static long countExtensionPointsTop(int[][] quad) {
int x0 = quad[0][0];
int y0 = quad[0][1];
int x1 = quad[1][0];
int y1 = quad[1][1];
int x2 = quad[2][0];
int y2 = quad[2][1];
int x3 = quad[3][0];
int y3 = quad[3][1];
long[] slope01 = getSlope(x0, y0, x1, y1);
long rise01 = slope01[0];
long run01 = slope01[1];
long[] slope23 = getSlope(x2, y2, x3, y3);
long rise23 = slope23[0];
long run23 = slope23[1];
long numExtPoints = 0;
long[] yCoord = yCoordOfIntersection(rise23, run23, x3, y3, rise01, run01, x0, y0);
long yMeetNumer = yCoord[0];
long yMeetDenom = yCoord[1];
long yMeet;
if (yMeetNumer % yMeetDenom == 0) { // equivalently, yMeetDenom = 1
yMeet = yMeetNumer/yMeetDenom - 1;
} // if
else { // yMeetNumer/yMeetDenom is not an integer
yMeet = yMeetNumer/yMeetDenom; // rounds toward 0
if (yMeetNumer < 0) {
yMeet--; // compensate for the fact that / rounded up (we want to round down here)
} // if
} // else
for (long y = y1 + 1; y <= yMeet; y++) { // NOTE: start at y1 + 1
numExtPoints += countPointsBetween(y, rise23, run23, x3, y3, rise01, run01, x0, y0);
} // for y
return numExtPoints;
} // countExtensionPointsTop(int[][])
//--------------------------------------------------------------------
// Count the number of extension points when S(p1,p2) is on top-right of hull
// - only called when rays R(p0,p1) and R(p3,p2) are convergent
static long countExtensionPointsTopRight(int[][] quad) {
int x0 = quad[0][0];
int y0 = quad[0][1];
int x1 = quad[1][0];
int y1 = quad[1][1];
int x2 = quad[2][0];
int y2 = quad[2][1];
int x3 = quad[3][0];
int y3 = quad[3][1];
long[] slope01 = getSlope(x0, y0, x1, y1);
long rise01 = slope01[0];
long run01 = slope01[1];
long[] slope23 = getSlope(x2, y2, x3, y3);
long rise23 = slope23[0];
long run23 = slope23[1];
long[] slope12 = getSlope(x1, y1, x2, y2);
long rise12 = slope12[0];
long run12 = slope12[1];
// NOTE: Slope is negative iff rise < 0. On the other hand, slope >= 0 requires
// rise >= 0 *and* run != 0 (since a vertical slope is indicated by [1,0])
long numExtPoints = 0;
long[] yCoord = yCoordOfIntersection(rise23, run23, x3, y3, rise01, run01, x0, y0);
long yMeetNumer = yCoord[0];
long yMeetDenom = yCoord[1];
long yMeetFloor = yMeetNumer/yMeetDenom; // rounds toward 0
boolean yMeetIsInteger = yMeetNumer % yMeetDenom == 0;
if (yMeetNumer < 0 && !yMeetIsInteger) { // yMeet is negative and not an integer
yMeetFloor--; // compensate for the fact that / rounded up (we want to round down here)
} // if
// Subcase A: S(p0,p1) is negative-slope-lower or vertical or has positive slope
if ((rise01 < 0 && y0 < y1) || (run01 == 0) || (rise01 > 0)) {
// Sub-subcase(s): S(p2,p3) is negative-slope-upper or horizontal (so y1 < yMeet <= y2)
if ((rise23 < 0 && y3 > y2) || rise23 == 0) {
// If S(p2,p3) is horizontal, then yMeet = y2, so want yMeetFloor = y2 - 1
if (rise23 == 0) {
yMeetFloor--;
} // if
// - lower part lies between S(p1,p2) on left and R(p0,p1) on right
for (long y = y1 + 1; y <= yMeetFloor; y++) { // NOTE: start at y1 + 1
numExtPoints += countPointsBetween(y, rise12, run12, x1, y1, rise01, run01, x0, y0);
} // for y
// - upper part lies between S(p1,p2) on left and R(p3,p2) on right
for (long y = yMeetFloor + 1; y < y2; y++) { // NOTE: < y2
numExtPoints += countPointsBetween(y, rise12, run12, x1, y1, rise23, run23, x3, y3);
} // for y
} // if
// Sub-subcase: S(p2,p3) is negative-slope-lower or has positive slope (so yMeet > y2)
// - S(p2,p3) cannot be vertical, since if only one of S(p0,p1), S(p2,p3) is vertical, it is always S(p0,p1) (see transformIfNecessary())
// - if S(p2,p3) is negative-slopel-lower, then S(p0,p1) must also be, since R(p0,p1) and R(p3,p2) are convergent
else if ((rise23 < 0 && y3 < y2) || (run23 != 0 && rise23 > 0)) { // don't actually need to check run23 !== 0, since S(p2,p3) shouldn't be vertical here
// - lower part lies between S(p1,p2) on left and R(p0,p1) on right
for (long y = y1 + 1; y <= y2; y++) { // NOTE: start at y1 + 1
numExtPoints += countPointsBetween(y, rise12, run12, x1, y1, rise01, run01, x0, y0);
} // for y
// - upper part lies between R(p3,p2) on left and R(p0,p1) on right
long upperLimit = yMeetFloor;
if (yMeetIsInteger) {
upperLimit--;
} // if
for (long y = y2 + 1; y <= upperLimit; y++) { // NOTE: start at y2 + 1
numExtPoints += countPointsBetween(y, rise23, run23, x3, y3, rise01, run01, x0, y0);
} // for y
} // else if
} // if
// Subcase B: S(p0,p1) is horizontal
else if (rise01 == 0) {
// Sub-subcase: S(p2,p3) must be negative-slope-upper, since R(p0,p1) and R(p3,p2) are convergent
// - points of interest lie between S(p1,p2) on left and R(p3,p2) on right
for (long y = y1 + 1; y < y2; y++) { // NOTE: start at y1 + 1, stop before y2
numExtPoints += countPointsBetween(y, rise12, run12, x1, y1, rise23, run23, x3, y3);
} // for y
} // else if
// Subcase C: S(p0,p1) is negative-slope-upper
else if (rise01 < 0 && y0 > y1) {
// Sub-subcase: S(p2,p3) must be negative-slope-upper, since R(p0,p1) and R(p3,p2) are convergent
// - upper part lies between S(p1,p2) on left and R(p3,p2) on right
for (long y = y1; y < y2; y++) { // NOTE: include y1, exclude y2
numExtPoints += countPointsBetween(y, rise12, run12, x1, y1, rise23, run23, x3, y3);
} // for y
// - lower part lies between R(p0,p1) on left and R(p3,p2) on right
long lowerLimit = yMeetFloor;
for (long y = lowerLimit + 1; y < y1; y++) { // NOTE: start at lowerLimit + 1, exclue y1 (already included immediately above)
numExtPoints += countPointsBetween(y, rise12, run12, x1, y1, rise23, run23, x3, y3);
} // for y
} // else if
return numExtPoints;
} // countExtensionPointsTopRight(int[][])
//--------------------------------------------------------------------
// Count points with integer coordinates that lie *strictly* between the left line and
// the right line at height y
// - handles cases in which one of the two lines is vertical:
// - if, e.g., left line is vertical, then riseL = 1, runL = 0
// - so xLNumer = 0 + riseL*xL and xLDenom = riseL, so xLNumer/xLDenom = xL (correct)
// Assumption: neither line is horizontal (so riseL > 0 and riseR > 0)
static long countPointsBetween(long y, long riseL, long runL, int xL, int yL, long riseR, long runR, int xR, int yR) {
long numPoints = 0;
long xLNumer = runL*(y - yL) + riseL*xL;
long xLDenom = riseL;
if (xLDenom < 0) {
xLNumer = -xLNumer;
xLDenom = -xLDenom;
} // if
long xRNumer = runR*(y - yR) + riseR*xR;
long xRDenom = riseR;
if (xRDenom < 0) {
xRNumer = -xRNumer;
xRDenom = -xRDenom;
} // if
boolean leftIsInteger = (xLNumer % xLDenom == 0);
boolean rightIsInteger = (xRNumer % xRDenom == 0);
long widthTrunc = (xLDenom*xRNumer - xLNumer*xRDenom)/(xLDenom*xRDenom);
boolean widthIsInteger = (xLDenom*xRNumer - xLNumer*xRDenom) % (xLDenom*xRDenom) == 0;
// In theory this should never happen
if (widthIsInteger && widthTrunc == 0) {
return 0;
} // if
if (widthIsInteger) {
if (leftIsInteger && rightIsInteger) { // would suffice to check one of leftIsInteger, rightIsInteger
numPoints = widthTrunc - 1;
} // if
else { // neither endpoint is an integer
numPoints = widthTrunc;
} // else
} // if
else { // width is not an integer
if (leftIsInteger || rightIsInteger) { // at most one endpoint can be an integer
numPoints = widthTrunc;
} // if
else { // neither endpoint is an integer
// ceiling the left endpoint
long xLeftCeil = xLNumer/xLDenom;
if (xLNumer > 0) { // left endpoint is positive
xLeftCeil++; // compensate for fact that / rounded down (toward 0)
} // if
// floor the right endpoint
long xRightFloor = xRNumer/xRDenom;
if (xRNumer < 0) { // right endpoint is negative
xRightFloor--; // compensate for the fact that / rounded up (toward 0)
} // if
numPoints = xRightFloor - xLeftCeil + 1;
} // else
} // else
return numPoints;
} // countPointsBetween(long,long,long,int,int,long,long,int,int)
//--------------------------------------------------------------------
// Returns y-coord of intersection between the two lines as a fraction [numer, denom]
// in reduced form, with denom > 0
// - handles cases in which one of the lines is vertical
// - lines cannot be parallel
static long[] yCoordOfIntersection(long riseL, long runL, int xL, int yL, long riseR, long runR, int xR, int yR) {
long yMeetNumer;
long yMeetDenom;
// Case 1: neither line is vertical
if (runL != 0 && runR != 0) {
yMeetNumer = riseR*runR*runL*yL + riseR*riseL*runR*(xR - xL) - riseL*runR*runR*yR;
yMeetDenom = runR*(riseR*runL - riseL*runR);
} // if
// Case 2: right line is vertical
else if (runR == 0) {
yMeetNumer = riseL*xR + runL*yL - riseL*xL;
yMeetDenom = runL;
} // else if
// Case 3: left line is vertical
else { // runL == 0
yMeetNumer = riseR*xL + runR*yR - riseR*xR;
yMeetDenom = runR;
} // else
if (yMeetDenom < 0) {
yMeetNumer = -yMeetNumer;
yMeetDenom = -yMeetDenom;
} // if
long g = gcd(Math.abs(yMeetNumer), yMeetDenom);
yMeetNumer /= g;
yMeetDenom /= g;
return new long[]{yMeetNumer, yMeetDenom};
} // yCoordOfIntersection(long,long,int,int,long,long,int,int)
//--------------------------------------------------------------------
// Returns a length-2 array containing the slope of the line through
// (x1,y2) and (x2,y2) (in the form [rise, run]). The slope is a fraction,
// rise/run, in reduced form, with the convention that the denominator
// is never negative. A vertical line is assigned slope 1/0 (never -1/0).
// - Assumption: (x1,y1) != (x2,y2) (in which case slope is undefined)
// - returns an array of long to help with any overflow avoidance later
static long[] getSlope(int x1, int y1, int x2, int y2) {
long rise = y2 - y1;
long run = x2 - x1;
if (run == 0) { // special case: x1 == x2
return new long[]{1, 0};
} // if
else {
if (run < 0) { // convention: run should always be positive
rise = -rise;
run = -run;
} // if
long g = gcd(Math.abs(rise), run);
rise /= g;
run /= g;
return new long[]{rise, run};
} // else
} // getSlope(int,int,int,int)
//--------------------------------------------------------------------
// Assumption: a,b >= 0, not both 0
static long gcd(long a, long b) {
if (a < 0 || b < 0 || (a == 0 && b == 0)) {
throw new IllegalArgumentException("gcd() called with invalid arguments.");
} // if
while (b != 0) {
long rem = a % b;
a = b;
b = rem;
} // while
return a;
} // gcd(long,long)
//--------------------------------------------------------------------
static void printTransformed(int[][] quad) {
System.out.print("p0 = (" + quad[0][0] + "," + quad[0][1] + ") ");
System.out.print("p1 = (" + quad[1][0] + "," + quad[1][1] + ") ");
System.out.print("p2 = (" + quad[2][0] + "," + quad[2][1] + ") ");
System.out.println("p3 = (" + quad[3][0] + "," + quad[3][1] + ")");
} // printTransformed(int[])
//--------------------------------------------------------------------
} // class ConvexKeliher
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 51ms
memory: 48756kb
input:
5 0 2 -2 0 -1 -3 1 -3 2 1
output:
23
result:
ok single line: '23'
Test #2:
score: 0
Accepted
time: 51ms
memory: 49820kb
input:
4 -7 -7 7 -7 7 7 -7 7
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #3:
score: 0
Accepted
time: 44ms
memory: 48788kb
input:
4 -1000 1000 -1000 999 -999 999 -999 1000
output:
0
result:
ok single line: '0'
Test #4:
score: 0
Accepted
time: 124ms
memory: 51164kb
input:
6 0 -901 900 -900 900 900 0 901 -900 900 -900 -900
output:
1457999998
result:
ok single line: '1457999998'
Test #5:
score: 0
Accepted
time: 131ms
memory: 54508kb
input:
6 900 -900 901 0 900 900 -900 900 -901 0 -900 -900
output:
1457999998
result:
ok single line: '1457999998'
Test #6:
score: 0
Accepted
time: 59ms
memory: 49004kb
input:
6 0 0 400 1 400 2 0 3 -400 2 -400 1
output:
1596
result:
ok single line: '1596'
Test #7:
score: 0
Accepted
time: 151ms
memory: 54532kb
input:
6 0 -901 900 -899 900 900 0 901 -900 900 -900 -899
output:
970921796
result:
ok single line: '970921796'
Test #8:
score: 0
Accepted
time: 53ms
memory: 52376kb
input:
6 2 -2 401 399 399 401 -2 2 -401 -399 -399 -401
output:
4794
result:
ok single line: '4794'
Test #9:
score: 0
Accepted
time: 54ms
memory: 46684kb
input:
6 399 -401 401 -399 2 2 -399 401 -401 399 -2 -2
output:
4794
result:
ok single line: '4794'
Test #10:
score: 0
Accepted
time: 43ms
memory: 48840kb
input:
4 -1 -1 -2 -2 -2 -3 -1 -2
output:
0
result:
ok single line: '0'
Test #11:
score: 0
Accepted
time: 41ms
memory: 49332kb
input:
4 0 0 0 1 -1 2 -1 1
output:
0
result:
ok single line: '0'
Test #12:
score: 0
Accepted
time: 55ms
memory: 52724kb
input:
48 5 -70 14 -68 22 -66 31 -63 39 -58 46 -52 52 -46 58 -39 63 -31 66 -22 68 -14 70 -5 70 5 68 14 66 22 63 31 58 39 52 46 46 52 39 58 31 63 22 66 14 68 5 70 -5 70 -14 68 -22 66 -31 63 -39 58 -46 52 -52 46 -58 39 -63 31 -66 22 -68 14 -70 5 -70 -5 -68 -14 -66 -22 -63 -31 -58 -39 -52 -46 -46 -52 -39 -58 ...
output:
36
result:
ok single line: '36'
Test #13:
score: 0
Accepted
time: 46ms
memory: 48616kb
input:
4 -10 -10 -11 11 -11 10 -10 -11
output:
0
result:
ok single line: '0'
Test #14:
score: 0
Accepted
time: 61ms
memory: 50140kb
input:
4 10 -10 10 -11 11 10 11 11
output:
0
result:
ok single line: '0'
Test #15:
score: 0
Accepted
time: 43ms
memory: 48436kb
input:
4 5 5 6 5 -5 6 -6 6
output:
0
result:
ok single line: '0'
Test #16:
score: 0
Accepted
time: 57ms
memory: 51040kb
input:
4 100 -99 -99 -98 -100 -98 99 -99
output:
0
result:
ok single line: '0'
Test #17:
score: 0
Accepted
time: 55ms
memory: 53400kb
input:
4 0 1 -1 0 0 -1 1 0
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #18:
score: 0
Accepted
time: 44ms
memory: 48996kb
input:
4 -1000 0 0 -1000 1000 0 0 1000
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #19:
score: 0
Accepted
time: 51ms
memory: 49044kb
input:
4 -1000 1000 -1000 -1000 1000 -1000 1000 1000
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #20:
score: 0
Accepted
time: 51ms
memory: 48236kb
input:
4 0 0 0 2 -1 2 -1 1
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #21:
score: 0
Accepted
time: 58ms
memory: 48700kb
input:
4 -3 -2 -4 -2 -4 -3 -3 -4
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #22:
score: 0
Accepted
time: 53ms
memory: 52804kb
input:
4 6 -2 5 -2 4 -3 6 -3
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #23:
score: 0
Accepted
time: 44ms
memory: 48520kb
input:
48 4 -60 12 -59 19 -57 26 -54 33 -50 39 -45 45 -39 50 -33 54 -26 57 -19 59 -12 60 -4 60 4 59 12 57 19 54 26 50 33 45 39 39 45 33 50 26 54 19 57 12 59 4 60 -4 60 -12 59 -19 57 -26 54 -33 50 -39 45 -45 39 -50 33 -54 26 -57 19 -59 12 -60 4 -60 -4 -59 -12 -57 -19 -54 -26 -50 -33 -45 -39 -39 -45 -33 -50 ...
output:
40
result:
ok single line: '40'
Test #24:
score: 0
Accepted
time: 32ms
memory: 53080kb
input:
4 7 3 7 4 5 4 6 3
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #25:
score: 0
Accepted
time: 51ms
memory: 49844kb
input:
4 -1000 0 -999 -1000 -998 0 -999 1000
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #26:
score: 0
Accepted
time: 57ms
memory: 52536kb
input:
4 0 -1000 1000 -999 0 -998 -1000 -999
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #27:
score: 0
Accepted
time: 42ms
memory: 48572kb
input:
3 999 -1000 1000 -1000 1000 -999
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #28:
score: 0
Accepted
time: 44ms
memory: 48560kb
input:
3 -2 -1 -2 -2 -1 -2
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #29:
score: 0
Accepted
time: 48ms
memory: 49504kb
input:
3 -1 0 0 1 -1 1
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #30:
score: 0
Accepted
time: 62ms
memory: 50020kb
input:
3 5 0 5 1 4 1
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #31:
score: 0
Accepted
time: 36ms
memory: 53152kb
input:
3 -777 -777 777 776 0 0
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #32:
score: 0
Accepted
time: 46ms
memory: 51616kb
input:
3 42 -13 42 -14 44 -13
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #33:
score: 0
Accepted
time: 52ms
memory: 53208kb
input:
3 -123 55 -122 57 -123 57
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #34:
score: 0
Accepted
time: 53ms
memory: 51020kb
input:
48 7 -99 19 -98 32 -94 44 -89 55 -83 66 -75 75 -66 83 -55 89 -44 94 -32 98 -19 99 -7 99 7 98 19 94 32 89 44 83 55 75 66 66 75 55 83 44 89 32 94 19 98 7 99 -7 99 -19 98 -32 94 -44 89 -55 83 -66 75 -75 66 -83 55 -89 44 -94 32 -98 19 -99 7 -99 -7 -98 -19 -94 -32 -89 -44 -83 -55 -75 -66 -66 -75 -55 -83 ...
output:
156
result:
ok single line: '156'
Test #35:
score: 0
Accepted
time: 127ms
memory: 49628kb
input:
5 0 -1000 1000 -999 999 1000 -1000 1000 -1000 -999
output:
7986005002
result:
ok single line: '7986005002'
Test #36:
score: 0
Accepted
time: 134ms
memory: 49112kb
input:
6 -999 1000 -1000 0 -999 -1000 999 -1000 999 -1 998 999
output:
2992004004
result:
ok single line: '2992004004'
Test #37:
score: 0
Accepted
time: 50ms
memory: 48728kb
input:
12 -923 -771 -612 -869 778 -976 933 -289 930 553 907 731 845 822 324 920 -913 699 -957 596 -967 269 -946 -455
output:
609372
result:
ok single line: '609372'
Test #38:
score: 0
Accepted
time: 53ms
memory: 52788kb
input:
9 -497 -908 741 -696 978 -393 892 690 863 986 -510 934 -672 659 -972 60 -963 -762
output:
1867855
result:
ok single line: '1867855'
Test #39:
score: 0
Accepted
time: 58ms
memory: 48724kb
input:
21 -804 -988 -393 -993 806 -997 893 -982 986 -870 996 -744 1000 -268 1000 194 999 638 997 666 971 928 957 943 828 989 778 992 501 997 -692 1000 -964 991 -990 936 -993 521 -995 -929 -965 -956
output:
34183
result:
ok single line: '34183'
Test #40:
score: 0
Accepted
time: 61ms
memory: 49116kb
input:
15 -947 -801 -516 -997 427 -998 541 -998 566 -997 927 -966 990 -932 998 471 991 896 817 964 536 997 -715 998 -868 922 -993 664 -958 -492
output:
170756
result:
ok single line: '170756'
Test #41:
score: 0
Accepted
time: 242ms
memory: 55080kb
input:
5 1000 998 -999 1000 -1000 999 -998 -999 999 -1000
output:
5326010345
result:
ok single line: '5326010345'
Test #42:
score: 0
Accepted
time: 45ms
memory: 49104kb
input:
8 0 2 0 1 1 0 2 0 3 1 3 2 2 3 1 3
output:
0
result:
ok single line: '0'
Test #43:
score: 0
Accepted
time: 152ms
memory: 51128kb
input:
5 1000 0 999 1000 -1000 999 -1000 -1000 999 -1000
output:
7986005002
result:
ok single line: '7986005002'
Test #44:
score: 0
Accepted
time: 136ms
memory: 50064kb
input:
5 0 1000 -1000 999 -999 -1000 1000 -1000 1000 999
output:
7986005002
result:
ok single line: '7986005002'
Test #45:
score: 0
Accepted
time: 55ms
memory: 53260kb
input:
4 1000 1000 999 1000 999 999 1000 999
output:
0
result:
ok single line: '0'
Test #46:
score: 0
Accepted
time: 165ms
memory: 50500kb
input:
5 -1000 0 -999 -1000 1000 -999 1000 1000 -999 1000
output:
7986005002
result:
ok single line: '7986005002'
Test #47:
score: 0
Accepted
time: 60ms
memory: 50584kb
input:
50 883 0 876 110 855 219 820 325 773 425 714 519 643 604 562 680 473 745 375 798 272 839 165 867 55 881 -55 881 -165 867 -272 839 -375 798 -473 745 -562 680 -643 604 -714 519 -773 425 -820 325 -855 219 -876 110 -883 0 -876 -110 -855 -219 -820 -325 -773 -425 -714 -519 -643 -604 -562 -680 -473 -745 -3...
output:
19136
result:
ok single line: '19136'
Test #48:
score: 0
Accepted
time: 56ms
memory: 51292kb
input:
49 750 0 743 95 725 190 695 281 653 368 601 448 538 521 467 586 388 641 303 685 213 719 119 740 24 749 -72 746 -166 731 -259 703 -346 664 -429 615 -504 555 -571 486 -628 409 -675 325 -711 236 -736 143 -748 48 -748 -48 -736 -143 -711 -236 -675 -325 -628 -409 -571 -486 -504 -555 -429 -615 -346 -664 -2...
output:
14376
result:
ok single line: '14376'
Test #49:
score: 0
Accepted
time: 54ms
memory: 50948kb
input:
42 1000 0 988 149 955 294 900 433 826 563 733 680 623 781 500 866 365 930 222 974 74 997 -74 997 -222 974 -365 930 -499 866 -623 781 -733 680 -826 563 -900 433 -955 294 -988 149 -1000 0 -988 -149 -955 -294 -900 -433 -826 -563 -733 -680 -623 -781 -500 -866 -365 -930 -222 -974 -74 -997 74 -997 222 -97...
output:
34900
result:
ok single line: '34900'
Test #50:
score: 0
Accepted
time: 47ms
memory: 51060kb
input:
33 100 0 98 18 92 37 84 54 72 69 58 81 41 90 23 97 4 99 -14 98 -32 94 -49 86 -65 75 -78 61 -88 45 -95 28 -99 9 -99 -9 -95 -28 -88 -45 -78 -61 -65 -75 -50 -86 -32 -94 -14 -98 4 -99 23 -97 41 -90 58 -81 72 -69 84 -54 92 -37 98 -18
output:
515
result:
ok single line: '515'
Test #51:
score: 0
Accepted
time: 51ms
memory: 49232kb
input:
25 500 0 484 124 438 240 364 342 267 422 154 475 31 499 -93 491 -212 452 -318 385 -404 293 -464 184 -496 62 -496 -62 -464 -184 -404 -293 -318 -385 -212 -452 -93 -491 31 -499 154 -475 267 -422 364 -342 438 -240 484 -124
output:
24994
result:
ok single line: '24994'
Test #52:
score: 0
Accepted
time: 57ms
memory: 48460kb
input:
19 900 0 851 292 710 552 492 753 220 872 -74 896 -361 824 -609 662 -791 428 -887 148 -887 -148 -791 -428 -609 -662 -361 -824 -74 -896 220 -872 492 -753 710 -552 851 -292
output:
142538
result:
ok single line: '142538'
Test #53:
score: 0
Accepted
time: 63ms
memory: 54928kb
input:
7 800 0 498 625 -178 779 -720 347 -720 -347 -178 -779 498 -625
output:
1054757
result:
ok single line: '1054757'
Test #54:
score: 0
Accepted
time: 57ms
memory: 50032kb
input:
6 999 0 499 865 -499 865 -999 0 -499 -865 499 -865
output:
2588522
result:
ok single line: '2588522'
Test #55:
score: 0
Accepted
time: 48ms
memory: 52692kb
input:
5 1000 0 309 951 -809 587 -809 -587 309 -951
output:
5311708
result:
ok single line: '5311708'
Test #56:
score: 0
Accepted
time: 46ms
memory: 48776kb
input:
4 999 -999 999 -1000 1000 -1000 1000 -999
output:
0
result:
ok single line: '0'
Test #57:
score: 0
Accepted
time: 57ms
memory: 49364kb
input:
5 6 4 5 10 4 13 5 7 6 3
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #58:
score: 0
Accepted
time: 47ms
memory: 48828kb
input:
5 -4 6 -10 5 -13 4 -7 5 -3 6
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #59:
score: 0
Accepted
time: 49ms
memory: 51152kb
input:
5 -6 -4 -5 -10 -4 -13 -5 -7 -6 -3
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #60:
score: 0
Accepted
time: 49ms
memory: 52624kb
input:
5 4 -6 10 -5 13 -4 7 -5 3 -6
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #61:
score: 0
Accepted
time: 64ms
memory: 50516kb
input:
5 -6 4 -6 3 -5 7 -4 13 -5 10
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #62:
score: 0
Accepted
time: 59ms
memory: 49068kb
input:
5 -4 -6 -3 -6 -7 -5 -13 -4 -10 -5
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #63:
score: 0
Accepted
time: 48ms
memory: 52924kb
input:
5 6 -4 6 -3 5 -7 4 -13 5 -10
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #64:
score: 0
Accepted
time: 55ms
memory: 48244kb
input:
5 4 6 3 6 7 5 13 4 10 5
output:
infinitely many
result:
ok single line: 'infinitely many'
Test #65:
score: 0
Accepted
time: 46ms
memory: 52364kb
input:
4 -800 -100 -799 -103 -798 -105 -799 -102
output:
0
result:
ok single line: '0'
Test #66:
score: 0
Accepted
time: 48ms
memory: 48500kb
input:
4 602 -59 600 -60 603 -59 605 -58
output:
0
result:
ok single line: '0'
Test #67:
score: 0
Accepted
time: 49ms
memory: 48516kb
input:
4 -999 -999 -1000 -999 -1000 -1000 -999 -1000
output:
0
result:
ok single line: '0'
Test #68:
score: 0
Accepted
time: 46ms
memory: 52688kb
input:
4 -50 50 -52 51 -51 50 -49 49
output:
0
result:
ok single line: '0'
Test #69:
score: 0
Accepted
time: 54ms
memory: 48812kb
input:
4 5 0 6 0 7 1 6 1
output:
0
result:
ok single line: '0'
Test #70:
score: 0
Accepted
time: 47ms
memory: 48248kb
input:
4 3 -3 4 -4 5 -4 4 -3
output:
0
result:
ok single line: '0'