We need to parse problem: Given three points p0, p1, p2. We need to output one of five strings based on relative orientation and position of p2 relative to directed line p0->p1.
Definition: For points p0, p1, p2. Let cross product = (p1 - p0) × (p2 - p0). Actually standard CCW test: (x1-x0)(y2-y0) - (x2-x0)(y1-y0). If cross > 0 -> counterclockwise (CCW). cross < 0 -> clockwise. cross == 0 -> collinear. Then for collinear case, need to determine position:
- If (p2 - p0) dot (p1 - p0) < 0 => p2 is behind p0 (i.e., p0 between p2 and p1? Actually description: "if p2 is on a line p2, p0, p1 in this order" meaning order along line: p2, then p0, then p1? That's confusing. Let's interpret typical definitions from ICPC or geometry problems (e.g., CGL_1_C from AtCoder or AOJ). I recall a problem: "CGL_1_C : Counter-Clockwise" from AOJ. In that, given points p0, p1, p2, output:
- COUNTER_CLOCKWISE if cross > 0 (1)
- CLOCKWISE if cross < 0 (2)
- ONLINE_BACK if cross = 0 and (p2 - p0) dot (p1 - p0) < 0 (3)
- ONLINE_FRONT if cross = 0 and (p2 - p0) dot (p1 - p0) > |p1-p0|^2? Actually they define:
if p2 is on the line p1-p0 but not between p0 and p1, and is on the extension beyond p1 (i.e., p0->p1->p2 direction) then ONLINE_FRONT.
If p2 is on the segment p0p1 then ON_SEGMENT.
If p2 is on the line but behind p0 (i.e., p2->p0->p1) then ONLINE_BACK.
Thus typical conditions:
Let dot = (p1 - p0) · (p2 - p0). Let norm = (p1 - p0) · (p1 - p0).
If cross == 0:
- if dot < 0 -> ONLINE_BACK.
- else if dot > norm -> ONLINE_FRONT (since beyond p1)
- else (0 <= dot <= norm) -> ON_SEGMENT.
But example given:
Input1:
0 0 2 0 (p0=(0,0), p1=(2,0))
q=2
(-1,1) -> cross = (2-0)(1-0) - ((-1)-0)(0-0) = 2*1 - (-1)0 = 2 >0 => CCW -> output "COUNTER_CLOCKWISE"
(-1,-1) -> cross = (2)( -1) - (-1)*0 = -2 <0 => CLOCKWISE. Good.
Input2:
0 0 2 0
3
-1 0 -> collinear (0? cross= (2)(0) - (-1)0 =0) -> dot = (2)( -1) + 00 = -2 <0 -> ONLINE_BACK => output "ONLINE_BACK" matches.
0 0 -> collinear: p2=p0. dot = (2)(0) + 00 = 0, norm=4. 0 <= 0 <= 4 -> ON_SEGMENT => output "ON_SEGMENT".
3 0 -> dot = (2)(3) + 00 = 6 > norm=4 -> ONLINE_FRONT => output "ONLINE_FRONT".
So indeed matches typical.
Thus algorithm: For each query point p2, compute cross = (x1-x0)(y2-y0) - (x2-x0)(y1-y0). Then if cross > 0: print "COUNTER_CLOCKWISE"; elif cross < 0: print "CLOCKWISE"; else (cross == 0):
compute dot = (x1-x0)(x2-x0) + (y1-y0)(y2-y0)
compute norm = (x1-x0)^2 + (y1-y0)^2
if dot < 0: print "ONLINE_BACK"
elif dot > norm: print "ONLINE_FRONT"
else: print "ON_SEGMENT"
But careful: dot can be negative also if p2 is exactly p0? dot=0, that is fine. And if p2 exactly p1? dot = norm, so it's not > norm, so it's <= norm and >=0? dot = norm, thus falls into else? But p2 = p1 is on the segment? Usually endpoint p1 is considered on the segment? Yes, ON_SEGMENT typically includes endpoints. In example, p2 = (2,0) would be ON_SEGMENT? I think they count it as ON_SEGMENT? Not in example but likely yes. We'll follow typical. For p2 = p1: dot = norm, so condition dot > norm is false, dot < 0 false, so ON_SEGMENT. That's standard. For p2 = p0: dot=0, also ON_SEGMENT.
Now the input format: first line: xp0 yp0 xp1 yp1. Then integer q. Then q lines each with xp2 yp2. So we need to read q and then q points.
Constraints: q up to 1000, coordinates up to 10000, fits in int. cross product may be up to (2000020000) approx 4e8, still fits in 32-bit int? Actually coordinates up to 20000 difference, product up to 400 million, still fits 32-bit signed int (max ~2.1e9). But careful if differences up to 20000? Actually coordinate range -10000 to 10000, max difference 20000, so product up to 2000020000 = 400 million. For two such products (cross is difference) up to 800 million, still safe for int. However we may need to handle long just in case but int is fine in C++ etc. In Python, no problem.
Now produce solution. We'll parse input. Compute differences once: dx = x1-x0, dy = y1-y0. Then for each query compute cross = dx*(y2-y0) - dy*(x2-x0). Use Python's int.
Now output each result on separate line.
Edge cases: p0 and p1 are not identical per constraints.
Implementation: read from sys.stdin. Possibly multiple test lines? Actually one dataset.
Examples output: capital strings exactly as given.
Thus code.
Now need to produce final answer: Explanation + code? The user wants solution: maybe just the code? Usually they want code. But the instruction: "We need to parse problem: ..." might be internal. The final response should be the answer, which is the solution code? The user said: " For given three points..." and then the description. Likely they want the solution code in Python? Possibly they want algorithm explanation and code. The final output of this chat should be the answer to the problem: Provide a program that solves it. We'll output Python code.
Let's produce Python solution:
import sys
def main():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
x0 = int(next(it)); y0 = int(next(it))
x1 = int(next(it)); y1 = int(next(it))
dx = x1 - x0
dy = y1 - y0
norm = dx*dx + dy*dy
q = int(next(it))
out = []
for _ in range(q):
x2 = int(next(it)); y2 = int(next(it))
cross = dx*(y2 - y0) - dy*(x2 - x0)
if cross > 0:
out.append("COUNTER_CLOCKWISE")
elif cross < 0:
out.append("CLOCKWISE")
else:
dot = dx*(x2 - x0) + dy*(y2 - y0)
if dot < 0:
out.append("ONLINE_BACK")
elif dot > norm:
out.append("ONLINE_FRONT")
else:
out.append("ON_SEGMENT")
sys.stdout.write("\n".join(out))
if __name__ == "__main__":
main()
Test with given examples.
Example 1:
Input: "0 0 2 0\n2\n-1 1\n-1 -1\n"
x0=0,y0=0,x1=2,y1=0. dx=2,dy=0,norm=4.
First: cross=2*(1)-0*(-1)=2>0 -> COUNTER... correct.
Second: cross=2*(-1)-0*(-1) = -2 <0 -> CLOCK... correct.
Example2:
Input: "0 0 2 0\n3\n-1 0\n0 0\n3 0\n"
cross all zero.
dot1: 2*(-1)+00 = -2<0 -> BACK.
dot2: 20+00 = 0 -> ON_SEGMENT.
dot3: 23+0*0=6>4 -> FRONT. Correct.
So final answer.
But if multiple spaces or lines, reading all tokens works.
Make sure to handle possible extra newline.
Thus produce answer.
Now as ChatGPT final: Provide solution in Python with explanation? I'll include explanation and code in final answer, but as per typical problem-solving tasks, they often expect just the code. However, the user asked "print ..." and gave problem description; likely they want solution. So we'll give code.
I'll write a complete solution.
Potential pitfalls: Norm might be zero if p0 and p1 same but constraints say not identical, so fine.
Thus final. ```python
import sys
def main() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
x0 = int(next(it)); y0 = int(next(it))
x1 = int(next(it)); y1 = int(next(it))
dx = x1 - x0
dy = y1 - y0
norm = dx * dx + dy * dy # squared length of p0p1
q = int(next(it))
out_lines = []
for _ in range(q):
x2 = int(next(it)); y2 = int(next(it))
cross = dx * (y2 - y0) - dy * (x2 - x0)
if cross > 0:
out_lines.append("COUNTER_CLOCKWISE")
elif cross < 0:
out_lines.append("CLOCKWISE")
else: # collinear
dot = dx * (x2 - x0) + dy * (y2 - y0)
if dot < 0:
out_lines.append("ONLINE_BACK")
elif dot > norm:
out_lines.append("ONLINE_FRONT")
else:
out_lines.append("ON_SEGMENT")
sys.stdout.write("\n".join(out_lines))
if name == "main":
main()