Cf #215 div2: B. sereja and suffixes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.