Input
There are several test cases. The rst 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-le (EOF).
Output
For each query, print the 1-based location of the occurrence. If there is no such element, output `0'
instead.
Sample Input
8 4
1 3 2 2 4 3 2 1
1 3
2 4
3 2
4 2
Sample Output
2
0
7
0
题目大意:
给你一个长度为n的数组,然后询问你第k个val的下标是多少,n<=1e6, m<=1e6。
解题思路:
一看到这个题就想到了mlogn的方法,但是由于STL的水平有限,并没有独立AC,还是看了书上的代码才弄懂。。。
总结下,map<int,vector<int> >mp; 表示的是一个map类型的变量mp,mp[ ]就相当于一个容器,mp[a][b]表示的是mp[a]这个容器的第b+1个元素是多少。
有了这个概念,我们就知道了,如果要求n个数字中,第k个val的下标是多少,那么我们直接调用mp[val][k-1]就行了。
代码:
# include<cstdio> # include<iostream> # include<vector> # include<map> using namespace std; map<int,vector<int> >a; int main(void) { int n,m; while ( scanf("%d%d",&n,&m)!=EOF ) { a.clear(); for ( int i = 0;i < n;i++ ) { int x; scanf("%d",&x); if ( a.count(x)==0 ) a[x] = vector<int>(); a[x].push_back(i+1); } while (m--) { int x, y; scanf("%d%d",&x,&y); if ( a.count(y)==0||a[y].size() < x ) puts("0"); else printf("%d ",a[y][x-1]); } } return 0; }