<|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 downward from the point and count how many times it crosses the polygon boundary.
- Only horizontal edges can be crossed by a 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 segments (x₁,y₁,x₂,y₂), then the query point (X₀,Y₀).
Step 2. In a single pass over the segments, do the border check and the crossing count:
  - 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.
      * Otherwise, if y₁ < Y₀ (segment strictly below the point) and min(x₁,x₂) < X₀ ≤ max(x₁,x₂), increment crossings.
Step 3. If crossings is odd, output “INSIDE”; otherwise, output “OUTSIDE”.

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

4. C++ Implementation
```cpp
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n;
vector<pair<pair<int, int>, pair<int, int>>> segments;
pair<int, int> tgt;

void read() {
    cin >> n;
    segments.resize(n);
    cin >> segments;
    cin >> tgt;
}

void solve() {
    /*
     * Point-in-polygon test for an axis-parallel rectilinear closed loop via
     * a horizontal ray cast downward from the query point.
     *
     * For each segment: if the point lies on a vertical or horizontal segment
     * it is on the BORDER. Otherwise, count horizontal segments strictly below
     * the point whose x-span covers the point's x (half-open in x so shared
     * endpoints are counted once); odd count means INSIDE, even means OUTSIDE.
     */

    int cnt = 0;
    for(int i = 0; i < (int)segments.size(); i++) {
        auto [p1, p2] = segments[i];
        auto [x1, y1] = p1;
        auto [x2, y2] = p2;

        if(x1 == x2 && tgt.second >= min(y1, y2) && tgt.second <= max(y1, y2) &&
           tgt.first == x1) {
            cout << "BORDER" << endl;
            return;
        }

        if(y1 == y2 && tgt.second == y1 && min(x1, x2) <= tgt.first &&
           tgt.first <= max(x1, x2)) {
            cout << "BORDER" << endl;
            return;
        }

        if(y1 == y2 && tgt.second < y1 && min(x1, x2) < tgt.first &&
           tgt.first <= max(x1, x2)) {
            cnt++;
        }
    }

    if(cnt % 2 == 0) {
        cout << "OUTSIDE" << endl;
    } else {
        cout << "INSIDE" << endl;
    }
}

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

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    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))

    crossings = 0
    for x1, y1, x2, y2 in segments:
        if x1 == x2:
            # vertical segment border check
            if X0 == x1 and min(y1, y2) <= Y0 <= max(y1, y2):
                print("BORDER")
                return
        else:
            # horizontal segment border check
            if Y0 == y1 and min(x1, x2) <= X0 <= max(x1, x2):
                print("BORDER")
                return
            # downward ray: count horizontal segments strictly below the point
            if y1 < Y0:
                xl, xr = sorted((x1, x2))
                # Count if X0 is in (xl, xr]
                if xl < X0 <= xr:
                    crossings += 1

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

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