Sereja has an arrayA, ConsistingNIntegersA1,A2 ,...,AN. The boy cannot sit and do nothing, he decided to study an array. sereja took a piece of paper and wrote outMIntegersL1 ,?L2 ,?...,?LM(1? ≤?LI? ≤?N). For each numberLIHe wants to know how many distinct numbers are staying on the positionsLI,LI? +? 1 ,...,N. Formally, he want to find the number of distinct numbersALI,?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 eachLI.
Input
The first line contains two integersNAndM(1? ≤?N,?M? ≤? 105). The second line containsNIntegersA1,A2 ,...,AN(1? ≤?AI? ≤? 105)-the array elements.
NextMLines contain IntegersL1 ,?L2 ,?...,?LM.I-Th line contains integerLI(1? ≤?LI? ≤?N).
Output
PrintMLines-onI-Th line print the answer to the numberLI.
Sample test (s) Input
10 101 2 3 4 1 2 3 4 100000 9999912345678910
Output
6666654321
Hash resolution. to query the different numbers after each position, make the position of each number closer to the backend, and look for the last position and hash record of each number, and the number of recursion.
#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h>#include <ctype.h>#include <iostream>#define lson o << 1, l, m#define rson o << 1|1, m+1, rusing namespace std;typedef __int64 LL;const int MAX = 0x3f3f3f3f;const int maxn = 100010;int n, m;int vis[maxn], a[maxn], b[maxn];int main(){ cin >> n >> m; for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = n; i >= 1; i--) { b[i] = b[i+1]; if(!vis[ a[i] ]) { b[i]++; vis[ a[i] ] = 1; } } while(m--) { int tmp; scanf("%d", &tmp); printf("%d\n", b[tmp]); } return 0;}
Zookeeper