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

#FT0000151. Truy vấn tổng lớn nhất (Maximum Sum Queries)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Truy vấn tổng lớn nhất (Maximum Sum Queries)

Maximum Sum Queries

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

You are given two arrays of positive integers A1,…,AnA_1,\ldots,A_n and B1,…,BnB_1,\ldots,B_n. Index ii defines a pair (Ai,Bi)(A_i,B_i) and score Ai+BiA_i+B_i.

There are qq independent queries (xj,yj)(x_j,y_j). For a query you may select only an index ii meeting both Ai≥xjA_i\ge x_j and Bi≥yjB_i\ge y_j. If at least one eligible index exists, output the maximum Ai+BiA_i+B_i among eligible indices; otherwise output -1.

Queries never remove or update array elements. Output answers in original query order.

Input

The first line contains n,qn,q. The second line contains all AiA_i and the third contains all BiB_i. The next qq lines contain thresholds xj,yjx_j,y_j.

Output

Print qq lines. Line jj gives the maximum qualifying sum or -1 if no index is eligible.

Subtasks

  • Subtask 1 (20 points): 1≤n,q≤251\le n,q\le 25; 1≤Ai,Bi,xj,yj≤1091\le A_i,B_i,x_j,y_j\le 10^9 for all array positions and queries.
  • Subtask 2 (30 points): 1≤n,q≤20001\le n,q\le 2000; 1≤Ai,Bi,xj,yj≤1091\le A_i,B_i,x_j,y_j\le 10^9 for all array positions and queries.
  • Subtask 3 (50 points): 1≤n,q≤1000001\le n,q\le 100000; 1≤Ai,Bi,xj,yj≤1091\le A_i,B_i,x_j,y_j\le 10^9 for all array positions and queries.

Examples

Example 1

Input

4 3
4 3 1 2
2 4 9 5
4 1
1 3
2 5

Output

6
10
7

Explanation

For (4,1)(4,1), only (4,2)(4,2) qualifies, with sum 6. For (1,3)(1,3), qualified sums are 3+4=73+4=7, 1+9=101+9=10 and 2+5=72+5=7, so the maximum is 10. For (2,5)(2,5), only (2,5)(2,5) qualifies, giving 7.

Example 2

Input

2 3
2 1
2 3
3 3
1 3
2 2

Output

-1
4
4

Explanation

Query (3,3)(3,3) has no index with Ai≥3A_i\ge3, hence -1. For (1,3)(1,3) only pair (1,3)(1,3) qualifies, giving 4; for (2,2)(2,2) only (2,2)(2,2) qualifies, also giving 4.