-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemyisweak.java
More file actions
46 lines (44 loc) · 1.16 KB
/
enemyisweak.java
File metadata and controls
46 lines (44 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
import java.util.List;
public class enemyisweak {
static int mod = (int) 1e9 + 7;
static int[] bit;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++)arr[i]=sc.nextInt();
int[] temp=arr.clone();
Arrays.sort(temp);
for(int i=0;i<n;i++)arr[i]=Arrays.binarySearch(temp,arr[i])+1;
long result=0;
bit=new int[n+1];
for(int i=n-1;i>=0;i--){
int smaller=sum(arr[i]);
int greater=i-(arr[i]-smaller-1);
result+=(long)smaller*greater;
update(arr[i]);
}
System.out.println(result);
}
static int sum(int i){
int res=0;
while(i>0){
res+=bit[i];
i-=(i&-i);
}
return res;
}
static void update(int i){
while(i<bit.length){
bit[i]++;
i+=(i&-i);
}
}
}