Codeforces 1181D. Irrigation

https://codeforces.com/contest/1181/status/D

Misha was interested in water delivery from childhood. That's why his mother sent him to the annual Innovative Olympiad in Irrigation (IOI). Pupils from all Berland compete there demonstrating their skills in watering. It is extremely expensive to host such an olympiad, so after the first nn olympiads the organizers introduced the following rule of the host city selection.

The host cities of the olympiads are selected in the following way. There are mm cities in Berland wishing to host the olympiad, they are numbered from 11 to mm. The host city of each next olympiad is determined as the city that hosted the olympiad the smallest number of times before. If there are several such cities, the city with the smallest index is selected among them.

Misha's mother is interested where the olympiad will be held in some specific years. The only information she knows is the above selection rule and the host cities of the first nn olympiads. Help her and if you succeed, she will ask Misha to avoid flooding your house.

Input

The first line contains three integers nn, mm and qq (1≤n,m,q≤5000001≤n,m,q≤500000) — the number of olympiads before the rule was introduced, the number of cities in Berland wishing to host the olympiad, and the number of years Misha's mother is interested in, respectively.

The next line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤m1≤ai≤m), where aiai denotes the city which hosted the olympiad in the ii-th year. Note that before the rule was introduced the host city was chosen arbitrarily.

Each of the next qq lines contains an integer kiki (n+1≤ki≤1018n+1≤ki≤1018) — the year number Misha's mother is interested in host city in.

Output

Print qq integers. The ii-th of them should be the city the olympiad will be hosted in the year kiki.

Examples

input

Copy

6 4 10
3 1 1 1 2 2
7
8
9
10
11
12
13
14
15
16

output

Copy

4
3
4
2
3
4
1
2
3
4

input

Copy

4 5 4
4 4 5 1
15
9
13
6

output

Copy

5
3
3
3

Note

In the first example Misha's mother is interested in the first 1010 years after the rule was introduced. The host cities these years are 4, 3, 4, 2, 3, 4, 1, 2, 3, 4.

In the second example the host cities after the new city is introduced are 2, 3, 1, 2, 3, 5, 1, 2, 3, 4, 5, 1.

 

离线查询,因为每次要查询第K大编号,所以还要用权值线段树动态的insert编号,并查询。

参考:

https://codeforces.com/blog/entry/67727  

https://blog.csdn.net/qq_39565901/article/details/81782611

https://www.cnblogs.com/wzj-xhjbk/p/11039510.html

https://www.cnblogs.com/ilverene/p/9839660.html

https://codeforces.com/contest/1181/submission/55629074

权值线段树维护数组值的对应信息,普通线段树维护下标的对应信息(一个区间基于值,一个基于下标)。在范围大的时候,用对象写权值线段树在需要的时候再new对象。

Codeforces 1181D. Irrigation_第1张图片

import java.util.*;
import java.io.*;

public class Main {
	public static void main(String args[]) {new Main().run();}

	FastReader in = new FastReader();
	PrintWriter out = new PrintWriter(System.out);
	void run(){
		work();
		out.flush();
	}
	long mod=1000000007;
	long gcd(long a,long b) {
		return b==0?a:gcd(b,a%b);
	}
	void work() {
		int m=in.nextInt();
		int n=in.nextInt();
		int q=in.nextInt();
		long[] rec=new long[n+1];
		for(int i=0;i() {
			public int compare(long[] arr1,long[] arr2) {
				return (int)(arr1[0]-arr2[0]);
			}
		});
		long cur=m;
		int[] ret=new int[q];
		long[][] Q=new long[q][2];
		for(int i=0;i() {
			public int compare(long[] arr1,long[] arr2) {
				if(arr1[0]arr2[0]) {
					return 1;
				}else {
					return 0;
				}
			}
		});
		Node root=new Node(0);
		insert((int)A[0][1],root,1,n);
		for(int i=0,j=0;i=v) {
			return find(v,node.lNode,l,m);
		}else {
			return find(v-node.lNode.cnt,node.rNode,m+1,r);
		}
	}
	private void insert(int v, Node node, int l, int r) {
		node.cnt++;
		if(l==r)return;
		if(node.lNode==null)node.lNode=new Node(0);
		if(node.rNode==null)node.rNode=new Node(0);
		int m=(l+r)/2;
		if(v<=m) {
			insert(v,node.lNode,l,m);
		}else {
			insert(v,node.rNode,m+1,r);
		}
	}

	class Node{
		Node lNode;
		Node rNode;
		int cnt;
		Node(int c){
			cnt=c;
		}
	}
}



class FastReader
{
	BufferedReader br;
	StringTokenizer st;

	public FastReader()
	{
		br=new BufferedReader(new InputStreamReader(System.in));
	}

	public String next() 
	{
		if(st==null || !st.hasMoreElements())
		{
			try {
				st = new StringTokenizer(br.readLine());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return st.nextToken();
	}

	public int nextInt() 
	{
		return Integer.parseInt(next());
	}

	public long nextLong()
	{
		return Long.parseLong(next());
	}
}

 

你可能感兴趣的:(Codeforces)