Sereja and Suffixestime limit:1000msmemory limit:262144kbthis problem would be judged onCodeforces. Original id:368b
64-bit integer IO format: %i64d Java class name: (any) Sereja have an array
a, consisting of
NIntegers
a1,
a2,...,
a
N. The boy cannot sit and does nothing, he decided to study an array. Sereja took a piece of paper and wrote out
mIntegers
L1,
L2, ...,
l
m (1≤
L
i ≤
n ). For each number
l I he wants to know how Many distinct numbers is staying on the Positions
l
i ,
l
I + 1, ...,
n . Formally, he want to find the number of distinct numbers Among
a
l
i ,
a
l
I + 1, ...,
a
n .?
Sereja wrote out the necessary array elements but the array is so large and the boy is so pressed for time. Help him, find the answer for the described question for each l.
Input
The first line contains Integers n and m (1≤ n , m ≤10 5). The second line Contains n integers < Span class= "Tex-span" > a 1, a 2, ..., a n (1≤ a I ≤105) -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 l i.
Sample InputInput
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
SourceCodeforces Round #215 (Div. 2) Problem solving: Offline query by idiot
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN =100010;4 structQuery {5 intIdx,ask;6 } Q[MAXN];7 BOOLVISIB[MAXN];8 intN,M,D[MAXN],RET[MAXN];9 BOOLcmpConstQuery &a,ConstQuery &b) {Ten returnA.ask >B.ask; One } A intMain () { -scanf"%d%d",&n,&m); - for(inti =0; I < n; ++i) thescanf"%d", d+i); - for(inti =0; I < m; ++i) { -scanf"%d",&q[i].ask); -Q[i].idx =i; + } -Sort (q,q+m,cmp); + for(inti =0, j = N1, cnt =0; I < m && J >=0; ++i) { A while(J >=0&& Q[i].ask-1!=j) { at if(!Visib[d[j]]) { -cnt++; -VISIB[D[J]] =true; - } -j--; - } in if(J >=0&& Q[i].ask-1==j) { - if(!Visib[d[j]]) { tocnt++; +VISIB[D[J]] =true; - } theRET[Q[I].IDX] =CNT; * } $ }Panax Notoginseng for(inti =0; I < m; ++i) -printf"%d\n", Ret[i]); the return 0; +}
View Code
Codeforces 368B Sereja and suffixes