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 ProductLinesPolygonsAnglesPoint in PolygonDistances and Intersection PointsConvex HullCircles
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

Pulling the Plate Apart

HardProblem #47
Time LimitMemoryInputOutput
1 s64 MBstdinstdout

Look at the directions the cut travels in, not at where it is.

A rectangular steel plate lies flat on a workbench. Its bottom left corner is at (0,0) and its top right corner is at (W,H).

A cutter has sliced the plate in two along a broken line that starts somewhere on the bottom edge, wanders through the plate, and ends somewhere on the top edge. The line never crosses itself, so the plate really does fall into exactly two pieces.

The steel is thick and heavy. The pieces cannot be bent, and they cannot be lifted off the bench - the only thing you are allowed to do is slide one piece across the bench, in a straight line, in a single direction, as far as you like. The other piece stays where it is.

Your task is to decide whether the two pieces can be pulled apart this way.

Input

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

  • The first line of each testcase contains three integers W, H and n - the width of the plate, its height, and the number of points of the cut.
  • Each of the next n lines contains two integers xi​ and yi​ - one point of the cut, given in the order you walk along it.

The first point lies on the bottom edge (y1​=0) and the last one lies on the top edge (yn​=H). Every other point lies strictly inside the plate. No two consecutive points are equal, and the cut never touches or crosses itself.

Output

For every testcase print a single line: YES if the two pieces can be pulled apart with one straight slide, and NO if they cannot.

Example

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

The second cut is a plain zigzag, and the two pieces come apart if you slide one of them straight to the right. The first cut doubles back on itself, and every direction you might try drives one piece into the other.

Constraints

1≤t≤10
2≤n≤5⋅104
1≤W,H≤106
0≤xi​≤W
0≤yi​≤H
n1​+n2​+…+nt​≤105


This problem was adapted, with permission, from Rastav translacijom, 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