Advertisement
Linkin_Park

Sereja and Suffixes, Tarasov's Contest

Feb 25th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. /*
  2. G. Sereja and Suffixes
  3. time limit per test1 second
  4. memory limit per test256 megabytes
  5. inputstandard input
  6. outputstandard output
  7. Sereja has an array a, consisting of n integers a1, a2, ..., an. 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.?
  8.  
  9. 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.
  10.  
  11. Input
  12. The first line contains two integers n and m (1 ≤ n, m ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105) — the array elements.
  13.  
  14. Next m lines contain integers l1, l2, ..., lm. The i-th line contains integer li (1 ≤ li ≤ n).
  15.  
  16. Output
  17. Print m lines — on the i-th line print the answer to the number li.
  18. */
  19.  
  20. #include <iostream>
  21. #include <cstdio>
  22. #include <vector>
  23. #include <algorithm>
  24. #include <string>
  25. #include <set>
  26. #include <map>
  27.  
  28. using namespace std;
  29.  
  30. typedef long long int64;
  31.  
  32. int main()
  33. {
  34.     int elementsQty, m;
  35.     cin >> elementsQty >> m;
  36.  
  37.     vector <int> array(elementsQty);
  38.     vector <int> tempSum(elementsQty);
  39.     set <int> isInArray;
  40.  
  41.     tempSum[elementsQty - 1] = 1;
  42.  
  43.     for (int i = 0; i < elementsQty; i++)
  44.     {
  45.         // Input the array
  46.         cin >> array[i];
  47.     }
  48.  
  49.     isInArray.insert(array[elementsQty - 1]);
  50.  
  51.     for (int i = elementsQty - 2; i >= 0; i--)
  52.     {
  53.         // Loop for counting different elements
  54.         // since i-th element (we start it in the end
  55.         // of array because we need to find qty of different elements
  56.         // from i-th elements till the last)
  57.         tempSum[i] = tempSum[i + 1];
  58.  
  59.         if (isInArray.find(array[i]) == isInArray.end())
  60.         {
  61.             tempSum[i]++;
  62.             isInArray.insert(array[i]);
  63.         }
  64.     }
  65.  
  66.     for (int i = 0; i < m; i++)
  67.     {
  68.         // Just output the data that we found out on the
  69.         // previous loop
  70.         int number;
  71.         cin >> number;
  72.         --number; // Because array indeces counts from 0
  73.  
  74.         cout << tempSum[number] << endl;
  75.     }
  76.  
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement