Jeronimo the bear loves numbers and he is planning to write n numbers in his notebook.
After writing the first m numbers, Jeronimo felt that he was spending a lot of time thinking new numbers, so he wrote the next n - m missing numbers as the sum modulo 3 × 107 of the numbers in the i - m and i - m + 1 positions for m < i ≤ n
While Jeronimo was writing, his sister Lupe arrived and asked him q questions. Thei - th question consist of a number bi, Jeronimo has to say what would be the number in the position bi if all the numbers were sorted in ascending order. Jeronimo wants to answer each question as soon as possible but he spends a lot of time counting so he ask your help.
Input
The first line of the input has three integers n (3 ≤ n ≤ 3 × 107), m (3 ≤ m ≤ min(100, n)) and q (1 ≤ q ≤ 10000).
The second line contains m numbers a1, a2, ..., am, (0 ≤ ai < 3 × 107), The first mnumbers that Jeronimo wrote.
The third line contains q questions b1, b2, ..., bq (1 ≤ bi ≤ n)
Output
Print q lines. The i - th line must be the answer of the i - th question made by Lupe.
Examples
Input
6 3 6
1 2 3
1 2 3 4 5 6
Output
1
2
3
3
5
6
Input
10 4 3
1 2 9 10
1 5 10
Output
1
10
30
题意:一共n个数字(3<=n<=3e7, 0<=ai<3e7 ),给出前面的m个(3<=m<=min(100, n)),a[i] = (a[i-m] + a[i-m+1]) % MOD,q个询问(1<=q<=1e4),询问a[1,n]里的从小到大第bi大。
思路:先观察这个题的数据,n 的范围为 3e7, 快排 归并 排序的复杂度都是 O(N* log N), 大概 6e8 左右,显然会超时,。。。所以就用到了 另一个排序算法, 桶排序.... 其最好的时间复杂度为 O( N );
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e7 + 10;
const int mod = 3e7;
int a[maxn];
bool bucket[maxn];
int num[maxn];
int s[maxn];
int main()
{
int n,m,q;
while(~scanf("%d%d%d",&n,&m,&q)){
for(int i = 0;i < maxn;i ++){
bucket[i] = 0;
num[i] = 0;
}
for(int i = 0;i < m;i ++)
scanf("%d",&a[i]);
for(int i = 0;i < m;i ++){
a[m ++] = (a[i] % mod + a[i + 1] % mod) % mod;
if(m == n) break;
}
int maxx = -1;
for(int i = 0;i < n;i ++){
if(maxx < a[i]) maxx = a[i];
bucket[a[i]] = 1;
num[a[i]] ++;
}
int p = 0;
for(int i = 0;i < maxx + 1;i ++){
if(bucket[i] == 1){
for(int j = 0;j < num[i];j ++)
s[p ++] = i;
}
}
while(q --){
int x; scanf("%d",&x);
printf("%d\n",s[x - 1]);
}
}
return 0;
}