AcWing 241. 楼兰图腾
原题链接
简单
作者:
Karma
,
2021-03-12 17:15:31
,
所有人可见
,
阅读 322
import java.util.*;
class Main{
static int[] tree, nums, ll, lh, rl, rh;
static int n;
static int lowBit(int x){
return x & -x;
}
static void add(int x, int a){
for(int i=x; i<=n; i+=lowBit(i))
tree[i] += a;
}
static int sum(int x){
int res = 0;
for(int i=x; i>0; i -= lowBit(i))
res += tree[i];
return res;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
n = in.nextInt();
tree = new int[n+1];
ll = new int[n+1];
lh = new int[n+1];
nums = new int[n+1];
for(int i=0; i<n; i++){
int cur = in.nextInt();
nums[i] = cur;
add(cur, 1);
ll[cur] = sum(cur-1);
lh[cur] = sum(n) - sum(cur);
}
Arrays.fill(tree, 0);
long resh = 0, resl = 0;
for(int i=n-1; i>=0; i--){
int cur = nums[i];
add(cur, 1);
resl += (long)ll[cur] * sum(cur-1);
resh += (long)lh[cur] * (sum(n) - sum(cur));
}
System.out.println(resh +" " + resl);
}
}