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

#BS0000221. Người dẫn đầu cuộc bỏ phiếu (Online Election)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Người dẫn đầu cuộc bỏ phiếu (Online Election)

Online Election

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn votes in strictly increasing chronological order. Vote ii is for candidate aia_i at time bib_i. Candidate identifiers range from 0 to n−1n-1.

For each query time tt, count votes with bi≤tb_i\le t. The leader is the candidate with the greatest count. Among tied leaders, choose the candidate whose most recent vote occurred latest.

Input

The first line contains n,qn,q. The second line contains the nn candidate identifiers aia_i. The third line contains the nn vote timestamps bib_i. Each of the next qq lines contains a query time tjt_j.

Output

Print qq lines, one leader identifier for each query in input order.

Subtasks

  • Subtask 1 (20 points): 1≤n,q≤1001\le n,q\le100; 0≤ai<n0\le a_i<n; 0≤bi≤1090\le b_i\le10^9 for every 1≤i≤n1\le i\le n; b1<⋯<bnb_1<\cdots<b_n; b1≤tj≤109b_1\le t_j\le10^9 for every 1≤j≤q1\le j\le q.
  • Subtask 2 (30 points): 1≤n≤50001\le n\le5000; 1≤q≤1041\le q\le10^4; 0≤ai<n0\le a_i<n; 0≤bi≤1090\le b_i\le10^9 for every 1≤i≤n1\le i\le n; b1<⋯<bnb_1<\cdots<b_n; b1≤tj≤109b_1\le t_j\le10^9 for every 1≤j≤q1\le j\le q; t1≤⋯≤tqt_1\le\cdots\le t_q.
  • Subtask 3 (50 points): 1≤n≤50001\le n\le5000; 1≤q≤1041\le q\le10^4; 0≤ai<n0\le a_i<n; 0≤bi≤1090\le b_i\le10^9 for every 1≤i≤n1\le i\le n; b1<⋯<bnb_1<\cdots<b_n; b1≤tj≤109b_1\le t_j\le10^9 for every 1≤j≤q1\le j\le q.

Examples

Input

4 5
0 1 1 0
0 5 10 15
0
5
14
15
3

Output

0
1
1
0
0

Explanation

At time 0 only candidate 0 has a vote. At 5 both have one, and 1 wins the tie by recency. At 14 candidate 1 has two against one. At 15 both have two, and 0 received the latest vote. At 3 only the first vote counts, so the leader is 0.