This page is still under construction.

Parts of this page are still being built. What you see may change.

Simple Polygon

Time limit10sMemory limit128 MB

Summary
Given up to 40,000 points defining a closed polygon, decide whether its edges only meet at shared endpoints (simple) or intersect anywhere (NO).
Level

Hard8 of 10

Topics
Geometry, Sorting, Divide and conquer, Brute force
Solved
No attempts yet

Problem

A polygon PP defined by points p1,p2,…,pnp_1, p_2, \dots, p_n in the plane is the closed chain of line segments (called edges) p1p2,p2p3,…,pnp1p_1p_2, p_2p_3, \dots, p_np_1. The polygon PP is simple if no two edges share any point, with the single exception that two consecutive edges may share their one common point (called a vertex). However, if a vertex also lies on any other (third) edge, the polygon is no longer simple.

A polygon that is not simple is called self-intersecting.

Your task is to determine whether a given polygon is simple or self-intersecting.

Input

The input contains several test cases; each test case describes one polygon. The first line of a test case contains NN, the number of points (1≤N≤40 0001 \le N \le 40\,000). Each of the next NN lines contains the coordinates of a point PiP_i, namely XiX_i and YiY_i separated by a space (1≤Xi,Yi≤30 0001 \le X_i, Y_i \le 30\,000). The points are given in the order they appear along the polygon.

The last test case is followed by a line containing a single 00, which marks the end of the input.

Output

For each test case, print YES on its own line if the polygon is simple, or NO if it is self-intersecting.

Examples3

  1. Example 1

    Input
    5
    1 6
    5 7
    9 4
    2 3
    6 1
    7
    1 6
    5 7
    9 4
    4 3
    7 4
    4 6
    3 1
    7
    1 1
    1 4
    1 3
    2 2
    3 1
    3 3
    2 2
    0
    
    Expected output
    NO
    YES
    NO
    
  2. Example 2

    Input
    4
    1 1
    1 5
    5 5
    5 1
    0
    
    Expected output
    YES
    
  3. Example 3

    Input
    4
    1 1
    5 5
    1 5
    5 1
    0
    
    Expected output
    NO