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;
#define ll long long
const int mxn=300005;
int n;
ll fwt[mxn], h[mxn], tmp[mxn], lf[mxn], rg[mxn];
map<ll, ll>mp;
void upd(int x, int v){for(; x<mxn; x+=x&-x) fwt[x]+=v;}
int que(int x){ll ans=0; for(; x>0; x-=x&-x) ans+=fwt[x]; return ans;}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n;
for(int i=0; i<n; i++){cin>>h[i]; tmp[i]=h[i];}
sort(tmp, tmp+n); int cnt=1;
for(int i=0; i<n; i++){
if(i==0) mp[tmp[i]]=cnt;
else if(tmp[i]>tmp[i-1]) {mp[tmp[i]]=cnt+1; cnt++;}
else mp[tmp[i]]=cnt;
}
for(int i=0; i<n; i++) h[i]=mp[h[i]]; //coordinate compression
// for(int i=0; i<n; i++) cout<<h[i]<<" "; cout<<endl;
memset(fwt, 0, sizeof(fwt));
for(int i=0; i<n; i++){
lf[i]=que(h[i]-1);
upd(h[i], 1);
}
for(int i=0; i<n; i++) rg[i]=que(h[i]-1)-lf[i];
ll ans=0ll;
for(int i=0; i<n; i++) ans+=lf[i]*rg[i];
cout<<ans<<endl;
return 0;
}
# | 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... |