<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

124. Broken line

time limit per test: 0.25 sec.
memory limit per test: 4096 KB


There is a closed broken line on a plane with sides parallel to coordinate axes, without self-crossings and self-contacts. The broken line consists of K segments. You have to determine, whether a given point with coordinates (X0,Y0) is inside this closed broken line, outside or belongs to the broken line.


Input

The first line contains integer K (4 Ј K Ј 10000) - the number of broken line segments. Each of the following N lines contains coordinates of the beginning and end points of the segments (4 integer xi1,yi1,xi2,yi2; all numbers in a range from -10000 up to 10000 inclusive). Number separate by a space. The segments are given in random order. Last line contains 2 integers X0 and Y0 - the coordinates of the given point delimited by a space. (Numbers X0, Y0 in a range from -10000 up to 10000 inclusive).


Output

The first line should contain:

INSIDE - if the point is inside closed broken line,

OUTSIDE - if the point is outside,

BORDER - if the point belongs to broken line.



Sample Input

4
0 0 0 3
3 3 3 0
0 3 3 3
3 0 0 0
2 2
Sample Output

INSIDE
Author	: Alex Y. Suslov, Sergey V. Mironov
Resource	: 5th Southern Subregional Contest. Saratov 2002
Date	: 2002-10-10

<|response|>
1. Abridged Problem Statement  
Given K axis-aligned segments forming a simple closed polygon (no self-intersections or contacts) in the plane, and a query point (X₀, Y₀), determine whether the point is strictly inside the polygon, strictly outside it, or exactly on one of its segments (border).

2. Key Observations  
- All edges are either horizontal or vertical.  
- Checking if the point lies on a segment (border case) is just range checking.  
- A classic point-in-polygon test (“ray casting”) can be specialized: cast a vertical ray upward from the point and count how many times it crosses the polygon boundary.  
- Only horizontal edges can be crossed by an upward vertical ray.  
- To avoid double-counting intersections at segment endpoints, adopt the rule: for a horizontal segment with x-span [x₁, x₂], count a crossing if its y > Y₀ and min(x₁,x₂) < X₀ ≤ max(x₁,x₂).  
- If the crossing count is odd → INSIDE; if even → OUTSIDE.

3. Full Solution Approach  
Step 1. Read input: integer K, then K lines of segments (x₁,y₁,x₂,y₂), then the query point (X₀,Y₀).  
Step 2. Border check:  
  - For each **vertical** segment (x₁ == x₂): if X₀ == x₁ and Y₀ lies between y₁ and y₂ (inclusive), output “BORDER” and exit.  
  - For each **horizontal** segment (y₁ == y₂): if Y₀ == y₁ and X₀ lies between x₁ and x₂ (inclusive), output “BORDER” and exit.  
Step 3. Crossing count with an upward vertical ray:  
  - Initialize crossings = 0.  
  - For each **horizontal** segment at y = Yₛ:  
      * If Yₛ > Y₀ (segment strictly above the point), let xl = min(x₁,x₂), xr = max(x₁,x₂).  
      * If xl < X₀ ≤ xr, increment crossings by 1.  
Step 4. If crossings is odd, output “INSIDE”; otherwise, output “OUTSIDE”.

Time Complexity: O(K).  
Memory: O(K) to store segments.

4. C++ Implementation with Detailed Comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int K;
    cin >> K;
    // Read all segments
    vector<array<int,4>> segs(K);
    for(int i = 0; i < K; i++) {
        cin >> segs[i][0]  // x1
            >> segs[i][1]  // y1
            >> segs[i][2]  // x2
            >> segs[i][3]; // y2
    }
    // Read query point
    int X0, Y0;
    cin >> X0 >> Y0;

    // 1) Border check: if the point lies exactly on any segment
    for (auto &s : segs) {
        int x1 = s[0], y1 = s[1], x2 = s[2], y2 = s[3];
        if (x1 == x2) {
            // Vertical segment at x = x1
            if (X0 == x1 &&
                min(y1, y2) <= Y0 && Y0 <= max(y1, y2)) {
                cout << "BORDER\n";
                return 0;
            }
        } else {
            // Horizontal segment at y = y1 (== y2)
            if (Y0 == y1 &&
                min(x1, x2) <= X0 && X0 <= max(x1, x2)) {
                cout << "BORDER\n";
                return 0;
            }
        }
    }

    // 2) Cast a vertical ray upward and count crossings with horizontal segments
    int crossings = 0;
    for (auto &s : segs) {
        int x1 = s[0], y1 = s[1], x2 = s[2], y2 = s[3];
        // Only horizontal segments can be intersected
        if (y1 == y2) {
            int y = y1;
            // Consider only segments strictly above the point
            if (y > Y0) {
                int xl = min(x1, x2);
                int xr = max(x1, x2);
                // Use (xl, xr] so that shared endpoints are counted once
                if (xl < X0 && X0 <= xr) {
                    crossings++;
                }
            }
        }
    }

    // 3) Determine result by parity of crossings
    if (crossings % 2 == 1) {
        cout << "INSIDE\n";
    } else {
        cout << "OUTSIDE\n";
    }
    return 0;
}
```

5. Python Implementation with Detailed Comments  
```python
import sys

def main():
    data = sys.stdin.read().split()
    it = iter(data)
    K = int(next(it))
    segments = []
    for _ in range(K):
        x1 = int(next(it)); y1 = int(next(it))
        x2 = int(next(it)); y2 = int(next(it))
        segments.append((x1, y1, x2, y2))

    X0 = int(next(it)); Y0 = int(next(it))

    # 1) Border check
    for x1, y1, x2, y2 in segments:
        if x1 == x2:
            # vertical segment
            if X0 == x1 and min(y1, y2) <= Y0 <= max(y1, y2):
                print("BORDER")
                return
        else:
            # horizontal segment
            if Y0 == y1 and min(x1, x2) <= X0 <= max(x1, x2):
                print("BORDER")
                return

    # 2) Cast upward ray, count intersections with horizontal segments
    crossings = 0
    for x1, y1, x2, y2 in segments:
        if y1 == y2:
            y = y1
            if y > Y0:
                xl, xr = sorted((x1, x2))
                # Count if X0 is in (xl, xr]
                if xl < X0 <= xr:
                    crossings += 1

    # 3) Parity determines inside/outside
    if crossings % 2 == 1:
        print("INSIDE")
    else:
        print("OUTSIDE")

if __name__ == "__main__":
    main()
```