Created
September 16, 2016 16:45
-
-
Save chermehdi/45edf438bfed320c6e237b7137521498 to your computer and use it in GitHub Desktop.
Murder Spoj Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.algorithms; | |
/** | |
* @Author Mehdi Maick | |
* Created on 15/09/2016. | |
*/ | |
import java.util.*; | |
import java.io.*; | |
public class Murder { | |
public static void solve(FastReader fs, PrintWriter pw) { | |
int t = fs.nextInt(); | |
while (t-- > 0) { | |
int n = fs.nextInt(); | |
long[] tab = fs.nextLongArray(n); | |
long[] hold = tab.clone(); | |
Arrays.sort(tab); | |
for (int i = 0; i < n; i++) { | |
int pos = Arrays.binarySearch(tab, hold[i]); | |
hold[i] = pos; | |
} | |
long sum = 0L; | |
FenwickTree ft = new FenwickTree(n + 1); | |
for (int i = 0; i < n; i++) { | |
sum += ft.query((int) hold[i]); | |
ft.update((int) hold[i] + 1, (int) tab[(int) hold[i]]); | |
} | |
pw.println(sum); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
FastReader fs = new FastReader(System.in); | |
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); | |
solve(fs, pw); | |
fs.close(); | |
pw.close(); | |
} | |
static class FastReader { | |
BufferedReader reader; | |
StringTokenizer st; | |
FastReader(InputStream stream) { | |
reader = new BufferedReader(new InputStreamReader(stream)); | |
st = null; | |
} | |
String next() { | |
while (st == null || !st.hasMoreTokens()) { | |
try { | |
String line = reader.readLine(); | |
if (line == null) { | |
return null; | |
} | |
st = new StringTokenizer(line); | |
} catch (Exception e) { | |
throw new RuntimeException(); | |
} | |
} | |
return st.nextToken(); | |
} | |
String nextLine() { | |
String s = null; | |
try { | |
s = reader.readLine(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return s; | |
} | |
int nextInt() { | |
return Integer.parseInt(next()); | |
} | |
long nextLong() { | |
return Long.parseLong(next()); | |
} | |
double nextDouble() { | |
return Double.parseDouble(next()); | |
} | |
char nextChar() { | |
return next().charAt(0); | |
} | |
int[] nextIntArray(int n) { | |
int[] arr = new int[n]; | |
int i = 0; | |
while (i < n) { | |
arr[i++] = nextInt(); | |
} | |
return arr; | |
} | |
long[] nextLongArray(int n) { | |
long[] arr = new long[n]; | |
int i = 0; | |
while (i < n) { | |
arr[i++] = nextLong(); | |
} | |
return arr; | |
} | |
int[] nextIntArrayOneBased(int n) { | |
int[] arr = new int[n + 1]; | |
int i = 1; | |
while (i <= n) { | |
arr[i++] = nextInt(); | |
} | |
return arr; | |
} | |
long[] nextLongArrayOneBased(int n) { | |
long[] arr = new long[n + 1]; | |
int i = 1; | |
while (i <= n) { | |
arr[i++] = nextLong(); | |
} | |
return arr; | |
} | |
void close() { | |
try { | |
reader.close(); | |
} catch (IOException e) { | |
System.err.println("There's been an error trying closing the reader "); | |
e.printStackTrace(); | |
} | |
} | |
} | |
static class FenwickTree { | |
long[] tree; | |
public FenwickTree(int size) { | |
tree = new long[size + 1]; | |
} | |
public long query(int ind) { | |
long sum = 0L; | |
while (ind > 0) { | |
sum += tree[ind]; | |
ind -= ind & (-ind); | |
} | |
return sum; | |
} | |
public long query(int a, int b) { | |
return query(b) - query(a - 1); | |
} | |
public void update(int ind, int value) { | |
while (ind < tree.length) { | |
tree[ind] += value; | |
ind += ind & (-ind); | |
} | |
} | |
public int size() { | |
return tree.length - 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment