Snuke Festival(二分法)

题目描述

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

binary函数写的太麻烦了,不如分开写,不过总算是通过了。

在数据量很大的时候,使用sort函数排序之后在用二分法是很快的。

代码:

#include <iostream>     
#include <algorithm>         
using namespace std;
typedef long long ll;
const int inf=1e5+10;
int A[inf],B[inf],C[inf];

ll binary(int *arr,int low,int high,int temp,int flag)  
{
    while(low<=high)  
    {
        int middle=(low+high)/2;
        if(arr[middle]==temp)      
        {
            if(flag==1)
            {
                high=middle;
                while(low<=high)
                {
                    middle=(low+high)>>1;
                    if(arr[middle]>=temp) high=middle-1;
                    else low=middle+1;
                } 
                return high+1;     
            }    
            else
            {
                low=middle;
                while(low<=high)
                {
                    middle=(low+high)>>1;
                    if(arr[middle]<=temp)low=middle+1;  
                    else high=middle-1;
                }
                return low-1;                           
            }
        }
        if(arr[middle]<temp)
            low=middle+1;
        if(arr[middle]>temp)
            high=middle-1;    
    }
    return high;

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&A[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&B[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&C[i]);
    sort(A+1,A+n+1);
    sort(B+1,B+n+1);    
    sort(C+1,C+n+1);
    long long sum=0; 
    for(int i=1;i<=n;i++)
    {                                    
        ll ans1=binary(A,1,n,B[i],1);      
        if(A[ans1]==B[i])
            ans1--; 
        ll ans2=binary(C,1,n,B[i],3);        
        ans2=n-ans2;
        sum=sum+ans1*ans2;        
    }
    printf("%lld\n",sum);                 
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40799464/article/details/81517407