Today's headlines 2018 school recruitment back-end direction (user preference) c++

Title description :

In order to continuously optimize the recommendation effect, Toutiao stores and processes massive amounts of data every day. Suppose there is a scenario: we label users according to their registration time. For a type of article, each user has a different preference value. We will want to know the users registered in a certain period of time (the label is connected to one In batch users), how many users have a preference value of k for this type of article. For some special reasons, the user range of one query will not completely cover the user range of another query (there is no L1<=L2<=R2<=R1).

Enter a description:

Input: The first line is n represents the number of users, the second line is n integers, and the i-th line represents the user's preference for a certain type of article with the user label i. The third line is a positive integer q represents the number of groups in the query. Line 4 Go to line (3+q), each line contains 3 integers l, r, k representing a set of queries, that is, the number of users with a preference value of k for this type of article among the users labeled l<=i<=r number. Data range n <= 300000, q<=300000 k is an integer

Output description:

Output: a total of q lines, each line an integer represents the number of users with a preference value of k

Example 1

enter

5
1 2 3 3 5
3
1 2 1
2 4 5
3 5 3

Output

1
0
2

Description

Sample explanation:
There are 5 users, the preference values ​​are 1, 2, 3, 3, 5,
For the first group of queries, the number of users with a preference value of 1 for the user labeled [1,2] is 1.
For the second group of queries, the number of users with a preference value of 5 for the user labeled [2,4] is 0
For the third group of queries, the number of users with a preference value of 3 for the user labeled [3,5] is 2.

answer:

The vector type array used at the beginning, the result of the k value is too large, directly segfaults.

Positive solution: Using multimap in the c++ STL library, multimap provides an STL map type that can have duplicate keys. Its insertion method is similar to map. If you don’t know how to use it, please Baidu yourself. 

Here you need to explain the following code: ret=equal_range(k): The return value of the function is a pair, which stores the iterator ranges of the same key k respectively.

Insert the user preference value as the key and the user label as the value . All that remains is to traverse the interval of the same key k.

#include<bits/stdc++.h>
using namespace std;
#define mk make_pair
typedef long long ll;
multimap <int,int> mp;
int n;
int a,q;
int l,k,r,ans;
int main(void)
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a);
        mp.insert(mk(a,i));
    }
    scanf("%d",&q);
    for(int i=0;i<q;i++)
    {
        scanf("%d%d%d",&l,&r,&k);
        auto ret=mp.equal_range(k);
        ans=0;
        for(auto it=ret.first;it!=ret.second;it++)
        {
            if(it->second>=l&&it->second<=r)
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/Yang_1998/article/details/82252327