This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mx=4e5+20;
const int mod= 1e9+7 ;
const ll inf=1e9+5;
//***while there is life there is hope
int n;
int arr[mx*4];
map<ll,int>mp;
void compres(){
int cnt=0;
for(auto &it:mp){
//cout<<it.first;
it.second=cnt;
cnt++;
}
}
void update(int node,int st,int en,int pos){
if(st==en){
arr[node]++;
return;
}
int mid=(st+en)/2;
if(pos<=mid){
update(node*2,st,mid,pos);
}else{
update(node*2+1,mid+1,en,pos);
}
arr[node]=arr[node*2]+arr[node*2+1];
return ;
}
int query(int node,int st,int en,int l,int r){
if(st>r||en<l){return 0;}
if(st>=l&&en<=r){return arr[node];}
int mid=(st+en)/2;
return query(node*2,st,mid,l,r)+query(node*2+1,mid+1,en,l,r);
}
int l[mx];int r[mx];
int main() {
cin>>n;
vector<ll>v(n);
for(int i=0;i<n;i++){
cin>>v[i];
mp[v[i]];
}
compres();
for(int i=0;i<n;i++){
v[i]=mp[v[i]];
// cout<<v[i];
}
for(int i=0;i<n;i++){
l[i]=query(1,0,mx,0,v[i]-1);
// cout<<l[i]<<" ";
update(1,0,mx,v[i]);
}
memset(arr,0,sizeof(arr));ll ans=0;
for(int i=n-1;i>=0;i--){
r[i]=query(1,0,mx,0,v[i]-1);
//cout<<r[i]<<" ";
update(1,0,mx,v[i]);
ans+=1LL*r[i]*l[i];
}cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |