Enemy is weak

The Romans have attacked again. This time they are much more than the Persians but Shapur is ready to defeat them. He says: "A lion is never afraid of a hundred sheep".

Nevertheless Shapur has to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number.

In Shapur's opinion the weakness of an army is equal to the number of tripletsi, j, k such that i < j < k and ai > aj > ak where ax is the power of man standing at position x. The Roman army has one special trait — powers of all the people in it are distinct.

Help Shapur find out how weak the Romans are.

Input

The first line of input contains a single number n (3 ≤ n ≤ 106) — the number of men in Roman army. Next line contains n different positive integers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 109) — powers of men in the Roman army.

Output

A single integer number, the weakness of the Roman army.

Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).

Example
Input
3
3 2 1
Output
1
Input
3
2 3 1
Output
0
Input
4
10 8 3 1
Output
4
Input
4
1 5 4 3
Output
1

题解:求出左端比该位置大的元素和右端比该位置小的元素。

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
typedef long long ll;
const int Maxn=1e6+10;
using namespace std;
int n,sums[Maxn],suml[Maxn];
ll l[Maxn],s[Maxn];
struct nod{
    int val,index;
}a[Maxn];
bool cmp1(nod a,nod b){
    return a.val>b.val;
}
bool cmp2(nod a,nod b){
    return a.val<b.val;
}
int lowbit(int x){
    return x&(-x);
}
void updata(int x){
    while(x<=n){
        suml[x]++;
        x+=lowbit(x);
    }
}
void downdata(int x){
    while(x>0){
        sums[x]++;
        x-=lowbit(x);
    }
}
ll getSuml(int x){
    ll ret=0;
    while(x>0){
        ret+=suml[x];
        x-=lowbit(x);
    }
    return ret;
}
ll getSums(int x){
    ll ret=0;
    while(x<=n){
        ret+=sums[x];
        x+=lowbit(x);
    }
    return ret;
}
int main(){
    //freopen("in.txt","r",stdin);
    while(scanf("%d",&n)!=EOF){
        for(int i=1;i<=n;i++) suml[i]=0,sums[i]=0;
        for(int i=1;i<=n;i++) scanf("%d",&a[i].val),a[i].index=i;
        sort(a+1,a+1+n,cmp1);
        ll ans=0;
        for(int i=1;i<=n;i++) l[a[i].index]=getSuml(a[i].index),updata(a[i].index);
        sort(a+1,a+1+n,cmp2);
        for(int i=1;i<=n;i++) s[a[i].index]=getSums(a[i].index),downdata(a[i].index);
        for(int i=1;i<=n;i++) ans+=l[a[i].index]*s[a[i].index];
        printf("%I64d\n",ans);
    }
}

这种方法简单一些。。。

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
typedef long long ll;
const int Maxn=1e6+10;
using namespace std;
int n;
struct nod{
    int val,index;
}a[Maxn];
bool cmp(nod a,nod b){
    return a.val>b.val;
}
int lowbit(int x){
    return x&(-x);
}
struct st{
    ll sum[Maxn];
    void updata(int x,ll val){
        while(x<=n){
            sum[x]+=val;
            x+=lowbit(x);
        }
    }
    ll getSum(int x){
        ll ret=0;
        while(x>0){
            ret+=sum[x];
            x-=lowbit(x);
        }
        return ret;
    }
};
st sum1,sum2;
int main(){
    //freopen("in.txt","r",stdin);
    while(scanf("%d",&n)!=EOF){
        for(int i=1;i<=n;i++) sum1.sum[i]=0,sum2.sum[i]=0;
        for(int i=1;i<=n;i++) scanf("%d",&a[i].val),a[i].index=i;
        sort(a+1,a+1+n,cmp);
        ll ans=0;
        for(int i=1;i<=n;i++){
            ll cnt1=sum1.getSum(a[i].index);//比该位置元素大个数
            ll cnt2=sum2.getSum(a[i].index);//比该位置元素大对数
            ans+=cnt2;
            sum1.updata(a[i].index,1);
            sum2.updata(a[i].index,cnt1);
        }
        printf("%I64d\n",ans);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值