Submission #33508589


Source Code Expand

import java.util.*;
import java.io.*;

class Main {

    static int[] tree;
    
    static int len;

    static HashMap<Integer, Integer>[] map;

    
    public static int lowbit(int i){
        return i & -i;
    }

    public static void add(int i, int v, int c) {
        while (i < len) {
            tree[i] += v;
            map[i].put(c, map[i].getOrDefault(c, 0) + 1);
            i += lowbit(i);
        }
    }

    public static long sum(int i) {
        long res = 0;
        while (i > 0) {
            res += tree[i];
            i -= lowbit(i);
        }
        return res;
    }

    public static long sumColor(int i, int c) {
        long res = 0;
        while (i > 0) {
            res += map[i].getOrDefault(c, 0);
            i -= lowbit(i);
        }
        return res;
    }


    public static void main(String[] args) throws Exception {
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = read(br)[0];
        var C = read(br);
        var X = read(br);

        tree = new int[N+1];
        len = N;
        map = new HashMap[N];
        for (int i = 0; i < N; i++) {
            map[i] = new HashMap<>();
        }
        long res = 0;
        for (int i = N-1; i >= 0; i--) {
            add(X[i], 1, C[i]);
            res += sum(X[i]-1) - sumColor(X[i]-1, C[i]);
        }
        // Arrays.stream(map).forEach(System.out::println);
        out.println(res);
        out.flush();
    }


    public static int[] read(BufferedReader br) throws Exception {
        return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
}

Submission Info

Submission Time
Task F - Sorting Color Balls
User Resolmi
Language Java (OpenJDK 11.0.6)
Score 500
Code Size 1789 Byte
Status AC
Exec Time 2534 ms
Memory 318892 KiB

Compile Error

Note: ./Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 37
Set Name Test Cases
Sample example_00.txt, example_01.txt, example_02.txt
All example_00.txt, example_01.txt, example_02.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt
Case Name Status Exec Time Memory
example_00.txt AC 83 ms 34216 KiB
example_01.txt AC 86 ms 34436 KiB
example_02.txt AC 80 ms 34336 KiB
hand_00.txt AC 1816 ms 318892 KiB
hand_01.txt AC 1578 ms 269328 KiB
hand_02.txt AC 1719 ms 281396 KiB
hand_03.txt AC 740 ms 116512 KiB
hand_04.txt AC 1942 ms 269360 KiB
hand_05.txt AC 76 ms 34116 KiB
hand_06.txt AC 81 ms 34280 KiB
random_00.txt AC 2169 ms 248088 KiB
random_01.txt AC 1858 ms 225568 KiB
random_02.txt AC 839 ms 100192 KiB
random_03.txt AC 633 ms 97968 KiB
random_04.txt AC 605 ms 87976 KiB
random_05.txt AC 2072 ms 231792 KiB
random_06.txt AC 1891 ms 216276 KiB
random_07.txt AC 1138 ms 127324 KiB
random_08.txt AC 729 ms 99052 KiB
random_09.txt AC 638 ms 99504 KiB
random_10.txt AC 2282 ms 255264 KiB
random_11.txt AC 2250 ms 238380 KiB
random_12.txt AC 1779 ms 194124 KiB
random_13.txt AC 1033 ms 107276 KiB
random_14.txt AC 836 ms 100172 KiB
random_15.txt AC 2534 ms 261668 KiB
random_16.txt AC 2365 ms 243752 KiB
random_17.txt AC 2002 ms 211188 KiB
random_18.txt AC 1344 ms 139552 KiB
random_19.txt AC 1083 ms 111612 KiB
random_20.txt AC 2406 ms 249944 KiB
random_21.txt AC 2523 ms 266296 KiB
random_22.txt AC 1975 ms 221336 KiB
random_23.txt AC 1605 ms 151404 KiB
random_24.txt AC 1091 ms 111368 KiB
random_25.txt AC 2470 ms 250732 KiB
random_26.txt AC 2392 ms 250284 KiB