PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000304. Hành lang vòng tròn (Round Corridor)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Hành lang vòng tròn (Round Corridor)

Round Corridor

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A circular corridor consists of two concentric rings. The inner ring has nn equal sectors numbered (1,1),(1,2),…,(1,n)(1,1),(1,2),\ldots,(1,n) clockwise. The outer ring has mm equal sectors numbered (2,1),(2,2),…,(2,m)(2,1),(2,2),\ldots,(2,m) in the same direction.

There are walls between adjacent sectors of the same ring, but no wall separates the inner and outer rings, so a person may move between rings wherever no wall blocks the path. The twelve-o’clock position is a wall boundary for both rings.

For each of qq independent queries, determine whether a person can move from sector (sx,sy)(s_x,s_y) to sector (ex,ey)(e_x,e_y) without passing through a wall.

Input

The first line contains n,m,qn,m,q: the sector counts of the two rings and the number of queries.

Each of the next qq lines contains sx,sy,ex,eys_x,s_y,e_x,e_y. The ring identifiers sx,exs_x,e_x are 11 or 22, while sy,eys_y,e_y are sector indices within the specified rings.

Output

For each query, print YES if the destination is reachable and NO otherwise, preserving query order.

Subtasks

  • Subtask 1 (20 points): 1≤n,m≤1001\le n,m\le100; 1≤q≤301\le q\le30; sx,ex∈{1,2}s_x,e_x\in\{1,2\}; 1≤sy≤n1\le s_y\le n if sx=1s_x=1, otherwise 1≤sy≤m1\le s_y\le m; 1≤ey≤n1\le e_y\le n if ex=1e_x=1, otherwise 1≤ey≤m1\le e_y\le m.
  • Subtask 2 (30 points): 1≤n,m≤1091\le n,m\le10^9; 1≤q≤10001\le q\le1000; sx,ex∈{1,2}s_x,e_x\in\{1,2\}; 1≤sy≤n1\le s_y\le n if sx=1s_x=1, otherwise 1≤sy≤m1\le s_y\le m; 1≤ey≤n1\le e_y\le n if ex=1e_x=1, otherwise 1≤ey≤m1\le e_y\le m.
  • Subtask 3 (50 points): 1≤n,m≤10181\le n,m\le10^{18}; 1≤q≤1041\le q\le10^4; sx,ex∈{1,2}s_x,e_x\in\{1,2\}; 1≤sy≤n1\le s_y\le n if sx=1s_x=1, otherwise 1≤sy≤m1\le s_y\le m; 1≤ey≤n1\le e_y\le n if ex=1e_x=1, otherwise 1≤ey≤m1\le e_y\le m.

Examples

Example 1

Input

4 6 3
1 1 2 3
2 6 1 2
2 6 2 4

Output

YES
NO
YES

Explanation

There are gcd⁡(4,6)=2\gcd(4,6)=2 connected components. Each component contains two consecutive inner sectors or three consecutive outer sectors. Query 1: (1,1)(1,1) and (2,3)(2,3) belong to group 00, so YES. Query 2: (2,6)(2,6) is in group 11 but (1,2)(1,2) is in group 00, so NO. Query 3: (2,6)(2,6) and (2,4)(2,4) are both in group 11, so YES.