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 rep(i,a,b) for (int i=a;i<=b;i++)
#define rep2(i,a,b,c) for (int i=a;i<=b;i+=c)
#define rev(i,a,b) for (int i=a;i>=b;i--)
#define rev2(i,a,b,c) for (int i=a;i>=b;i-=c)
#define ii pair<int,int>
#define bit(i,j) ((i>>j)&1)
#define ull unsigned long long
#define pb push_back
#define ll long long
#define int long long
#define F first
#define S second
const int N=3e5+5;
const int mod=1e9+7;
const int MAX=1e9;
int n,res,a[N],dp[N],s[N*4],dem[N];
vector<int> uni;
void update(int id,int l,int r,int i,int val)
{
if(i>r||i<l)
return;
if(l==r)
{
s[id]+=val;
return;
}
int mid=(l+r)/2;
update(id*2,l,mid,i,val);
update(id*2+1,mid+1,r,i,val);
s[id]=s[id*2]+s[id*2+1];
}
int get(int id,int l,int r,int u,int v)
{
if(v<l||r<u)
return 0;
if(l>=u&&r<=v)
{
return s[id];
}
int mid=(l+r)/2;
return get(id*2,l,mid,u,v)+get(id*2+1,mid+1,r,u,v);
}
void solution()
{
cin>>n;
rep(i,1,n)
{
cin>>a[i];
uni.push_back(a[i]);
}
sort(uni.begin(),uni.end());
uni.resize(unique(uni.begin(),uni.end())-uni.begin());
rep(i,1,n) a[i]=lower_bound(uni.begin(),uni.end(),a[i])-uni.begin()+1;
rep(i,1,n)
{
dp[i]=get(1,1,n,0,a[i]-1);
update(1,1,n,a[i],1);
}
memset(s,0,sizeof(s));
rev(i,n,1){
res+=dp[i]*get(1,1,n,0,a[i]-1);
update(1,1,n,a[i],1);
}
cout<<res;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if(fopen("Mountains.inp","r")){freopen("Mountains.inp","r",stdin);freopen("Mountains.out","w",stdout);}
int test=1;
// cin>>test;
while(test--)
solution();
}
/*
*/
Compilation message (stderr)
Mountains.cpp: In function 'int32_t main()':
Mountains.cpp:72:43: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | if(fopen("Mountains.inp","r")){freopen("Mountains.inp","r",stdin);freopen("Mountains.out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
Mountains.cpp:72:78: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | if(fopen("Mountains.inp","r")){freopen("Mountains.inp","r",stdin);freopen("Mountains.out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |