fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Scanner sc = new Scanner(System.in);
  13.  
  14. int n = sc.nextInt();
  15. int m = sc.nextInt();
  16. int[] arrn = new int[n];
  17. int[] arrm = new int[m];
  18. int[] dp = new int[n];
  19.  
  20. for(int i = 0; i<n;i++){
  21. arrn[i] = sc.nextInt();
  22. }
  23.  
  24. for(int i = 0; i<m;i++){
  25. arrm[i] = sc.nextInt();
  26. }
  27.  
  28. Set<Integer> set = new HashSet<Integer>();
  29. set.add(arrn[n-1]);
  30. dp[n-1] = 1;
  31. for(int i = n-2; i>=0;i--){
  32. if(set.contains(arrn[i])){
  33. dp[i] = dp[i+1];
  34. } else {
  35. dp[i] = dp[i+1]+1;
  36. set.add(arrn[i]);
  37. }
  38. }
  39.  
  40. for(int i = 0; i<m;i++){
  41. System.out.println(dp[arrm[i]-1]);
  42. }
  43. }
  44. }
Success #stdin #stdout 0.12s 35316KB
stdin
10 10
1 2 3 4 1 2 3 4 100000 99999
1
2
3
4
5
6
7
8
9
10
stdout
6
6
6
6
6
5
4
3
2
1