Problem:


Score : 700 points

Problem Statement

Snuke is playing a puzzle game.
In this game, you are given a rectangular board of dimensions R × C, filled with numbers. Each integer i from 1 through N is written twice, at the coordinates (x_{i,1},y_{i,1}) and (x_{i,2},y_{i,2}).

The objective is to draw a curve connecting the pair of points where the same integer is written, for every integer from 1 through N.
Here, the curves may not go outside the board or cross each other.

Determine whether this is possible.

Constraints

1 ≤ R,C ≤ 10^8

1 ≤ N ≤ 10^5

0 ≤ x_{i,1},x_{i,2} ≤ R(1 ≤ i ≤ N)

0 ≤ y_{i,1},y_{i,2} ≤ C(1 ≤ i ≤ N)

All given points are distinct.

All input values are integers.

Input

Input is given from Standard Input in the following format:

R C N
x_{1,1} y_{1,1} x_{1,2} y_{1,2}
:
x_{N,1} y_{N,1} x_{N,2} y_{N,2}

Output

Print YES if the objective is achievable; print NO otherwise.

Sample Input 1

4 2 3
0 1 3 1
1 1 4 1
2 0 2 2

Sample Output 1

YES

The above figure shows a possible solution.

Sample Input 2

2 2 4
0 0 2 2
2 0 0 1
0 2 1 2
1 1 2 1

Sample Output 2

NO

Sample Input 3

5 5 7
0 0 2 4
2 3 4 5
3 5 5 2
5 5 5 4
0 3 5 1
2 2 4 4
0 5 4 1

Sample Output 3

YES

Sample Input 4

1 1 2
0 0 1 1
1 0 0 1

Sample Output 4

NO



Sample input:
4 2 3
0 1 3 1
1 1 4 1
2 0 2 2


Sample output:
YES

Submission status: Wrong Answer

Solution code:
import sys

input=sys.stdin.readline

def check_intersection(ax, ay, bx, by, cx, cy, dx, dy):
    dax, day = ax - bx, ay - by
    dcx, dcy = cx - dx, cy - dy
    ta = dcx * (ay - cy) + dcy * (cx - ax)
    tb = dcx * (by - cy) + dcy * (cx - bx)
    tc = dax * (cy - ay) + day * (ax - cx)
    td = dax * (dy - ay) + day * (ax - dx)
    return tc * td < 0 and ta * tb < 0

R,C,N=map(int,input().split())

def is_on_edge(x,y):
    return x==0 or x==R or y==0 or y==C

line=[]
node=set([])
for i in range(0,N):
    x1,y1,x2,y2=map(int,input().split())
    if is_on_edge(x1,y1) and is_on_edge(x2,y2):
        line.append([x1,y1,x2,y2])
        node.add((x1,y1))
        node.add((x2,y2))

if len(node)!=len(line)*2:
    print("NO")
    exit()

def condition():
    global R,C,line
    n=len(line)
    def is_on_xaxis(x,y):
        return y==0 or y==C

    def is_on_yaxis(x,y):
        return x==0 or x==R

    xaxis=True
    for i in range(0,n):
        x1,y1,x2,y2=line[i]
        if (not is_on_xaxis(x1,y1) ) and (not is_on_xaxis(x2,y2)):
            xaxis=False
            break

    yaxis=True
    for i in range(0,n):
        x1,y1,x2,y2=line[i]
        if (not is_on_yaxis(x1,y1) ) and (not is_on_yaxis(x2,y2)):
            yaxis=False
            break

    if xaxis:
        X={0:[],C:[]}
        for i in range(0,n):
            x1,y1,x2,y2=line[i]
            if y1==0:
                if y2!=0:
                    X[0].append(line[i])
                else:
                    if x1>x2:
                        X[0].append([x2,y2,x1,y1])
                    else:
                        X[0].append(line[i])
            else:
                if y2!=C:
                    X[C].append(line[i])
                else:
                    if x1>x2:
                        X[C].append([x2,y2,x1,y1])
                    else:
                        X[C].append(line[i])

        m=len(X[0])
        k=len(X[C])
        X[0].sort()
        X[C].sort()
        for i in range(0,m-1):
            x1,y1,x2,y2=X[0][i]
            x3,y3,x4,y4=X[0][i+1]
            if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                print("NO")
                exit()
            else:
                if y1==y2:
                    if x2>=x3>=x1:
                        print("NO")
                        exit()

        for i in range(0,k-1):
            x1,y1,x2,y2=X[C][i]
            x3,y3,x4,y4=X[C][i+1]
            if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                print("NO")
                exit()
            else:
                if y1==y2:
                    if x2>=x3>=x1:
                        print("NO")
                        exit()
        if X[C]:
            for i in range(0,m):
                x1,y1,x2,y2=X[0][i]
                if y2==C:
                    start=0
                    end=k-1
                    while end-start>1:
                        test=(end+start)//2
                        if X[C][test][0]>=x2:
                            end=test
                        else:
                            start=test

                    if X[C][start][0]>=x2:
                        index=start
                    else:
                        if X[C][end][0]>=x2:
                            index=end
                        else:
                            index=k
                    #index-1,indexのやつと交差判定
                    if index!=0:
                        x3,y3,x4,y4=X[C][index-1]
                        if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                            print("NO")
                            exit()
                        else:
                            if y3==y4:
                                if x4>=x2>=x3:
                                    print("NO")
                                    exit()
                    if index!=k:
                        x3,y3,x4,y4=X[C][index]
                        if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                            print("NO")
                            exit()
                        else:
                            if y3==y4:
                                if x4>=x2>=x3:
                                    print("NO")
                                    exit()

        if X[0]:
            for i in range(0,k):
                x1,y1,x2,y2=X[C][i]
                if y2==0:
                    start=0
                    end=m-1
                    while end-start>1:
                        test=(end+start)//2
                        if X[0][test][0]>=x2:
                            end=test
                        else:
                            start=test

                    if X[0][start][0]>=x2:
                        index=start
                    else:
                        if X[0][end][0]>=x2:
                            index=end
                        else:
                            index=m
                    #index-1,indexのやつと交差判定
                    if index!=0:
                        x3,y3,x4,y4=X[0][index-1]
                        if y3==y4:
                            if x4>=x2>=x3:
                                print("NO")
                                exit()

                    if index!=m:
                        x3,y3,x4,y4=X[0][index]
                        if y3==y4:
                            if x4>=x2>=x3:
                                print("NO")
                                exit()

    elif yaxis:
        r=R
        c=C
        C=r
        R=c
        for i in range(0,n):
            x1,y1,x2,y2=line[i]
            line[i]=[y1,x1,y2,x2]
        condition()
    else:
        print("NO")
        exit()

condition()
print("YES")

Submission status: Wrong Answer

Solution code:
import sys

input=sys.stdin.readline

def check_intersection(ax, ay, bx, by, cx, cy, dx, dy):
    dax, day = ax - bx, ay - by
    dcx, dcy = cx - dx, cy - dy
    ta = dcx * (ay - cy) + dcy * (cx - ax)
    tb = dcx * (by - cy) + dcy * (cx - bx)
    tc = dax * (cy - ay) + day * (ax - cx)
    td = dax * (dy - ay) + day * (ax - dx)
    return tc * td < 0 and ta * tb < 0

R,C,N=map(int,input().split())

def is_on_edge(x,y):
    return x==0 or x==R or y==0 or y==C

line=[]
node=set([])
for i in range(0,N):
    x1,y1,x2,y2=map(int,input().split())
    if is_on_edge(x1,y1) and is_on_edge(x2,y2):
        line.append([x1,y1,x2,y2])
        node.add((x1,y1))
        node.add((x2,y2))
        
if len(node)!=len(line)*2:
    print("NO")
    exit()

def condition():
    global R,C,line
    n=len(line)
    def is_on_xaxis(x,y):
        return y==0 or y==C

    def is_on_yaxis(x,y):
        return x==0 or x==R

    xaxis=True
    for i in range(0,n):
        x1,y1,x2,y2=line[i]
        if (not is_on_xaxis(x1,y1) ) and (not is_on_xaxis(x2,y2)):
            xaxis=False
            break

    yaxis=True
    for i in range(0,n):
        x1,y1,x2,y2=line[i]
        if (not is_on_yaxis(x1,y1) ) and (not is_on_yaxis(x2,y2)):
            yaxis=False
            break

    if xaxis:
        X={0:[],C:[]}
        for i in range(0,n):
            x1,y1,x2,y2=line[i]
            if y1==0:
                if y2!=0:
                    X[0].append(line[i])
                else:
                    if x1>x2:
                        X[0].append([x2,y2,x1,y1])
                    else:
                        X[0].append(line[i])
            else:
                if y2!=C:
                    X[C].append(line[i])
                else:
                    if x1>x2:
                        X[C].append([x2,y2,x1,y1])
                    else:
                        X[C].append(line[i])

        m=len(X[0])
        k=len(X[C])
        X[0].sort()
        X[C].sort()
        for i in range(0,m-1):
            x1,y1,x2,y2=X[0][i]
            x3,y3,x4,y4=X[0][i+1]
            if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                print("NO")
                exit()
            else:
                if y1==y2:
                    if x2>=x3>=x1:
                        print("NO")
                        exit()

        for i in range(0,k-1):
            x1,y1,x2,y2=X[C][i]
            x3,y3,x4,y4=X[C][i+1]
            if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                print("NO")
                exit()
            else:
                if y1==y2:
                    if x2>=x3>=x1:
                        print("NO")
                        exit()
        if X[C]:
            for i in range(0,m):
                x1,y1,x2,y2=X[0][i]
                if y2==C:
                    start=0
                    end=k-1
                    while end-start>1:
                        test=(end+start)//2
                        if X[C][test][0]>=x2:
                            end=test
                        else:
                            start=test

                    if X[C][start][0]>=x2:
                        index=start
                    else:
                        if X[C][end][0]>=x2:
                            index=end
                        else:
                            index=k
                    #index-1,indexのやつと交差判定
                    if index!=0:
                        x3,y3,x4,y4=X[C][index-1]
                        if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                            print("NO")
                            exit()
                        else:
                            if y3==y4:
                                if x4>=x2>=x3:
                                    print("NO")
                                    exit()
                    if index!=k:
                        x3,y3,x4,y4=X[C][index]
                        if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                            print("NO")
                            exit()
                        else:
                            if y3==y4:
                                if x4>=x2>=x3:
                                    print("NO")
                                    exit()

        if X[0]:
            for i in range(0,k):
                x1,y1,x2,y2=X[C][i]
                if y2==0:
                    start=0
                    end=m-1
                    while end-start>1:
                        test=(end+start)//2
                        if X[0][test][0]>=x2:
                            end=test
                        else:
                            start=test

                    if X[0][start][0]>=x2:
                        index=start
                    else:
                        if X[0][end][0]>=x2:
                            index=end
                        else:
                            index=m
                    #index-1,indexのやつと交差判定
                    if index!=0:
                        x3,y3,x4,y4=X[0][index-1]
                        if y3==y4:
                            if x4>=x2>=x3:
                                print("NO")
                                exit()

                    if index!=m:
                        x3,y3,x4,y4=X[0][index]
                        if y3==y4:
                            if x4>=x2>=x3:
                                print("NO")
                                exit()
        print("YES")

    elif yaxis:
        r=R
        c=C
        C=r
        R=c
        for i in range(0,n):
            x1,y1,x2,y2=line[i]
            line[i]=[y1,x1,y2,x2]
        condition()
    else:
        print("NO")
        exit()

condition()

Submission status: Wrong Answer

Solution code:
import sys

input=sys.stdin.readline

def check_intersection(ax, ay, bx, by, cx, cy, dx, dy):
    dax, day = ax - bx, ay - by
    dcx, dcy = cx - dx, cy - dy
    ta = dcx * (ay - cy) + dcy * (cx - ax)
    tb = dcx * (by - cy) + dcy * (cx - bx)
    tc = dax * (cy - ay) + day * (ax - cx)
    td = dax * (dy - ay) + day * (ax - dx)
    return tc * td < 0 and ta * tb < 0

R,C,N=map(int,input().split())

def is_on_edge(x,y):
    return x==0 or x==R or y==0 or y==C

line=[]
node=set([])
for i in range(0,N):
    x1,y1,x2,y2=map(int,input().split())
    if is_on_edge(x1,y1) and is_on_edge(x2,y2):
        line.append([x1,y1,x2,y2])
        node.add((x1,y1))
        node.add((x2,y2))
if len(node)!=len(line)*2:
    print("NO")
    exit()
print(line)
def condition():
    global R,C,line
    n=len(line)
    def is_on_xaxis(x,y):
        return y==0 or y==C

    def is_on_yaxis(x,y):
        return x==0 or x==R

    xaxis=True
    for i in range(0,n):
        x1,y1,x2,y2=line[i]
        if (not is_on_xaxis(x1,y1) ) and (not is_on_xaxis(x2,y2)):
            xaxis=False
            break

    yaxis=True
    for i in range(0,n):
        x1,y1,x2,y2=line[i]
        if (not is_on_yaxis(x1,y1) ) and (not is_on_yaxis(x2,y2)):
            yaxis=False
            break

    if xaxis:
        X={0:[],C:[]}
        for i in range(0,n):
            x1,y1,x2,y2=line[i]
            if y1==0:
                if y2!=0:
                    X[0].append(line[i])
                else:
                    if x1>x2:
                        X[0].append([x2,y2,x1,y1])
                    else:
                        X[0].append(line[i])
            else:
                if y2!=C:
                    X[C].append(line[i])
                else:
                    if x1>x2:
                        X[C].append([x2,y2,x1,y1])
                    else:
                        X[C].append(line[i])

        m=len(X[0])
        k=len(X[C])
        X[0].sort()
        X[C].sort()
        for i in range(0,m-1):
            x1,y1,x2,y2=X[0][i]
            x3,y3,x4,y4=X[0][i+1]
            if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                print("NO")
                exit()
            else:
                if y1==y2:
                    if x2>=x3>=x1:
                        print("NO")
                        exit()

        for i in range(0,k-1):
            x1,y1,x2,y2=X[C][i]
            x3,y3,x4,y4=X[C][i+1]
            if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                print("NO")
                exit()
            else:
                if y1==y2:
                    if x2>=x3>=x1:
                        print("NO")
                        exit()
        if X[C]:
            for i in range(0,m):
                x1,y1,x2,y2=X[0][i]
                if y2==C:
                    start=0
                    end=k-1
                    while end-start>1:
                        test=(end+start)//2
                        if X[C][test][0]>=x2:
                            end=test
                        else:
                            start=test

                    if X[C][start][0]>=x2:
                        index=start
                    else:
                        if X[C][end][0]>=x2:
                            index=end
                        else:
                            index=k
                    #index-1,indexのやつと交差判定
                    if index!=0:
                        x3,y3,x4,y4=X[C][index-1]
                        if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                            print("NO")
                            exit()
                        else:
                            if y3==y4:
                                if x4>=x2>=x3:
                                    print("NO")
                                    exit()
                    if index!=k:
                        x3,y3,x4,y4=X[C][index]
                        if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                            print("NO")
                            exit()
                        else:
                            if y3==y4:
                                if x4>=x2>=x3:
                                    print("NO")
                                    exit()

        if X[0]:
            for i in range(0,k):
                x1,y1,x2,y2=X[C][i]
                if y2==0:
                    start=0
                    end=m-1
                    while end-start>1:
                        test=(end+start)//2
                        if X[0][test][0]>=x2:
                            end=test
                        else:
                            start=test

                    if X[0][start][0]>=x2:
                        index=start
                    else:
                        if X[0][end][0]>=x2:
                            index=end
                        else:
                            index=m
                    #index-1,indexのやつと交差判定
                    if index!=0:
                        x3,y3,x4,y4=X[0][index-1]
                        if y3==y4:
                            if x4>=x2>=x3:
                                print("NO")
                                exit()

                    if index!=m:
                        x3,y3,x4,y4=X[0][index]
                        if y3==y4:
                            if x4>=x2>=x3:
                                print("NO")
                                exit()
        print("YES")

    elif yaxis:
        r=R
        c=C
        C=r
        R=c
        for i in range(0,n):
            x1,y1,x2,y2=line[i]
            line[i]=[y1,x1,y2,x2]
        condition()
    else:
        print("NO")
        exit()

condition()

Submission status: Wrong Answer

Solution code:
import sys

input=sys.stdin.readline

def check_intersection(ax, ay, bx, by, cx, cy, dx, dy):
    dax, day = ax - bx, ay - by
    dcx, dcy = cx - dx, cy - dy
    ta = dcx * (ay - cy) + dcy * (cx - ax)
    tb = dcx * (by - cy) + dcy * (cx - bx)
    tc = dax * (cy - ay) + day * (ax - cx)
    td = dax * (dy - ay) + day * (ax - dx)
    return tc * td < 0 and ta * tb < 0

R,C,N=map(int,input().split())

def is_on_edge(x,y):
    return x==0 or x==R or y==0 or y==C

line=[]
for i in range(0,N):
    x1,y1,x2,y2=map(int,input().split())
    if is_on_edge(x1,y1) and is_on_edge(x2,y2):
        line.append([x1,y1,x2,y2])

def condition():
    global R,C,line
    n=len(line)
    def is_on_xaxis(x,y):
        return y==0 or y==C

    def is_on_yaxis(x,y):
        return x==0 or x==R

    xaxis=True
    for i in range(0,n):
        x1,y1,x2,y2=line[i]
        if (not is_on_xaxis(x1,y1) ) and (not is_on_xaxis(x2,y2)):
            xaxis=False
            break

    yaxis=True
    for i in range(0,n):
        x1,y1,x2,y2=line[i]
        if (not is_on_yaxis(x1,y1) ) and (not is_on_yaxis(x2,y2)):
            yaxis=False
            break

    if xaxis:
        X={0:[],C:[]}
        for i in range(0,n):
            x1,y1,x2,y2=line[i]
            if y1==0:
                if y2!=0:
                    X[0].append(line[i])
                else:
                    if x1>x2:
                        X[0].append([x2,y2,x1,y1])
            else:
                if y2!=C:
                    X[C].append(line[i])
                else:
                    if x1>x2:
                        X[C].append([x2,y2,x1,y1])

        m=len(X[0])
        k=len(X[C])
        X[0].sort()
        X[C].sort()
        for i in range(0,m-1):
            x1,y1,x2,y2=X[0][i]
            x3,y3,x4,y4=X[0][i+1]
            if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                print("NO")
                exit()
            else:
                if y1==y2:
                    if x2>=x3>=x1:
                        print("NO")
                        exit()

        for i in range(0,k-1):
            x1,y1,x2,y2=X[C][i]
            x3,y3,x4,y4=X[C][i+1]
            if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                print("NO")
                exit()
            else:
                if y1==y2:
                    if x2>=x3>=x1:
                        print("NO")
                        exit()
        if X[C]:
            for i in range(0,m):
                x1,y1,x2,y2=X[0][i]
                if y2==C:
                    start=0
                    end=k-1
                    while end-start>1:
                        test=(end+start)//2
                        if X[C][test][0]>=x2:
                            end=test
                        else:
                            start=test

                    if X[C][start][0]>=x2:
                        index=start
                    else:
                        if X[C][end][0]>=x2:
                            index=end
                        else:
                            index=k
                    #index-1,indexのやつと交差判定
                    if index!=0:
                        x3,y3,x4,y4=X[C][index-1]
                        if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                            print("NO")
                            exit()
                        else:
                            if y3==y4:
                                if x4>=x2>=x3:
                                    print("NO")
                                    exit()
                    if index!=k:
                        x3,y3,x4,y4=X[C][index]
                        if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                            print("NO")
                            exit()
                        else:
                            if y3==y4:
                                if x4>=x2>=x3:
                                    print("NO")
                                    exit()

        if X[0]:
            for i in range(0,k):
                x1,y1,x2,y2=X[C][i]
                if y2==0:
                    start=0
                    end=m-1
                    while end-start>1:
                        test=(end+start)//2
                        if X[0][test][0]>=x2:
                            end=test
                        else:
                            start=test

                    if X[0][start][0]>=x2:
                        index=start
                    else:
                        if X[0][end][0]>=x2:
                            index=end
                        else:
                            index=m
                    #index-1,indexのやつと交差判定
                    if index!=0:
                        x3,y3,x4,y4=X[0][index-1]
                        if y3==y4:
                            if x4>=x2>=x3:
                                print("NO")
                                exit()

                    if index!=m:
                        x3,y3,x4,y4=X[0][index]
                        if y3==y4:
                            if x4>=x2>=x3:
                                print("NO")
                                exit()
        print("YES")

    elif yaxis:
        r=R
        c=C
        C=r
        R=c
        for i in range(0,n):
            x1,y1,x2,y2=line[i]
            line[i]=[y1,x1,y2,x2]
        condition()
    else:
        print("NO")
        exit()

condition()

Submission status: Wrong Answer

Solution code:
import sys

input=sys.stdin.readline

def check_intersection(ax, ay, bx, by, cx, cy, dx, dy):
    dax, day = ax - bx, ay - by
    dcx, dcy = cx - dx, cy - dy
    ta = dcx * (ay - cy) + dcy * (cx - ax)
    tb = dcx * (by - cy) + dcy * (cx - bx)
    tc = dax * (cy - ay) + day * (ax - cx)
    td = dax * (dy - ay) + day * (ax - dx)
    return tc * td < 0 and ta * tb < 0

R,C,N=map(int,input().split())

def is_on_edge(x,y):
    return x==0 or x==R or y==0 or y==C

line=[]
for i in range(0,N):
    x1,y1,x2,y2=map(int,input().split())
    if is_on_edge(x1,y1) and is_on_edge(x2,y2):
        line.append([x1,y1,x2,y2])

def condition():
    global R,C,line
    n=len(line)
    def is_on_xaxis(x,y):
        return y==0 or y==C

    def is_on_yaxis(x,y):
        return x==0 or x==R

    xaxis=True
    for i in range(0,n):
        x1,y1,x2,y2=line[i]
        if (not is_on_xaxis(x1,y1) ) and (not is_on_xaxis(x2,y2)):
            xaxis=False
            break

    yaxis=True
    for i in range(0,n):
        x1,y1,x2,y2=line[i]
        if (not is_on_yaxis(x1,y1) ) and (not is_on_yaxis(x2,y2)):
            yaxis=False
            break

    if xaxis:
        X={0:[],C:[]}
        for i in range(0,n):
            x1,y1,x2,y2=line[i]
            if y1==0:
                if y2!=0:
                    X[0].append(line[i])
                else:
                    if x1>x2:
                        X[0].append([x2,y2,x1,y1])
            else:
                if y2!=C:
                    X[C].append(line[i])
                else:
                    if x1>x2:
                        X[C].append([x2,y2,x1,y1])

        m=len(X[0])
        k=len(X[C])
        X[0].sort()
        X[C].sort()
        for i in range(0,m-1):
            x1,y1,x2,y2=X[0][i]
            x3,y3,x4,y4=X[0][i+1]
            if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                print("NO")
                exit()
            else:
                if y1==y2:
                    if x2>=x3>=x1:
                        print("NO")
                        exit()

        for i in range(0,k-1):
            x1,y1,x2,y2=X[C][i]
            x3,y3,x4,y4=X[C][i+1]
            if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                print("NO")
                exit()
            else:
                if y1==y2:
                    if x2>=x3>=x1:
                        print("NO")
                        exit()
        if X[C]:
            for i in range(0,m):
                x1,y1,x2,y2=X[0][i]
                if y2==C:
                    start=0
                    end=k-1
                    while end-start>1:
                        test=(end+start)//2
                        if X[C][test][0]>=x2:
                            end=test
                        else:
                            start=test

                    if X[C][start][0]>=x2:
                        index=start
                    else:
                        if X[C][end][0]>=x2:
                            index=end
                        else:
                            index=k
                    #index-1,indexのやつと交差判定
                    if index!=0:
                        x3,y3,x4,y4=X[C][index-1]
                        if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                            print("NO")
                            exit()
                        else:
                            if y3==y4:
                                if x4>=x2>=x3:
                                    print("NO")
                                    exit()
                    if index!=k:
                        x3,y3,x4,y4=X[C][index]
                        if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                            print("NO")
                            exit()
                        else:
                            if y3==y4:
                                if x4>=x2>=x3:
                                    print("NO")
                                    exit()

        if X[0]:
            for i in range(0,k):
                x1,y1,x2,y2=X[C][i]
                if y2==0:
                    start=0
                    end=m-1
                    while end-start>1:
                        test=(end+start)//2
                        if X[0][test][0]>=x2:
                            end=test
                        else:
                            start=test

                    if X[0][start][0]>=x2:
                        index=start
                    else:
                        if X[0][end][0]>=x2:
                            index=end
                        else:
                            index=m
                    #index-1,indexのやつと交差判定
                    if index!=0:
                        x3,y3,x4,y4=X[0][index-1]
                        if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                            print("NO")
                            exit()
                        else:
                            if y3==y4:
                                if x4>=x2>=x3:
                                    print("NO")
                                    exit()
                    if index!=m:
                        x3,y3,x4,y4=X[0][index]
                        if check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
                            print("NO")
                            exit()
                        else:
                            if y3==y4:
                                if x4>=x2>=x3:
                                    print("NO")
                                    exit()
        print("YES")

    elif yaxis:
        r=R
        c=C
        C=r
        R=c
        for i in range(0,n):
            x1,y1,x2,y2=line[i]
            line[i]=[y1,x1,y2,x2]
        condition()
    else:
        print("NO")
        exit()

condition()

Submission status: Wrong Answer

Solution code:
print("YES")