LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion FindSqrt Decomposition
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsConvex Hull
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPDP problemsTree DPBitmask DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

Inside the Reserve

MediumProblem #44
Time LimitMemoryInputOutput
1 s64 MBstdinstdout

The fence itself counts as inside.

A nature reserve is fenced off along a closed line that never crosses itself. The fence is described by its n corners, listed in the order somebody walking along it would pass them - either clockwise or counter-clockwise, we are not told which. After the last corner the fence goes straight back to the first one.

A ranger stands at the point T and wants to know whether they are inside the reserve. Standing on the fence, corners included, counts as being inside.

Input

The first line contains a single integer t - the number of testcases.

  • The first line of each testcase contains a single integer n - the number of corners.
  • Each of the next n lines contains two integers xi​ and yi​ - one corner of the fence, in walking order.
  • The last line of the testcase contains two integers Tx​ and Ty​ - where the ranger stands.

Output

For every testcase print YES if the ranger is inside the reserve, and NO otherwise.

Example

Input
2
8
0 0
5 0
5 1
1 1
1 3
5 3
5 4
0 4
2 2
4
0 0
5 0
5 5
0 5
2 2
Output
NO
YES

The first fence is a wide letter C opening to the right, and the point (2,2) sits in the notch between its two arms - outside the reserve, even though it looks surrounded. The second fence is a plain square with the point comfortably in the middle.

Constraints

1≤t≤1000
3≤n≤5⋅104
−109≤xi​,yi​≤109
−109≤Tx​,Ty​≤109
the fence never crosses itself, and no two neighbouring corners are the same point
n1​+n2​+…+nt​≤105


This problem was adapted, with permission, from Pripadnost tačke prostom poligonu, authored by Društvo matematičara Srbije and Fondacija Petlja.

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit