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.
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.
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).
3 3 2 1
1
3 2 3 1
0
4 10 8 3 1
4
4 1 5 4 3
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);
}
}