题目链接:Codeforces 368B Sereja and Suffixes
由于是计算每个位置及其后边有多少个不同元素,所以input完成后从后向前遍历一次,这个过程中用map去重。
#include <iostream>
#include <map>
using namespace std;
const int MAX_N = 100000 + 1000;
int a[MAX_N];
int n,m;
map<int,int> _hash;
int main()
{
cin >> n >> m;
for(int i = 0;i < n;i++)
cin >> a[i];
int cnt = 0;
for(int i = n - 1;i >= 0;i--)
{
if(!_hash.count(a[i]))
{
cnt++;
_hash[a[i]] = 1;
a[i] = cnt;
}
else
a[i] = cnt;
}
int temp;
for(int i = 0;i < m;i++)
{
cin >> temp;
cout << a[temp - 1] << endl;
}
return 0;
}