#include <bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define _zero(a) memset(a, 0, sizeof(a))
#define _far(a) memset(a, 0x3f,sizeof (a))
#define __far(a) memset(a, -0x3f,sizeof (a))
#define far 0x3f3f3f3f
#define endl '\n'
#define int long long
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define rep(a,b,c) for(int (a) = (b);(a)<=(c);(a)++)
#define per(a,b,c) for(int (a)=(b);(a)>=(c);(a)--)
#define sqr(a) (a)*(a)
#define VI vector<int>
#define VII vector<vector<int>>
#define pb push_back
#define PII pair<int,int>
typedef long long ll;
typedef double dd;
typedef long double ld;
using namespace std;
const int inf = 1e10;
const int M = 998244353;
const int maxn = 2e5 + 1000;
const ld pi = atan2(0, -1);//arctan(y/x);
const ld eps = 1e-4;
const int N=2e5+100;
int n;
int a[N];
int t[N],c[N];
int r[N],l[N];
int ask(int x)
{
int ans=0;
for(;x;x-=x&-x)ans+=c[x];
return ans;
}
void add(int x,int y)
{
for(;x<=n;x+=x&-x)c[x]+=y;
}
void getback()
{
_zero(c);
rep(i,1,n)add(i,t[i]);
}
signed main()
{
IOS;
cin>>n;
int ans1=0,ans2=0;
rep(i,1,n)cin>>a[i];
_zero(t);
rep(i,1,n)t[a[i]]++;
getback();
_zero(r);_zero(l);
per(i,n,1)
{
int k=a[i];
l[i]=ask(n)-ask(k);
add(k,-1);
}
getback();
rep(i,1,n)
{
int k=a[i];
r[i]=ask(n)-ask(k);
add(k,-1);
}
rep(i,1,n)ans1+=l[i]*r[i];
_zero(r);_zero(l);
getback();
per(i,n,1)
{
int k=a[i];
l[i]=ask(k-1);
add(k,-1);
}
getback();
rep(i,1,n)
{
int k=a[i];
r[i]=ask(k-1);
add(k,-1);
}
rep(i,1,n)ans2+=l[i]*r[i];
cout<<ans1<<" "<<ans2<<endl;
return 0;
}
acwing 241
最新推荐文章于 2025-04-25 23:48:06 发布