Title Description
JERRY TOM cat and mouse contest recently, but after all, they are adults, they do not like to have to play with the kind of starting a game, and now they like to play statistics. Recently, TOM cat to a human Now called "reverse order" something, something which is defined as: For a given sequence of positive integers period, reverse and aj i <j is an ordered sequence of ai> Correct. Knowing this concept, whoever they match a given period is calculated positive integer number of sequences in the reverse order.
Update: Data has been strengthened.
Input Format
The first line, a number n, there is a sequence number n.
Second line number n, the given sequence. Each sequence number does not exceed 10 ^ 9
Output Format
A given number of the reverse sequence.
Description / Tips
For 25% of the data, n≤2500
For 50% of the data, n≤4 × 10 ^ 4
For all data, n≤5 × 10 ^ 5
Please use a faster input-output
N it should not be over 500,000 square bar by chen_zhe
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
const int N=5e5+10;
#define int long long
using namespace std;
int c[N],n,a[N],b[N];
inline void add(int x,int y){
for(;x<=n;x+=x&(-x))c[x]+=y;
}
inline int sum(int x){
int ans=0;
for(;x;x-=x&(-x))ans+=c[x];
return ans;
}
signed main(){
cin>>n;
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
b[i]=a[i];
}
sort(b+1,b+1+n);
int len=unique(b+1,b+1+n)-b-1;
for(int i=1;i<=n;i++)
a[i]=lower_bound(b+1,b+1+len,a[i])-b;
int ans=0;
for(int i=n;i>=1;i--){
ans+=sum(a[i]-1);
add(a[i],1);
}
cout<<ans<<endl;
}