#P17198. [KOI 2026 #2] 杂技

[KOI 2026 #2] 杂技

Problem Description

Alice and Bob, two acrobats, are preparing to perform on a balance beam. The balance beam consists of NN consecutive cells, numbered from 11 to NN from left to right.

During the performance, at any moment, each acrobat must stand on exactly one cell. To ensure safety, at all times the index of the cell where Alice stands must be less than the index of the cell where Bob stands. That is, Alice must always stand to the left of Bob, and they cannot stand on the same cell.

There are MM springboards installed on the balance beam. The ii-th (1≤i≤M1 \le i \le M) springboard is installed on cell xix_i. After an acrobat standing on cell xix_i uses this springboard, they will land exactly on cell yiy_i.

Multiple springboards may be installed on the same cell. Both acrobats may use any springboard any number of times.

A performance consists of executing any number of actions (possibly 00). In one action, exactly one of the two acrobats performs one of the following two operations:

  1. Walk: Alice may move one cell to the right from her current cell; Bob may move one cell to the left from his current cell. Alice cannot walk left, and Bob cannot walk right.
  2. Jump: Choose and use a springboard installed on the cell where the acrobat is currently standing. That is, for some integer ii (1≤i≤M1 \le i \le M), an acrobat standing on cell xix_i may use the ii-th springboard and land on cell yiy_i.

After an action is performed, Alice must still stand on a cell to the left of Bob. Any action that would violate this condition cannot be performed.

The two acrobats have QQ performance plans. The jj-th (1≤j≤Q1 \le j \le Q) performance plan is given by four integers aj,bj,cj,dja_j,b_j,c_j,d_j satisfying 1≤aj<bj≤N1 \le a_j<b_j \le N and 1≤cj<dj≤N1 \le c_j<d_j \le N. The jj-th plan is called valid if, by performing some sequence of actions, starting from the state where Alice and Bob stand on cells aja_j and bjb_j respectively, they can eventually end in the state where they stand on cells cjc_j and djd_j respectively.

For each of the QQ performance plans, determine whether it is valid.

Input Format

The first line contains two space-separated integers NN and MM.

The next MM lines give information about the MM springboards. Line ii (1≤i≤M1 \le i \le M) contains two space-separated integers xix_i and yiy_i, describing the ii-th springboard.

The next line contains an integer QQ, the number of performance plans.

The next QQ lines describe the QQ performance plans. Line jj (1≤j≤Q1 \le j \le Q) contains four space-separated integers aj,bj,cj,dja_j,b_j,c_j,d_j, describing the jj-th performance plan.

Output Format

Output QQ lines starting from the first line. Line jj (1≤j≤Q1 \le j \le Q) should be the answer to the jj-th performance plan: if it is possible to start with Alice and Bob standing on cells aja_j and bjb_j respectively and eventually make them stand on cells cjc_j and djd_j respectively, output YES; otherwise output NO.

6 2
3 5
4 2
4
2 4 3 4
2 3 2 5
2 3 4 5
3 4 1 2
YES
YES
YES
NO
10 3
5 10
6 8
7 4
5
5 6 4 10
5 6 3 9
1 10 2 9
6 8 4 9
9 10 1 10
YES
NO
YES
YES
NO

Hint

Explanation of Sample 1

In the second performance plan, Alice and Bob start on cells 22 and 33 respectively. Bob uses the springboard on cell 33 to move to cell 55. Then Alice and Bob are on cells 22 and 55 respectively, reaching the target state.

In the fourth performance plan, when Alice and Bob are on cells 33 and 44 respectively, no action can be performed.

  • If Alice walks right, both would stand on cell 44, violating the condition.
  • If Bob walks left, both would stand on cell 33, violating the condition.
  • If Bob uses the springboard on cell 44 and lands on cell 22, he would stand to the left of Alice on cell 33, violating the condition.

Therefore, it is impossible to reach the target state where Alice and Bob stand on cells 11 and 22 respectively.

Constraints

  • All given values are integers.
  • 2≤N≤200 0002 \le N \le 200\,000.
  • 0≤M≤200 0000 \le M \le 200\,000.
  • 1≤Q≤500 0001 \le Q \le 500\,000.
  • For each integer ii (1≤i≤M1 \le i \le M), 1≤xi,yi≤N1 \le x_i,y_i \le N and xi≠yix_i \ne y_i.
  • For each integer jj (1≤j≤Q1 \le j \le Q), 1≤aj<bj≤N1 \le a_j<b_j \le N and 1≤cj<dj≤N1 \le c_j<d_j \le N.

Subtasks

  1. (88 points) N,M,Q≤100N,M,Q \le 100.
  2. (1414 points) For each integer ii (1≤i≤M1 \le i \le M), xi<yix_i<y_i.
  3. (1313 points) N≤3 000N \le 3\,000.
  4. (1313 points) Q≤10Q \le 10.
  5. (5252 points) No additional constraints.

Translated by ChatGPT-5.6.

Translated by ChatGPT 5