Skip to content

368B. Sereja and Suffixes

data structures/dp, 1100, https://codeforces.com/problemset/problem/368/B

Sereja has an array a, consisting of n integers a~1~, a~2~, ..., a~n~. The boy cannot sit and do nothing, he decided to study an array. Sereja took a piece of paper and wrote out m integers l1,l2,...,lm  (1 ≤ li ≤ n). For each number li he wants to know how many distinct numbers are staying on the positions li,li+1,...,n. Formally, he want to find the number of distinct numbers among ali,ali+1,...,an.?

Sereja wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each li.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 10^5^). The second line contains n integers a1,a2,...,an(1ai105) — the array elements.

Next m lines contain integers l1,l2,...,lm. The i-th line contains integer li (1 ≤ li ≤ n).

Output

Print m lines — on the i-th line print the answer to the number li.

Examples

input

10 10
1 2 3 4 1 2 3 4 100000 99999
1
2
3
4
5
6
7
8
9
10

output

6
6
6
6
6
5
4
3
2
1

要求数据结构实现一次,dp实现一次

DP方法:

2020fall-cs101,郭冠廷。本题的思路不困难,只需反序 dp即可(甚至看不出 dp)。为了节省时间,应用到了两个窍门:1)将已经读取的数据投入到 set中,这样能避免列表查找的缓速。2)将答案储存在一个列表中,同时输出。二者都是以空间换时间。

python
n,m = map(int, input().split())
nl = [int(x) for x in input().split()]

out = 0
dp = []

tset = set()
for i in reversed(nl):
    if i not in tset:
        out += 1
        tset.add(i)
    dp.append(out)

for _ in range(m):
    pos = len(nl) - int(input())
    print(dp[pos])

2020fall-cs101,王康安。最开始用 in去判断,tle了。于是开一个到一万的桶,空间换时间,成功 AC。

python
n,m = map(int,input().split())
seq = [int(x) for x in input().split()]
dp = [0]*(n-1)+[1]
barrel = [False]*(100000+1)
barrel[seq[n-1]] = True
for i in reversed(range(n-1)):
    if barrel[seq[i]]:        
        dp[i] = dp[i+1]
    else:        
        dp[i] = dp[i+1]+1        
        barrel[seq[i]] = True
for i in range(m):
    print(dp[int(input())-1])

data structures方法:

2020fall-cs101,蓝克轩。解题思路:先用dictionary记录每个元素数量,然后从第一个元素开始,每一个元素从dictionary减一,如果在dictionary对应对应的因素是0就去除,dictionary的长度即是不同元素的数量。

python
n, m = map(int,input().split())
a = list(map(int, input().split()))
adic = {}
for i in range(n):
    if a[i] in adic:
        adic[a[i]] += 1
    else:
        adic[a[i]] = 1
 
ans = [0]*n
for i in range(n):
    if a[i] in adic:
        if adic[a[i]] == 1:
            adic.pop(a[i])
            ans[i] += 1
        else:
            adic[a[i]] -= 1
    ans[i] += len(adic)
 
for i in range(m):
    print(ans[int(input()) - 1])