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

#BS0000175. Chọn điểm nguyên bằng vé trên các hình chữ nhật (Random Point in Non-overlapping Rectangles)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Chọn điểm nguyên bằng vé trên các hình chữ nhật (Random Point in Non-overlapping Rectangles)

Random Point in Non-overlapping Rectangles — Ticket Mapping

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

You are given nn pairwise non-overlapping axis-aligned rectangles. Rectangle ii has bottom-left corner (ai,bi)(a_i,b_i) and top-right corner (xi,yi)(x_i,y_i). Boundary points are included. Its number of integer lattice points is (xi−ai+1)(yi−bi+1)(x_i-a_i+1)(y_i-b_i+1).

Number every lattice point starting at 11: visit rectangles in the input order; within each rectangle, visit rows by increasing yy, and visit points within each row by increasing xx. For each given ticket number rjr_j, output the coordinates of the point bearing that ticket.

PHOJ Extended adaptation: The original LeetCode 497 API independently samples a lattice point uniformly at random. This version supplies tickets and checks their exact mapping to points. Uniform tickets generate a uniform distribution on all lattice points, but the contestant's random-number generator is not evaluated.

Input

The first line contains integers n,qn,q. Each of the following nn lines contains four integers ai,bi,xi,yia_i,b_i,x_i,y_i. The last line contains qq ticket numbers r1,r2,…,rqr_1,r_2,\ldots,r_q.

Output

Print qq lines. Line jj contains two integers u v, the coordinates of the point owning ticket rjr_j.

Subtasks

  • Subtask 1 (20 points): 1≤n≤51\le n\le5; 1≤q≤301\le q\le30; −100≤ai<xi≤100-100\le a_i<x_i\le100; −100≤bi<yi≤100-100\le b_i<y_i\le100; 1≤xi−ai≤101\le x_i-a_i\le10; 1≤yi−bi≤101\le y_i-b_i\le10; rectangles are pairwise non-overlapping; 1≤rj≤∑i=1n(xi−ai+1)(yi−bi+1)1\le r_j\le\sum_{i=1}^n(x_i-a_i+1)(y_i-b_i+1) for every ticket.
  • Subtask 2 (30 points): 1≤n≤301\le n\le30; 1≤q≤20001\le q\le2000; −100000≤ai<xi≤100000-100000\le a_i<x_i\le100000; −100000≤bi<yi≤100000-100000\le b_i<y_i\le100000; 1≤xi−ai≤1001\le x_i-a_i\le100; 1≤yi−bi≤1001\le y_i-b_i\le100; rectangles are pairwise non-overlapping; 1≤rj≤∑i=1n(xi−ai+1)(yi−bi+1)1\le r_j\le\sum_{i=1}^n(x_i-a_i+1)(y_i-b_i+1) for every ticket.
  • Subtask 3 (50 points): 1≤n≤1001\le n\le100; 1≤q≤100001\le q\le10000; −1000000000≤ai<xi≤1000000000-1000000000\le a_i<x_i\le1000000000; −1000000000≤bi<yi≤1000000000-1000000000\le b_i<y_i\le1000000000; 1≤xi−ai≤20001\le x_i-a_i\le2000; 1≤yi−bi≤20001\le y_i-b_i\le2000; rectangles are pairwise non-overlapping; 1≤rj≤∑i=1n(xi−ai+1)(yi−bi+1)1\le r_j\le\sum_{i=1}^n(x_i-a_i+1)(y_i-b_i+1) for every ticket.

Examples

Input

2 5
-2 -2 1 1
2 2 4 6
1 16 17 19 31

Output

-2 -2
1 1
2 2
4 2
4 6

Explanation

The first rectangle contains 4×4=164\times4=16 points, while the second contains 3×5=153\times5=15 points, making 3131 points in total. Ticket 11 maps to (−2,−2)(-2,-2); ticket 1616 to (1,1)(1,1); ticket 1717 to (2,2)(2,2); ticket 1919 to the third point on the first row of the second rectangle, (4,2)(4,2); and ticket 3131 to (4,6)(4,6).