Description
UVa 11991 - Easy Problem from Rujia Liu?
Problem: Given an array, your task is to find the k-th occurrence (from left to right) of an integer v. To make the problem more difficult (and interesting!), you’ll have to answer m such queries.
Sample Input
There are several test cases. The first line of each test case contains two integers n, m (1 ≤ n, m ≤ 100, 000), the number of elements in the array, and the number of queries. The next line contains n positive integers not larger than 1,000,000. Each of the following m lines contains two integer k and v (1 ≤ k ≤ n, 1 ≤ v ≤ 1, 000, 000). The input is terminated by end-of-file (EOF).
1
2
3
4
5
6
8 4
1 3 2 2 4 3 2 1
1 3
2 4
3 2
4 2
Sample Output
For each query, print the 1-based location of the occurrence. If there is no such element, output ‘0’ instead.
1
2
3
4
2
0
7
0
Ideas
Use Graph representation to store the occurance. Use map<int, vector
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (typeof((c)).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
int main()
{
int n, m;
while (scanf("%d %d", &n, &m) != EOF)
{
unordered_map<int, vector<int>> mp;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
if (mp.find(x) != mp.end())
mp[x].push_back(i + 1);
else
mp[x] = vector<int>(1, i + 1);
}
for (int i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
if (mp.find(b) == mp.end() || mp[b].size() < a)
cout << 0 << endl;
else
cout << mp[b][a - 1] << endl;
}
}
return 0;
}