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对象。

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<m;i++) {
			rec[in.nextInt()]++;
		}
		long[][] A=new long[n][2];
		for(int i=0;i<n;i++) {
			A[i]=new long[] {rec[i+1],i+1};
		}
		Arrays.sort(A,new Comparator<long[]>() {
			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<q;i++) {
			Q[i]=new long[]{in.nextLong(),i};
		}
		Arrays.sort(Q,new Comparator<long[]>() {
			public int compare(long[] arr1,long[] arr2) {
				if(arr1[0]<arr2[0])return-1;
				else 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<q;i++) {
			long[] query=Q[i];
			long r=query[0]-cur;
			while(j<n-1&&(A[j+1][0]-A[j][0])*(j+1)<r) {
				r-=(A[j+1][0]-A[j][0])*(j+1);
				cur+=(A[j+1][0]-A[j][0])*(j+1);
				insert((int)A[j+1][1],root,1,n);
				j++;
			}
			int v=(int)(r%(j+1));
			if(v==0)v=j+1;
			int t=find(v,root,1,n);//第v大的编号
			ret[(int)query[1]]=t;
		}
		for(int i=0;i<q;i++)out.println(ret[i]);
	}
	
	private int find(int v,Node node,int l,int r) {
		if(l==r)return l;
		int m=(l+r)/2;
		if(node.lNode.cnt>=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());
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值