Atcoder arc 084 C - Snuke Festival(前缀和+二分)

题目链接:http://arc084.contest.atcoder.jp/tasks/arc084_a


C - Snuke Festival


Time limit : 2sec / Memory limit : 256MB

Score : 300 points

Problem Statement

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

  • 1N105
  • 1Ai109(1iN)
  • 1Bi109(1iN)
  • 1Ci109(1iN)
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A1  AN
B1  BN
C1  CN

Output

Print the number of different altars that Ringo can build.


Sample Input 1

Copy
2
1 5
2 4
3 6

Sample Output 1

Copy
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

Sample Input 2

Copy
3
1 1 1
2 2 2
3 3 3

Sample Output 2

Copy
27

Sample Input 3

Copy
6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288

Sample Output 3

Copy
87

解析:这个题显然不能用O(n^2)或者O(n^3),只能O(n*log(n)),这里就想到了前缀和

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
 
const int N = 100009;
 
int a[N], b[N], c[N];
LL f[N];
 
 
int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++) scanf("%d", &a[i]);
    for(int i = 0; i < n; i++) scanf("%d", &b[i]);
    for(int i = 0; i < n; i++) scanf("%d", &c[i]);
    sort(a, a+n);
    sort(b, b+n);
    sort(c, c+n);
    LL ans = 0;
 
    for(int i = 0; i < n; i++)
    {
        if(b[i] >= c[n-1]) break;
        f[i+1] = 1ll*n - (upper_bound(c, c+n, b[i]) - c);
    }
    f[0] = 0;
    for(int i = 1; i <= n; i++) f[i] += f[i-1];
 
    for(int i = 0; i < n; i++)
    {
        if(a[i] >= b[n-1]) break;
        int s = upper_bound(b, b+n, a[i]) - b;
        ans += f[n] - f[s];
    }
 
    printf("%lld\n", ans);
    return 0;
 
 
 
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值