Contest1368 - 2018年第三阶段个人训练赛第八场. Snuke Festival(STL)

问题 J: Snuke Festival

时间限制: 1 Sec  内存限制: 128 MB
提交: 655  解决: 153
[提交] [状态] [讨论版] [命题人:admin]

题目描述

The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.
He has N parts for each of the three categories. The size of the i-th upper part is Ai, the size of the i-th middle part is Bi, and the size of the i-th lower part is Ci.
To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.
How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.

Constraints
1≤N≤105
1≤Ai≤109(1≤i≤N)
1≤Bi≤109(1≤i≤N)
1≤Ci≤109(1≤i≤N)
All input values are integers.

输入

Input is given from Standard Input in the following format:
N
A1 … AN
B1 … BN
C1 … CN

输出

Print the number of different altars that Ringo can build.

样例输入

2
1 5
2 4
3 6

样例输出

3

提示

The following three altars can be built:
Upper: 1-st part, Middle: 1-st part, Lower: 1-st part
Upper: 1-st part, Middle: 1-st part, Lower: 2-nd part
Upper: 1-st part, Middle: 2-nd part, Lower: 2-nd part

解题思路 : 便利第二层数据,利用函数库里的 lower_bound 和 upper_bound 二分求出每一个数据前后有多少数累加即可。

参考程序:

#include<bits/stdc++.h>
using namespace std;
long long a[100010],b[100010],c[100010];

int main()
{
    int  n,m,T;
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    while(cin>>n)
    {
        for(int i=0; i<n; i++) cin>>a[i];
        for(int i=0; i<n; i++) cin>>b[i];
        for(int i=0; i<n; i++) cin>>c[i];
        sort(a,a+n);
        sort(b,b+n);
        sort(c,c+n);

        long long sum1,sum2,ans=0;
        for(int i=0; i<n; i++)
        {
            sum1=lower_bound(a,a+n,b[i])-a;
            sum2=upper_bound(c,c+n,b[i])-c;
            ans+=(n-sum2)*sum1;
        }
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/XxxxxM1/article/details/81387631